W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
if語(yǔ)句后面可以跟隨可選的else語(yǔ)句,該語(yǔ)句在布爾表達(dá)式為false時(shí)執(zhí)行。
D編程語(yǔ)言中if.else語(yǔ)句的語(yǔ)法是-
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á)式的計(jì)算結(jié)果為true,則執(zhí)行代碼的if塊,否則執(zhí)行代碼的else塊。
d編程語(yǔ)言將任何非零和非NULL值假定為TRUE,如果它是零或NULL,則假定它為FALSE值。
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;
}
編譯并執(zhí)行上述代碼時(shí),將生成以下結(jié)果-
a is not less than 20;
value of a is : 100
If語(yǔ)句后面可以跟隨可選的Else If.Else語(yǔ)句,這對(duì)于使用單個(gè)If.Else If語(yǔ)句測(cè)試各種條件非常有用。
在使用If,Else語(yǔ)句時(shí),有幾點(diǎn)需要牢記-
if可以有零個(gè)或一個(gè)其他IF,并且它必須在任何其他IF之后。
if可以有零到許多其他if,它們必須出現(xiàn)在else之前。
一旦Else If成功,就不會(huì)測(cè)試其余的Else If或Else。
D編程語(yǔ)言中if.else語(yǔ)句的語(yǔ)法是-
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;
}
編譯并執(zhí)行上述代碼時(shí),將生成以下結(jié)果-
None of the values is matching
Exact value of a is: 100
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: