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-1 | Its represents outer "if statement" condition or set of conditions. |
condition-2 | Its represents inner "if statement" condition or set of conditions. |
Block-1 | It represents a statement or set of statements of inner "if statement". |
Block-2 | It represents a statement or set of statements of outer "if statement". |
Block-3 | It represents a statement or set of statements of "else" part of its outer "if statement". |
Flowchart
The general flowchart of this type of selection structure is as follows: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.
Example Programs
Example 1: The following source code of the program inputs three numbers and finds out the smallest value using "nested if statement".Output of the Program:
#include
#include
main()
{
float num1, num2, num3, mini;
clrscr();
cout
cin>>num1;
cout
cin>>num2;
cout
cin>>num3;
if ( num1
if (num1
mini = num1;
else
mini = num3;
else
if (num2
mini = num2;
else
mini = num3;
cout
getch();
}
(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".
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).
#include
#include
main()
{
int a, b, c, d;
clrscr();
cout
cin>>a;
cout
cin>>b;
cout
cin>>c;
cout
cin>>d;
if (a == d)
if ( b == c && c == d)
cout
else
cout
else
cout
getch();
} // end main function
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.
Output of the Program:
#include
#include
main()
{
int math, physics, chemistry, average;
char grade;
clrscr();
cout
cin>>math;
cout
cin>>physics;
cout
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 cout
getch();
}
(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