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

上一篇: D語言決策語句 下一篇: D語言if...else語句