鍍金池/ 教程/ C++/ D語言if...else語句
類的訪問修飾符
D語言運(yùn)算符
D語言邏輯運(yùn)算符
類指針
D語言元組
D語言指針
D語言模塊
D語言sizeof運(yùn)算符
D語言混合類型
D語言封裝
D語言條件編譯
類的靜態(tài)成員
D語言do...while循環(huán)
D語言結(jié)構(gòu)體
重載
D語言字符串-String
D語言決策語句
D語言接口
D語言for循環(huán)
D語言switch語句
D語言關(guān)聯(lián)數(shù)組
D語言范圍
D語言枚舉Enums
契約式編程
D語言并發(fā)
D語言開發(fā)環(huán)境設(shè)置
D語言別名
D語言常值
D語言常量
D語言函數(shù)
D語言if嵌套語句
D語言循環(huán)
D語言概述,D語言是什么?
D語言運(yùn)算符優(yōu)先級
D語言continue語句
D語言異常處理
D語言break語句
D語言if...else語句
D語言類和對象
類繼承
D語言字符
D語言教程
D語言關(guān)系運(yùn)算符
比較操作符重載
構(gòu)造函數(shù)和析構(gòu)函數(shù)
D語言抽象類
D語言if語句
D語言賦值運(yùn)算符
D中算術(shù)運(yùn)算符
D語言類成員函數(shù)
D語言位運(yùn)算符
D語言變量
D語言數(shù)據(jù)類型
D語言文件I/O
D語言數(shù)組
一元運(yùn)算符重載
D語言嵌套switch語句
D語言基本語法
二元運(yùn)算符重載
this指針
D語言聯(lián)合體
D語言模板
D語言嵌套循環(huán)
D語言while循環(huán)

D語言if...else語句

一個(gè)if語句后面可以跟一個(gè)可選的else語句,該語句執(zhí)行時(shí)的布爾表達(dá)式為false。

語法

在D編程語言的if... else語句的語法是:

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

如果布爾表達(dá)式的值為true,那么if代碼塊將被執(zhí)行代碼,否則else塊將被執(zhí)行。

D編程語言假設(shè)任何非零和非空值作為true,如果是零或null,則假定為false。

流程圖:

D if...else statement

例子:

import std.stdio;
 
int main ()
{
   /* local variable definition */
   int a = 100;
 
   /* check the boolean condition */
   if( a < 20 )
   {
       /* if condition is true then print the following */
       writefln("a is less than 20" );
   }
   else
   {
       /* if condition is false then print the following */
       writefln("a is not less than 20" );
   }
   writefln("value of a is : %d", a);
 
   return 0;
}

當(dāng)上面的代碼被編譯并執(zhí)行,它會產(chǎn)生以下結(jié)果:

a is not less than 20;
value of a is : 100

if...else if...else語句

一個(gè)if后面可以跟一個(gè)可選的if... else語句,如果測試各種條件下... else if語句,可使用單一的else語句。

當(dāng)使用 if , else if , else語句有幾點(diǎn)要記?。?/p>

  • if可以有零個(gè)或else,它必須跟在else if后面。

  • if 可以有零到多個(gè)else if,它們必須在else之前。

  • 一旦一個(gè) else if 匹配成功,剩余else if不會被測試或計(jì)算。

語法

if...else if...else語句在D編程語言的語法是:

if(boolean_expression 1)
{
   /* Executes when the boolean expression 1 is true */
}
else if( boolean_expression 2)
{
   /* Executes when the boolean expression 2 is true */
}
else if( boolean_expression 3)
{
   /* Executes when the boolean expression 3 is true */
}
else 
{
   /* executes when the none of the above condition is true */
}

例子:

import std.stdio;
 
int main ()
{
   /* local variable definition */
   int a = 100;
 
   /* check the boolean condition */
   if( a == 10 )
   {
       /* if condition is true then print the following */
       writefln("Value of a is 10" );
   }
   else if( a == 20 )
   {
       /* if else if condition is true */
       writefln("Value of a is 20" );
   }
   else if( a == 30 )
   {
       /* if else if condition is true  */
       writefln("Value of a is 30" );
   }
   else
   {
       /* if none of the conditions is true */
       writefln("None of the values is matching" );
   }
   writefln("Exact value of a is: %d", a );
 
   return 0;
}

當(dāng)上面的代碼被編譯并執(zhí)行,它會產(chǎn)生以下結(jié)果:

None of the values is matching
Exact value of a is: 100