codecamp

D编程 if...else statement

if语句后面可以跟随可选的else语句,该语句在布尔表达式为false时执行。

if...else - 语法

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 */
}

如果布尔表达式的计算结果为true,则执行代码的if块,否则执行代码的else块。

d编程语言将任何非零和非NULL值假定为TRUE,如果它是零或NULL,则假定它为FALSE值。

if...else - 流程图

D if...else statement

if...else - 示例

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; 
}

编译并执行上述代码时,将生成以下结果-

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

if...elseif...else - 语句

If语句后面可以跟随可选的Else If.Else语句,这对于使用单个If.Else If语句测试各种条件非常有用。

在使用If,Else语句时,有几点需要牢记-

  • if可以有零个或一个其他IF,并且它必须在任何其他IF之后。

  • if可以有零到许多其他if,它们必须出现在else之前。

  • 一旦Else If成功,就不会测试其余的Else If或Else。

D编程语言中if.else语句的语法是-

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 */
}

if...elseif...else - 示例

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; 
}

编译并执行上述代码时,将生成以下结果-

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


D编程 if statement
D编程 nested if statements
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }