Latest web development tutorials

C nested if statements

Analyzing C Analyzing C

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

grammar

C language syntax fornested if statements:

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

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

Examples

#include <stdio.h>
 
int main ()
{
   /* 局部变量定义 */
   int a = 100;
   int b = 200;
 
   /* 检查布尔条件 */
   if( a == 100 )
   {
       /* 如果条件为真,则检查下面的条件 */
       if( b == 200 )
       {
          /* 如果条件为真,则输出下面的语句 */
          printf("a 的值是 100,且 b 的值是 200\n" );
       }
   }
   printf("a 的准确值是 %d\n", a );
   printf("b 的准确值是 %d\n", b );
 
   return 0;
}

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

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

Analyzing C Analyzing C