当前位置: 首页 > 文档资料 > 学习 C 语言 >

if statement

优质
小牛编辑
120浏览
2023-12-01

if语句由一个布尔表达式后跟一个或多个语句组成。

语法 (Syntax)

C编程语言中'if'语句的语法是 -

if(boolean_expression) {
   /* statement(s) will execute if the boolean expression is true */
}

如果布尔表达式的计算结果为true ,那么将执行'if'语句中的代码块。 如果布尔表达式的计算结果为false ,那么将执行'if'语句结束后(在结束大括号之后)的第一组代码。

C编程语言将任何non-zeronon-null值假定为true ,如果它zeronull ,则将其假定为false值。

流程图 (Flow Diagram)

C if语句

例子 (Example)

#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\n" );
   }
   printf("value of a is : %d\n", a);
   return 0;
}

编译并执行上述代码时,会产生以下结果 -

a is less than 20;
value of a is : 10