Nested if else statement in C++

When we use an if else statement inside other if else block then this is called nested if else statement. These are used to check multiple condition in different stages of the program which determine flow of program. However, it is recommended to see our if else statement article first to easily understand this nested if else statement guide.

The syntax of nested if else is as follows;

// syntax of nested if else statement

if(condition){
    // code to execute when the condition is true
    if(condition){
        // code to execute when the condition will be true
        if(condition){
            // code to execute
        }
    }
}

In the above syntax we can also use else and else if block for each of the if block. We can nest a number of if statement according to our need.

See the bellow program where we have used an if else statement inside another. If you enter zero then the “if else statement” of second layer will be executed. Let’s try to understand the program.

#include<iostream>
using namespace std;

int main(){
    int num;
    cout << "Enter a number here : ";
    cin >>num;

    if(num == 0){
        cout << "\nYou have entered zero\n";
        cout << "\n\tEnter a positive or negative number : ";
        cin >>num;

        if(num >= 0){
            cout << "\n\t" << num << " is positive.\n";
        }else{
            cout << "\n\t" << num << " is negative.\n";
        }

    }else if(num > 0){
        cout << "\n\t" << num << " is positive.\n";

    }else{
        cout << "\n\t" << num << " is negative.\n";
    }

    return 0;

}

When a user gives the input zero then the program will ask for a positive or negative number again.

nested-if-else-statement-in-c-nesting-condition

But if he enter a positive or negative number at the first time then the second layer of if else does not executed.

nested-if-else-statement-in-c-condition-nesting

C++ program to find leap year using nested if else

In this program we will use nested if else statement to check a year that it is leap year or not. Here we will implement the following condition to check a given year.

  • Year should be divisible by 4.
  • Year should not be divisible by 100 but if it is divisible by 400 then the year will be leap year.
// leap year program using nested if else statement

#include <iostream>
using namespace std;

int main(){
    int given_year;

    cout << "Enter a year to check : ";
    cin >> given_year;

    if ( given_year % 4 == 0 ){    // first layer of if else sttement

        if ( given_year % 100 == 0 ){    // second layer of if else

            if ( given_year % 400 == 0 ){    // third layer
                cout << "\n\t" << given_year << " is leap year.\n";

            }else{
                cout << "\n\t" << given_year << " is not leap year.\n";
            }

        }else{
            cout << "\n\t" << given_year << " is leap year.\n";
        }

    }else{
        cout << "\n\t" << given_year << " is not leap year.\n";
    }

    return 0;
}

Output of this nested if else program:

Now let’s check the program giving different year as input.

If you give 2000 as the input then every condition will be true. So the code under most inner block will be executed.

Enter a year to check : 2000

        2000 is leap year.

When you give the input 2004, then only the outer if statement will be true and other two nested if else statement will be false.

Enter a year to check : 2004

        2004 is leap year.

The else block of first if statement will be executed when a user give such year which is not divisible by 4.

Enter a year to check : 2017

        2017 is not leap year.

Previous pageif else in C++

Next pageswitch case in C++