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

C if...else Statement

C if...else Statement

C Judgment C Judgment

A if statement An if statement can be followed by an optional else statement, the else statement executes when the Boolean expression is false.

Syntax

In the C language if...else statement syntax:



if(boolean_expression)

{

   /* statement to execute if the boolean expression is true */

}

else

{

   /* statement to execute if the boolean expression is false */

}

If the Boolean expression is true, then the if block's code is executed. If the Boolean expression is false, then the else block's code.

C treats anynon-zeroandnon-nullvalue as true, and treatszeroor null as false.

Flowchart

Example

Example

#include

<

stdio.h

int

main

(

)

{

/*

Local variable definition

*/

int

a

=

100

;

/*

Check the Boolean condition

*/

if

(

a

<

20

)

{

/*

If the condition is true, the following statement is output

*/

printf

(

"

a is less than 20

\

n

"

)

;

}

else

{

/*

If the condition is false, the following statement is output

*/

printf

(

"

a is greater than 20

\

n

"

)

;

}

printf

(

"

The value of a is %d

\

n

"

,

a

)

;

return

0

;

}

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



a 大于 20

a 的值是 100

if...else if...else statement

A if statement can be followed by an optional else if...else statement, which can be used to test multiple conditions.

When using the if...else if...else statement, note the following:

Syntax

In C, the if...else if...else statement syntax:



if(boolean_expression 1)

{

   /* Execute when boolean expression 1 is true */

}

else if( boolean_expression 2)

{

   /* Execute when boolean expression 2 is true */

}

else if( boolean_expression 3)

{

   /* Execute when boolean expression 3 is true */

}

else 

{

   /* execute when none of the above conditions are true */

}

Example

Example

#include

 

int

main

(

)

{

   

/* Local variable definition */

   

int

a

=

100

;

 

   

/* Check the Boolean condition */

   

if

(

a

==

10

)

   

{

       

/* If the if condition is true, output the following statement */

       

printf

(

"The value of a is 10\n"

)

;

   

}

   

else

if

(

a

==

20

)

   

{

       

/* If the else if condition is true, output the following statement */

       

printf

(

"The value of a is 20\n"

)

;

   

}

   

else

if

(

a

==

30

)

   

{

       

/* If the else if condition is true, output the following statement */

       

printf

(

"The value of a is 30\n"

)

;

   

}

   

else

   

{

       

/* If none of the above conditions is true, output the following statement */

       

printf

(

"No matching value\n"

)

;

   

}

   

printf

(

"The exact value of a is %d\n"

,

a

)

;

 

   

return

0

;

}

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



没有匹配的值

a 的准确值是 100

C Judgment C Judgment