C default 關鍵字
示例
指定在 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"
自己動手試一試 »
定義和用法
default
關鍵字在 switch
語句中指定預設的程式碼塊,當 switch 中沒有匹配的 case 時,將執行此程式碼。
注意:如果 default
關鍵字是 switch 塊中的最後一個語句,則不需要 break。
相關頁面
有關 switch 語句的更多資訊,請參閱我們的 C switch 教程。