D编程 switch statement
2021-09-01 11:24 更新
switch语句允许根据值判断是否相等性,每个值称为一个case语句,并且为每个switch case检查每个变量。
switch - 语法
D编程语言中switch语句语法如下所示:-
switch(expression) {
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
* you can have any number of case statements */
default : /* Optional */
statement(s);
}
switch - 流程图
switch - 示例
import std.stdio;
int main () {
/* local variable definition */
char grade='B';
switch(grade) {
case 'A' :
writefln("Excellent!" );
break;
case 'B' :
case 'C' :
writefln("Well done" );
break;
case 'D' :
writefln("You passed" );
break;
case 'F' :
writefln("Better try again" );
break;
default :
writefln("Invalid grade" );
}
writefln("Your grade is %c", grade );
return 0;
}
编译并执行上述代码时,将生成以下结果-
Well done
Your grade is B
以上内容是否对您有帮助:
更多建议: