C++

Nested If in C++

In C++ programming, there are various situations in which we need to check the conditions. Sometimes, we need to satisfy multiple conditions simultaneously. We utilize the “nested if” condition in C++ programming for this. If we place the “if” condition inside the other “if”, it is said to be the “nested if”. When the first “if” condition is satisfied, we move inside that “if” where we place another “if”. Then, it checks the “if” condition which is placed inside the first “if” condition, and so on. It returns the result which we place inside the “if” when all the conditions are satisfied.

Example 1:

Let’s do some codes where we utilize the “nested if”. To begin the C++ code, we include the “iostream” header file here and then the “std” namespace. After this, we place the “main()”driver code and initialize three variables, “n1”, “n2”, and “n3”, with the values of “20”, “10”, and “2”, respectively. Then, we utilize the “if” condition here in which we check whether “n1” is greater than “n2”. If this condition is satisfied, we move ahead inside this “if” where we add another “if” condition.

Now, the second “if” checks the “n1” values that are greater than “n3”. If this “nested if” condition is also satisfied, the statement below this is executed in which we place the “cout” statement. So, it prints that statement if both conditions of the “nested if” are satisfied in this code. If any of the conditions is false, it will not display any message on the console.

Code 1:

#include <iostream>
using namespace std;
int main()
{
    int n1 = 20, n2 = 10, n3 = 2;
    if (n1 > n2) {
        if (n1 > n3) {
            cout << " n1 is the largest values which is " << n1 << endl;
        }
    }
    return 0;
}

 

Output:

Here, it shows the statement on the console which means both “if” conditions of the “nested if” are true. The statement that we added inside the “nested if” is also rendered here.

Example 2:

The “std” namespace and the “iostream” header file are included here to start the C++ code. The “main()”driver code is then inserted, and three variables, “var1”, “var2”, and “var3”, are initialized with the values of “8”, “91,” and “32”, correspondingly.

Next, we use the “if” condition to determine whether “val1” is smaller than “val2”. If this condition is met, we continue within the “if” condition and add another “if” condition. The second “if” now checks to see if the “val1” value is smaller than “val3”. If this “nested if” condition is also fulfilled, the statement that is placed inside the “cout” is run in the following. Thus, if both of the “nested if” conditions are met in this code, it prints that statement. The console won’t show any message if the conditions are untrue.

Code 2:

#include <iostream>
using namespace std;
int main()
{

    int var1 = 8, var2 = 91, var3 = 32;
    if (var1 < var2) {
        if (var1 < var3) {
            cout << " var1 is the smallest values which is " << var1 << endl;
        }
    }
    return 0;
}

 

Output:

The statement that we added within the “nested if” is now rendered here. The message on the console indicates that both of the “if” conditions of the “nested if” are true.

Example 3:

Here in the “main()”, we initialize the variables named “x”, “y”, and “z” with the values of “11”, “22”, and “33”, respectively. Then, we utilize an “if” in which we place the condition that is “x == 11” and place another “if” inside this “if” which is the “nested if” where we add the “y == 22” condition. This “nested if” is implemented only when the first “if” condition is fulfilled.

After this, we utilize one more “if” inside the second “if” which is executed when both “if” are satisfied which we previously added. The third “if” contains the “z == 33” condition and we include the “cout” inside the last “if” condition. This will only execute when all the three “if” conditions are true.

Code 3:

#include <iostream>
using namespace std;
int main()
{
    int x = 11, y = 22, z = 33;
    if (x == 11) {
        if (y == 22) {
            if (z == 33) {
                cout << "Hey! C++ nested if here!!" << endl;
            }
        }
    }
    return 0;
}

 

Output:

This outcome shows that all the “nested if” conditions are satisfied. The statement inside the last “if” is displayed here. This statement won’t appear here if any of the listed conditions is false.

Example 4:

The “ch1”, “ch2”, and “ch3” variables are initialized as the “char” data type with the “a”, “b”, and “z” characters after invoking the “main()” function. Next, we employ an “if” statement with the “ch1 == a” condition and another “if” inside it (referred to as a “nested if”) with the additional “ch2 == b” condition. This means that only when the first “if” condition is met will this “nested if” be run.

Subsequently, we employ an additional “if” within the second “if” which is carried out upon the satisfaction of both of the previously inserted “if” conditions. The “ch3 == z” condition is found in the third “if” and the word “cout” is present in the last “if” condition. This will only pass if all conditions are fulfilled.

Code 4:

#include <iostream>
using namespace std;

int main()
{

    char ch1 = 'a', ch2 = 'b', ch3 = 'z';

    if (ch1 == 'a') {
        if (ch2 == 'b') {
            if (ch3 == 'z') {
                cout << "The C++ programming nested if..!!" << endl;
            }
        }
    }
    return 0;
}

 

Output:

The sentence inside the last “if” is shown here which indicates that all of the “nested if” conditions are satisfied. This statement won’t appear here if any of the conditions is false.

Example 5:

After calling the “main()” method, the “f1”, “f2”, and “f3” variables are initialized as the “float” data type with the “2.40”, “19.7”, and “43.1” values. Next, we use an “if” statement with the “f1 == 2.40” condition and another “if” (also known as a “nested if”) with the “f2 == 19.7” condition inside of it. This indicates that this “nested if” will only be performed if the first “if” condition is fulfilled.

Afterward, we use the third “if” inside the second “if” which is executed if both of the previously added “if” conditions are met. The third “if” condition has the “f3 == 9” condition, and the last “if” condition contains the “cout”. Only in a situation where all three requirements are met will this “cout” statement render. Then, outside all these “if” conditions, we also add the “cout” which is rendered when the “if” condition is not true.

Code 5:

#include <iostream>
using namespace std;

int main()
{

    float f1 = 2.40, f2 = 19.7 , f3 = 43.1;
    if (f1 == 2.40) {
        if (f2 == 19.7) {
            if (f3 == 9) {
                cout << "All nested if conditions are true here!!" << endl;
            }
        }
    }
    cout << " The condition of the nested if is not satisfied..!! " << endl;
    return 0;
}

 

Output:

Here, it displays the statement that we added outside the “nested if” condition and shows that the “nested if” condition is not satisfied.

Conclusion

The “nested if” concept in C++ is learned thoroughly here. We explored that “nested if” means that we placed an “if” condition inside the other “if” condition. We utilized this “nested if” condition where we had to fulfill numerous conditions simultaneously. We explored some examples in which we utilized the “nested if” condition in our C++ codes and explained how it works.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.