目录

if statement

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

语法 (Syntax)

D编程语言中if语句的语法是 -

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

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

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

流程图 (Flow Diagram)

D if语句

例子 (Example)

import std.stdio;
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 */ 
      writefln("a is less than 20" ); 
   } 
   writefln("value of a is : %d", a); 
   return 0;
}

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

a is less than 20;
value of a is : 10
↑回到顶部↑
WIKI教程 @2018