Open In App

C++ Nested if-else Statement

Last Updated : 31 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, there are various types of conditional statements such as if, if-else, nested if, nested if-else, and switch. In this article, we will learn about the nested if-else statement.

What are Nested if-else Statements?

Nested if-else statements are those statements in which there is an if statement inside another if else. We use nested if-else statements when we want to implement multilayer conditions(condition inside the condition inside the condition and so on). C++ allows any number of nesting levels.

Basic Syntax of Nested if-else

if(condition1)
{
// Code to be executed
if(condition2)
{
// Code to be executed
}
else
{
// Code to be executed
}
}
else
{
// code to be executed
}

In the above syntax of nested if-else statements, the inner if-else statement is executed only if ‘condition1’ becomes true otherwise else statement will be executed and this same rule will be applicable for inner if-else statements.

Flowchart of Nested if-else

flow diagram of nested if-else

Flow Diagram of Nested if-else

Examples of Nested if-else

The following C++ programs demonstrate the use of nested if-else statements.

Example 1: Check the greatest of three numbers

In the below example, we will find the greatest number out of the three given numbers using nested if-else statements.

Algorithm

Assume that a, b, and c are three given numbers:

  1. If ( a < b ) evaluates to true, then
    1. If ( c < b ) is true, then b is greatest.
    2. else c is greatest.
  2. If ( a < b ) evaluates to false, then
    1. If ( c < a ) is true, then a is greatest.
    2. else c is greatest.

Implementation in C++

C++




// C++ Program to evaluate largest of the three numbers
// using nested if else
#include <iostream>
using namespace std;
  
int main()
{
    // declaring three numbers
    int a = 10;
    int b = 2;
    int c = 6;
  
    // outermost if else
    if (a < b) {
        // nested if else
        if (c < b) {
            printf("%d is the greatest", b);
        }
        else {
            printf("%d is the greatest", c);
        }
    }
    else {
        // nested if else
        if (c < a) {
            printf("%d is the greatest", a);
        }
        else {
            printf("%d is the greatest", c);
        }
    }
  
    return 0;
}


Output

10 is the greatest



Example 2: Checking leap year using nested if-else

A year is a leap year if it follows the condition, the year should be evenly divisible by 4 then the year should be evenly divisible by 100 then lastly year should evenly divisible by 400 otherwise the year is not a leap year. 

Algorithm

  1. If the year is divisible by 4,
    1. If the year is divisible by 100,
      1. If the year is divisible by 400, the year is a leap year
      2. else the year is not a leap year.
    2. else the year is a leap year
  2. else, the year is not a leap year.

Implementation in C++

C++




// C++ program to check leap year using if-else
#include <iostream>
using namespace std;
  
int main()
{
    int year = 2023;
  
    if (year % 4 == 0) {
        // first nested if-else statement
        if (year % 100 == 0) {
            // second nested if-else statement
            if (year % 400 == 0) {
                cout << year << " is a leap year.";
            }
            else {
                cout << year << " is not a leap year.";
            }
        }
        else {
            cout << year << " is a leap year.";
        }
    }
    else {
        cout << year << " is not a leap year.";
    }
  
    return 0;
}


Output

2023 is not a leap year.



If the input year is 2023:

Output

2023 is not a leap year.



If the input year is 2024:

Output

2024 is a leap year.





Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads