C++ nested if statement

This statement allows the user to use if block inside the other if block. And the inner if statement is executed only if the outer if statement’s condition is true.

The syntax of the nested if statement in C++:

if(condition1)
{    
   //block of code to be executed    
   if(condition2)
   {  
     //block of code to be executed    
   }    
} 

You can also nest the if..else statement in same manner as shown below.

//Nested if....else statement

if(condition1)
{
    if(condition2)
    {
        //block of statement;
    }
    else 
    {
        //block of statement;
    }
}
else
{
    //block of statement;
}

nested if statement Flowchart:

nested if

Example of nested if statement in C++

#include <iostream>
using namespace std;

int main()
{
   int a = 50;
   int b = 110;

  	// check condition
   if (a < 100)
   {
     	// agian the use of if
      if (b < 200)
      {
         cout << "Value of a and b are 50 and 110 respectively" << endl;
      }
   }

   cout << "Value of a is: " << a << endl;
   cout << "Value of b is: " << b << endl;

   return 0;
}

Output:

Value of a and b are 50 and 110 respectively
Value of a is: 50
Value of b is: 110

Example of C++ nested if…else statement

#include <iostream>
using namespace std;

int main()
{
   int a, b, c;

   cout << "Enter 3 integers: ";
   cin >> a >> b >> c;

   if (a > b)
   {
      if (a > c)
      {
         cout << a << " is greatest of all.";
      }
      else
      {
         cout << c << " is greatest of all.";
      }
   }
   else
   {
      if (b > c)
      {
         cout << b << " is greatest of all.";
      }
      else
      {
         cout << c << " is greatest of all.";
      }
   }
}

Output:

Enter 3 integers: 2 5 4
5 is greatest of all.