switch语句允许根据值判断是否相等性,每个值称为一个case语句,并且为每个switch case检查每个变量。
switch - 语法
D编程语言中switch语句语法如下所示:-
switch(expression) { case constant-expression : statement(s); break; /* 可选 */case constant-expression : statement(s); break; /* 可选 */
default : /* 可选 */
statement(s); }
switch - 流程图

switch - 示例
import std.stdio;int main () { /* 局部变量定义 */ 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