Latest web development tutorials

C ++ nested if statements

C ++ judgment C ++ judgment

In C ++,nested if-else statement is legal, which means you can use another iforelse ifstatement in aniforelse ifstatement.

grammar

C ++nested if statements syntax:

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

You can nestelse if ... else, manner similar to nestedifstatements.

Examples

#include <iostream>
using namespace std;
 
int main ()
{
   // 局部变量声明
   int a = 100;
   int b = 200;
 
   // 检查布尔条件
   if( a == 100 )
   {
       // 如果条件为真,则检查下面的条件
       if( b == 200 )
       {
          // 如果条件为真,则输出下面的语句
          cout << "a 的值是 100,且 b 的值是 200" << endl;
       }
   }
   cout << "a 的准确值是 " << a << endl;
   cout << "b 的准确值是 " << 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

C ++ judgment C ++ judgment