The C language allows nested if statements in which the if block and/or else block of an if statement contains if or if-else statements. The inner if statement(s) may in turn contain other if statements and so on.
Two-Level Nested if Statements
Consider the general form of the if else statement given below.
else statement2 ;
The general form of a two-level nested if statement obtained by replacing both statement1 and statement2 in the above form with the if-else statement itself is given below.
Observe how the inner if-else statements are indented (shifted to the right) to improve readability. Also note that since the if block and else block of the outer if-else statement comprise a single if-else statement, they need not be included within braces. However, we can surround inner if-else statements with braces to improve readability as illustrated in the right-hand side form. As explained earlier, the use of such braces also prevents errors when we add one or more statements to these if and else blocks.
The flowchart for this general two-level nested if statement is given in Fig a. Observe that only one of the four alternative statements (statement}, statement2, statement3, statement4) will be executed depending on the values of the test expressions expr1, expr2 and expr3. Also note that these alternative statements can be any valid C statement such as a simple statement (assignment, function call, etc.), control statement or compound statement. The execution of this statement is as follows: Initially, expr1 is evaluated. If it is true (non-zero), expr2 is evaluated and depending on its value, either statement1 or statement2 is executed, i. e., if expr2 is true, statement] is executed; otherwise, statement2 is executed. After execution of either statement1 or statement2, the control leaves nested if statement. Note that expr3 is not evaluated in this case.
On the other hand, if expr1 evaluates as false, expr3 is evaluated and depending on its value, either statement3 or statement4 is executed. After execution of either statement, control leaves the nested if statement. Note that expr2 is not evaluated in this case.
Other Forms of Two-Level Nested if Statements
Consider that either the if block or the else block in the general form of the if-else statement is replaced with an if-else statement, but not both. Now we have two other forms of two-level nested if statement, as shown below.
The flowcharts for these statements are given in Fig b and Fig c, respectively. The second form which contains an if-else statement only in the else part is called as if else- if statement and is explained shortly in detail.
As the else block in an if-else statement is optional, we can omit the else blocks in the inner if statements to obtain several additional forms of the two-level nested if statement. However, while writing such statements we need to remember the following rule of nested if statements: An else clause is associated with the nearest if statement that is not already associated with an else clause.
If we omit the else clause in the inner if statements of a general two-level nested if statement, we get two other forms given below and illustrated in Fig.
Note that braces are required around the inner if statement in the first format for correct interpretation that the else-clause of the inner if statement is omitted, as illustrated in Fig. In the absence of these braces, the else clause of the outer if statement becomes incorrectly associated with the inner if statement. Also note that we do not require such braces in the second case.
Two other forms of two-level nested if statements are given below. In the first form, the else clause of the outer if statement is omitted. Whereas, in the second form, the else clause of the outer and inner if statements are omitted.
Illustrates nested if (expressions)
void main ()
float A, B, C;
printf("Enter two integers.");
scanf ("%f %f", &A, &B );
printf("You have entered the following two numbers.\n");
printf("A = %.3f\t B = %.3f \n", A, B );
if (A >B)
printf("A is greater than B.\n");
printf("A is less than or equal to B.\n" ); }
C = (A+ B)/2;
printf("%.3f is mean of the two numbers %.3f and %.3f\n",C,A,B);