Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Nested if Statements

C Nested if Statements

C Judgment C Judgment

In the C language,Nested if-else statements are legal, which means you can nest one inside a if or else if if statement inside another if or else if if statement.

Syntax

In the C language Nested if statement syntax:



if( boolean_expression 1)

{

   /* Execute when boolean expression 1 is true */

   if(boolean_expression 2)

   {

      /* Execute when boolean expression 2 is true */

   }

}

You can nest else if...else, in the same way as nesting if statements.

Example

Example

#include

<

stdio.h

int

main

(

)

{

/*

Local variable definition

*/

int

a

=

100

;

int

b

=

200

;

/*

Check the Boolean condition

*/

if

(

a

==

100

)

{

/*

If the condition is true, check the following condition

*/

if

(

b

==

200

)

{

/*

If the condition is true, the following statement is output

*/

printf

(

"

the value of a is 100, and the value of b is 200

\

n

"

)

;

}

}

printf

(

"

The exact value of a is %d

\

n

"

,

a

)

;

printf

(

"

The exact value of b is %d

\

n

"

,

b

)

;

return

0

;

}

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



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

a 的准确值是 100

b 的准确值是 200

C Judgment C Judgment