C++ Nested if Statements

C++ Decision Making

In C++,Nestedif-else statements are legal, which means you can in oneiforelse ifUse another within a statementiforelse ifstatement.

A nested if statement is a variant of an if statement, in which one if statement can be nested inside another if statement.

Nested if statements can help you test multiple conditions more precisely.

Syntax

In C++Nested ifSyntax of the statement:

if( boolean_expression 1)
{
   // 当布尔表达式 1 为 true 时执行
   if(boolean_expression 2)
   {
      // 当布尔表达式 2 为 ture 时执行
   }
}

You can nestelse if...else, method and nestingifsimilar to statements.

if (condition1) {
   // 如果 condition1 为 true,则执行此处的代码块
   if (condition2) {
      // 如果 condition2 也为 true,则执行此处的代码块
   }
   else {
      // 如果 condition2 为 false,则执行此处的代码块
   }
}
else {
   // 如果 condition1 为 false,则执行此处的代码块
}

Example

The following example uses nested if statements to evaluate different conditions:

Example

#include <iostream>
using namespace std;

int main() {
    int x = 10;

    if (x < 20) {
        cout << "x is less than 20" << endl;

        if (x < 15) {
            cout << "x is less than 15" << endl;
        }
    }

    return 0;
}

In the above example, first check whether x is less than 20. If the condition holds, the first message in the if statement will be output. Then, the program will check whether x is less than 15. If this condition also holds, then the second message in the if statement will be output.

When the above code is compiled and executed, it produces the following results:

x 小于 20
x 小于 15

Another nested if example:

Example 2

#include <iostream>
using namespace std;
 
int main ()
{
   // Local variable declaration
   int a = 100;
   int b = 200;
 
   // Check the boolean condition
   if( a == 100 )
   {
       // If the condition is true, then check the condition below
       if( b == 200 )
       {
          // If the condition is true, then output the following statement
          cout << "The value of a is 100, and the value of b is 200" << endl;
       }
   }
   cout << "The exact value of a is " << a << endl;
   cout << "The exact value of b is " << b << endl;
 
   return 0;
}

When the above code is compiled and executed, it produces the following results:

a 的值是 100,且 b 的值是 200
a 的准确值是 100
b 的准确值是 200

The following example demonstrates the use of nested if-else statements:

Example 3

#include <iostream>
using namespace std;

int main() {
    int x = 15;

    if (x < 20) {
        cout << "x is less than 20" << endl;

        if (x < 15) {
            cout << "x is less than 15" << endl;
        } else {
            cout << "x is greater than or equal to 15" << endl;
        }
    } else {
        cout << "x is greater than or equal to 20" << endl;
    }

    return 0;
}

In the above example, it first checks whether x is less than 20. If the condition is true, the first message in the if statement will be output.

Then, the program will check whether x is less than 15; if this condition also holds, then the second message in the if statement will be output. If the second condition does not hold, then the message in the else statement will be output.

If x is greater than or equal to 20, then the first condition of the if statement does not hold, and the message in the else statement will be output.

When the above code is compiled and executed, it produces the following results:

x 小于 20
x 大于等于 15

C++ Decision Making

other extensions