C default 关键字

定义和用法

default 关键字在 switch 语句中指定默认代码块,即当 switch 中没有匹配的 case 时要运行的代码。

注意:如果 default 关键字用作 switch 块的最后一个语句,则不需要 break

实例

指定在 switch 块中没有匹配的 case 时要运行的代码:

int day = 4;

switch (day) {
  case 6:
    printf("Today is Saturday");
    break;
  case 7:
    printf("Today is Sunday");
    break;
  default:
    printf("Looking forward to the Weekend");
}

// 输出 "Looking forward to the Weekend"

亲自试一试

相关页面

教程:switch 语句