if语句包含一个布尔表达式后跟一个或多个语句。
if语句在C编程语言中的语法是:
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
如果 if 语句布尔表达式代码的值为 true,那么此块将被执行。如果 if 语句的结束(右大括号后)布尔表达式的值为 false,那么之后第一个代码块会被执行。
C语言编程认定任何非零和非空值为true,如果是零或null,则假定为false
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* check the boolean condition using if statement */
if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20
" );
}
printf("value of a is : %d
", a);
return 0;
}
当上述代码被编译和执行时,它产生了以下结果:
a is less than 20; value of a is : 10