D 程式設計 - if 語句



一個if語句由一個布林表示式後跟一個或多個語句組成。

語法

D 程式語言中 if 語句的語法如下:

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

如果布林表示式計算結果為true,則執行 if 語句內部的程式碼塊。如果布林表示式計算結果為false,則執行 if 語句結束後的第一組程式碼(在閉合花括號之後)。

D 程式語言將任何非零非空值視為true,如果它是,則將其視為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_programming_decisions.htm
廣告

© . All rights reserved.