Use of nested if else in C programming

When we need to check multiple condition in different stage then we use nested if else condition. We have to write an if else condition inside another if block for making a nested if else. Simply we can say that when we have to check another condition inside a condition then it is called nested if else.

It will be clear to you if you understand the following example of nested if else in C programming.

#include <stdio.h>
int main(){
    int i, j;
    printf("Enter two integer to compare : \n");
    scanf("%d%d", &i, &j);

    if (i == j){
        printf("Two number was equal.\n");
    }else{
        if (i > j){       // nested if else condition
            printf("First number was largest.\n");
        }else{
            printf("Second number was largest.\n");
        }
    }

return 0;
}

Write the code in your editor and give two input. If you give two same number then the first if block will work. If you give two unequal number then the program will go to the else block and again check which is the biggest number at second if condition.

When you give two same integer the output will be as follows.

Enter two integer to compare :
35 35
Two number was equal.

Process returned 0 (0x0)   execution time : 13.717 s
Press any key to continue.

When you give two unequal integer the output will be as given bellow.

Enter two integer to compare :
24 41
Second number was largest.

Process returned 0 (0x0)   execution time : 10.607 s
Press any key to continue.

Generally we use nested if else to divide our program into several independent block. We can give the direction that what should do after going inside any block by nested if else. It should be noted that the inside block of if else will only work when the outer condition will be true. Otherwise the compiler will ignore the entire nested if else condition.

Let’s be clear by seeing the given program.

Example C program using nested if else statement

#include <stdio.h>
int main(){
    int x, y;
    printf("Enter two integer : \n");
    scanf("%d%d", &x, &y);

    if (x > y){      // First if block

        if (x % 2 == 0){      // first nested if else condition
            printf("First number is biggest and it is Even.\n");
            if(y % 2 == 0){       // second nested if else condition
                printf("Second number is smallest and it is Even.\n");
            }else{
                printf("Second number is smallest and it is Odd.\n");
            }
        }else{
            printf("First number is biggest and it is Odd.\n");
            if(y % 2 == 0){
                printf("Second number is smallest and it is Even.\n");
            }else{
                printf("Second number is smallest and it is Odd.\n");
            }
        }

    }else if(y > x){

        if (y % 2 == 0){       // another nested if else condition
            printf("Second number is biggest and it is Even.\n");
            if(x % 2 == 0){
                printf("First number is smallest and it is Even.\n");
            }else{
                printf("First number is smallest and it is Odd.\n");
            }
        }else{
            printf("Second number is biggest and it is Odd.\n");
            if(x % 2 == 0){
                printf("First number is smallest and it is Even.\n");
            }else{
                printf("First number is smallest and it is Odd.\n");
            }
        }

    }else{

        if(x % 2 == 0){
            printf("The two number is same and they are Even.\n");
        }else{
            printf("The two number is same and they are Odd.\n");
        }
    }

return 0;
}

Obviously we can write this program in another way. But we have given this program in such way that you can clearly understand nested if else condition. Then try to write the same program using different logic. Best of luck!

Previous pageIf else statement in C

Next pageSwitch case in C