C if statement

C Decision Making

aif statementConsists of a boolean expression followed by one or more statements.

Syntax

In C languageifSyntax of statements:

if(boolean_expression)
{
   /* 如果布尔表达式为真将执行的语句 */
}

If the boolean expression istruethen the code block within the if statement will be executed. If the boolean expression isfalsethen the first set of code after the if statement ends (after the closing brace) will be executed.

C language treats anynon-zeroandnon-nullThe value is assumed to betrue, takezeroornullassumed to befalse。

Flowchart

C 中的 if 语句

Example

Example

#include <stdio.h> int main () { /*Local variable definition*/ int a = 10; /*Use an if statement to check a boolean condition*/ if( a < 20 ) { /*If the condition is true, print the following statement*/ printf("a is less than 20\n" ); } printf("the value of a is %d\n", a); return 0; }

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

a 小于 20
a 的值是 10

C Decision Making

other extensions