Nesting of Decision Making Statements in C

Nesting if statement means placing if statement inside another if statement. The general syntax of nesting if statement is as follows:

if ( condition1 )
{
statement1;
if ( conditionA )

{
statementA;
}
statement2;
}

This means if condition1 evaluates to True, then statement1 will be executed. After this, conditionA will be checked, if it also evaluates to True then statementA will be executed, else not. However, in both cases statement2 will be executed.

It should be noted that same syntax applies to nesting of if-else construct. In fact, within the body of if statement or else part of if-else statement, we can have nested if statement or if-else statement as follows.

if ( condition1 )
{
statement1;
if ( conditionA )
{
statementA;
}
else
{
statementB;
}
statement2;
}

This means any variant of if statement can be nested inside another variant of if statement. In fact, the else-if ladder is actually nested if-else statement. Consider the code snippet below.

int x;
if ( x == 1 )
printf(“First one”);
else if ( x == 2 )
printf(“Second one”);
else if ( x == 3 )
printf(“Third one”);
else
printf(“Last one”);;
printf(“Done”);

In this code snippet we are using else-if ladder. Let us assume that x=1. Then, as the first condition evaluates to True, “First one” will be displayed. Now, no other condition will be checked and control will be transferred to “Done”. Now, consider that x=2, then first condition fails and second condition will be checked. Thus, this time “Second one” will be displayed as x==2 evaluates to True. Now, no more checks will be made and control will be transferred to “Done”.

Ok. This code snippet can re-arranged as follows.

int x;
if ( x == 1 )
printf(“First one”);
else
if ( x == 2 )
printf(“Second one”);
else
if ( x == 3 )
printf(“Third one”);
else
printf(“Last one”);;
printf(“Done”);

Yes. Actually, “Last one” and “Third one” are part of if-else statement which is nested inside the else part of another if-else statement “Second one”. And, this “Second one” if-else statement is actually nested into the else part of “First one” if-else statement. This is the only reason why all conditions before the True condition in else-if ladder are evaluated and all conditions after the True condition are never evaluated.

It is worth mentioning here that logically there is no limit on the level of nesting carried. However, if nesting is carried out to too deep a level and indenting is not consistent then deeply nested if or if-else statements can be confusing to read and interpret. Consider the code snippet below which is actually the above code without indentation.

int x;
if ( x == 1 )
printf(“First one”);
else
if ( x == 2 )
printf(“Second one”);
else
if ( x == 3 )
printf(“Third one”);
else
printf(“Last one”);;
printf(“Done”);

At first glance, it is very difficult to point out that this is nested if-else. Second, it is difficult to answer which construct is nested inside whom. One simple rule can solve this. It is important to note that an else always belongs to the closest if without an else.

As per this rule,
1. the “Last one” else belongs to “Third one” if,
2. this construct belongs is a statement that belongs to else above it.
3. this else belongs to “Second one” if statement.
4. this construct belongs is a statement that belongs to else above it.
5. this else belongs to “First one” if statement.