位置:首页 » 文章/教程分享 » D语言if语句

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

语法

if语句在D编程语言的语法是:

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

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

D编程语言假定任何非零和非空值作为true,如果它是零或为null,则假定为false。

流程图:

D if statement

例子:

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