C - Nested IF-Else

1. Nesting means "one within another".it is possible to nest if-else statement with another.There are several different forms that nested if-else statement can take.The most general form of two-layer nesting is

if(condition 1)
	 if(condition 2)
		 statement 1;
		 statement 2
	 if(condition 3)
		 statement 3;
		 statement 4;

where exp represent expression and s represent statement, statement can be simple or compound.

2. In this situation, one complete if-else statement (if (condition 2) statement 1 statement 2 ) will be executed if(condition 1) is nonzero (true), and another complete if-else statement(if (condition3) statement 3 and else statement 4 ) will be executed if condition is zero (false).

3. It is , of course possible that statement 1,statement 2,statement 3 and statement 4 themselves if-else statements.we would then have multi layer nesting.

→consider an example for calculating greatest of three numbers ,

#include <stdio.h>

int main()
    int a=5,b=10,c=15;
            printf("a is greatest");
            printf("c is greatest");
    else {
            printf("b is greatest");
            printf("c is greatest");


output :

c is greatest

  • Print Page