Your Ad Here

Wednesday, September 17, 2008

Nested ifs

A nested if is an if that is the target of another if or else. Nested ifs are very common in
programming. In a nested if, an else statement always refers to the nearest if statement that is within
the same block as the else and that is not already associated with an else. For example:
if(i)
{
if(j) dosomething1();
if(k) dosomething2(); /* this if */
else dosomething3(); /* is associated with this else */
}
else dosomething4(); /* associated with if(i) */
As noted, the final else is not associated with if(j) because it is not in the same block. Rather, the
final else is associated with if(i). Also, the inner else is associated with if(k), which is the nearest if.

No comments: