Introduction to Nested if in C++
Nested if in C++ is using more than one if statements in the same scope. The if statement is a decision-making statement that allows taking decisions based upon the condition specified. When there is more than one condition and they are dependent on one another, then if statement can be nested. The nested if statement allows checking multiple conditions one after another. Nested if statement is formed by using one if statement into another. The number of using them is not limited but the inner if statement will only execute when it’s outer if statement is true.
Syntax:
if ( condition1 ) {
// statement 1
if (condition 2 ) {
. . . and so on
// statement 2
}
}
Flowchart
Working of Nested if in C++
- In the above diagram, we have shown a flowchart for nested if with two if conditions.
- The first if condition will be checked first and in case if it is false then the program will exit the first if block and go to next statements after first if block.
- In the case of first if condition is true then the program will go into the body of first if. In the body of first if, we have second if condition.
- Now this condition will be checked and second if block will be executed.
- In the case of second if a condition is false then second if block will not be executed and the program will go to the statements after second if block.
- Note that second, if will only execute in case of first if is true.
- Similarly, when we will have more than two nested ifs the program will go inside as long as the consecutive if statements come as true.
- In case of if statement false the corresponding if block will not be executed.
Examples of Nested if in C++
Given below are the examples:
Example #1
All if conditions are true.
Code:
#include<iostream>
using namespace std;
int main()
{
int a = 10; // Declare and assign values to variables
int b = 20;
if (a == 10) { // first if condition : variable a is checked
cout << " value of a is: " << a << endl;
if ( b == 20) { // second if condition : variable b is checked
cout << " value of b is: " << b << endl;
}
}
return 0;
}
Output:
Here, we have nested two if the conditions. The first if condition is checking for the value of variable a. If the value matches then the first statement will be printed. Then the second if condition will be checked, in case of the second condition satisfies the second statement will be printed. In the output, it can be seen that both the statements are printed as conditions are satisfied in the required order.
Example #2
Only outer if condition is true.
Code:
#include <iostream>
using namespace std;
int main()
{
int a = 10; // Declare and assign values to variables
int b = 20;
if (a == 10) { // first if condition : variable a is checked
cout << " value of a is: " << a << endl;
if ( b == 30) { // second if condition : variable b is checked with wrong value
cout << " value of b is: " << b << endl;
}
}
return 0;
}
Output:
Here, the first statement is printed because only the outer if condition is true. When the program goes to the second if condition it becomes false and therefore the second statement is not printed.
Example #3
Inner if condition is true but outer if the condition is false.
Code:
#include <iostream>
using namespace std;
int main()
{
int a = 10; // Declare and assign values to variables
int b = 20;
if (a == 50) { // first if condition : variable a is checked with wrong value
cout << "value of a is: " << a << endl;
if ( b == 20) { // second if condition : variable b is checked
cout << " value of b is: " << b << endl;
}
}
cout << " End of program... " << endl;
return 0;
}
Output:
Here, even though the inner if the condition is true it is not checked because the outer if condition itself becomes false. As the first, if condition is false no code in first if block is executed and no statement from the above two is printed on the console output.
Example #4
Multiple if statements.
Code:
#include <iostream>
using namespace std;
int main()
{
int a = 10; // Declare and assign values to variables
int b = 20;
int c = 30;
if ( a < b ) { // first if condition : variable a is checked with variable b
cout << " a is less than b " << endl;
if ( b < c ) { // second if condition : variable b is checked with variable c
cout << " b is less than c " << endl;
if ( a < c ) { // third if condition : variable a is checked with variable c
cout << " a is less than c " << endl;
}
}
}
cout << " End of program... " << endl;
return 0;
}
Output:
Here we have nested three if conditional statements. There are three variables a, b and c. They are compared based on their values. We have taken all the conditions as true but in real-world applications, they can be anything. Notice the use of nested if statements here, because we can check the conditions one by one sequentially.
Conclusion
The nested if statement in C++ allows checking for multiple conditions. The multiple conditions will be checked one by one in order. If any of the outer if condition is false, then the sub sequential inner if statements will not be executed.
Recommended Articles
This is a guide to Nested if in C++. Here we discuss the introduction to Nested if in C++ along with the working and respective examples. You may also have a look at the following articles to learn more –