C++ Nested If Statement with examples

- February 26, 2018
When multiple conditions have to check in the program then "nested if statements" are used to solve the problem. The nested if structure may contain multiple if statements that may be nested up to any level.
If statement within another if statement is called nested if structure. The 'if statement' that contains another if statement is called outer if statement. Similarly, the if statement that is inside the outer if statement is called inner if statement.
The control enters into the "inner if structure" only when condition of the "outer if statement" is true.

Declaration

In C++, the general syntax of "nested if" structure is as follows:
if (condition-1)
  {
      if (condition-2)
         {
             Block-1;
         }
         else
         {
             Block-2;
          }
 else
   {
            Block-3;
   }
}

In the above syntax, the "if statement" that has "condition-1" contains another "if statement" that has "condition-2".
Where:
condition-1Its represents outer "if statement" condition or set of conditions.
condition-2Its represents inner "if statement" condition or set of conditions.
Block-1It represents a statement or set of statements of inner "if statement".
Block-2It represents a statement or set of statements of outer "if statement".
Block-3It represents a statement or set of statements of "else" part of its outer "if statement".

Nested if Structure Flowchart

The general flowchart of  this type of selection structure is as follows:
Nested in c++

Execution

The following is the logic of the execution of nested if statement:-
  • First of all, the condition of the "outer if statement" is evaluated.
  • If the condition of the "outer if statement" is true, then "inner if statement" is executed.
    • If condition of the inner if statement is true, then Block-1 is executed.
    • If condition of the inner if statement is false, then Block-2 is executed.
  • If condition of the "outer if statement" is false, then Block-3 is executed.

Nested if Statement Example Programs

Example 1: The following source code of the program inputs three numbers and finds out the smallest value using "nested if statement".
#include<iostream>
using namespace std;
main()
 {
   float num1, num2, num3, mini;
   clrscr();
   cout<<"Enter First Value:";
   cin>>num1;
   cout<<"Enter Second Value:";
   cin>>num2;
   cout<<"Enter Third Value:";
   cin>>num3;
   if ( num1 < num2)
      if (num1 < num3)
            mini = num1;
           else
            mini = num3;
   else
       if (num2 < num3)
            mini = num2;
           else
            mini = num3;
   cout<<"The Minimum Value is : "<<mini;
   return 0;
 }
Output of Program:
(suppose entered values are 12.5, 48, 8.3 respectively)

Enter First Value: 12.5
Enter Second Value: 48
Enter Third Value: 8.3
The Minimum Value is 8.3

Example 2: The following source code of the program inputs four integer values from the user and finds out if these values or equal or different using "nested if statement".

#include<iostream>
using namespace std;
main()
 {
   int a, b, c, d;
   cout<<"Enter First Value:";
   cin>>a;
   cout<<"Enter Second Value:";
   cin>>b;
   cout<<"Enter Third Value:";
   cin>>c;
   cout<<"Enter Fourth Value:";
   cin>>d;
   if (a == d)
      if ( b == c && c == d)
         cout<<"All values are equal. "<<endl;
      else
         cout<<"All values are different. "<<endl;
   else
       cout<<"These values are different.  "<<endl;
    return 0;
 } // end main function
Example 3: The following source code of C++ program finds out the grade of a student based on the average marks obtained in three subjects. (suppose subjects are Mathematics, Physics and Chemistry).
The grade is calculated as:

  • If average is greater than 80, grade is A.
  • If average is less than 80 and greater than 60, grade is B. 
  • If average is less than 60 and greater than 33, grade is C.
  • If average is less than 33, grade is F.

#include<iostream>
using namespace std;
main()
 {
   int math, physics, chemistry, average;
   char grade;
   clrscr();
   cout<<"Enter Marks of Mathematics:";
   cin>>math;
   cout<<"Enter Marks of Physics:";
   cin>>physics;
   cout<<"Enter Marks of Chemistry:";
   cin>>chemistry;
   average = (math + physics + chemistry) / 3; //to find average marks
   if (average > 33)
         if (average > 60)
               if ( average > 80)
                    grade = 'A';
              else
                    grade = 'B';
              else
                    grade = 'C';
   else
         grade = 'F';
   cout<<"Average Marks = "<<average<<endl;
   cout<<"Grade = "<<grade;
   return 0;
 }
Output of the Program:
(Suppose entered marks are 78, 65, 57 respectively)

Enter Marks of Mathematics: 78
Enter Marks of Physics: 65
Enter Marks of Chemistry: 57
Average Marks = 66
Grade = A