分支結構是程式「決策」的核心。C 語言提供 if(單/雙分支、多重 else if)與 switch 兩大機制,配合關係/邏輯運算子,能實現任意複雜的條件判斷。本文系統梳理語法、執行流程、常見陷阱,並以評分系統、位數判斷、成績等級、閏年判斷、日期天數計算五個實例深度講解。
if (表達式) {
語句; // 表達式非 0 時執行
}
// 後續語句繼續執行
範例:及格判斷
#include <stdio.h>
int main() {
double score;
printf("input the score: ");
scanf("%lf", &score);
if (score >= 60)
printf("\npass!\n");
printf("programming is over!\n");
return 0;
}
if (表達式) {
語句1; // 非 0 執行
} else {
語句2; // 為 0 執行
}
範例:及格/不及格分流
if (score >= 60)
printf("\npass!\n");
else
printf("fail!\n");
printf("programming is over!\n");
適用於「多個互斥條件,依序檢查」的場景。
if (條件1) { ... }
else if (條件2) { ... }
else if (條件3) { ... }
else { ... } // 以上皆非
範例:判斷整數位數
#include <stdio.h>
#include <math.h>
int main() {
int a; scanf("%d", &a);
if (abs(a) < 10) printf("%d is one bit.\n", a);
else if (abs(a) < 100) printf("%d is two bit.\n", a);
else if (abs(a) < 1000) printf("%d is three bit.\n", a);
else if (abs(a) < 10000) printf("%d is four bit.\n", a);
else if (abs(a) < 32767) printf("%d is five bit.\n", a);
else printf("the number is too large\n");
return 0;
}
switch (表達式) {
case 常量表達式1: 語句; [break;]
case 常量表達式2: 語句; [break;]
...
[default: 語句;]
}
關鍵規則:
float/double 不可)。case 標籤必須是編譯期常量,不可為變數。break 會「貫穿」執行下一個 case(可利用此特性合併情況)。default 可選,建議保留作為防禦性程式設計。#include <stdio.h>
int main() {
int score; printf("score="); scanf("%d", &score);
switch ((int)(score / 10)) { // 整數除法取十位
case 10: case 9: printf("Your grade is A\n"); break;
case 8: printf("Your grade is B\n"); break;
case 7: printf("Your grade is C\n"); break;
case 6: printf("Your grade is D\n"); break;
default: printf("Your grade is E\n");
}
return 0;
}
break 僅跳出當層 switch。
switch (n1) {
case 1:
switch (n2) { case 11: ...; break; }
break;
case 2: ...; break;
}
規則:能被 4 整除且不能被 100 整除,或能被 400 整除。
#include <stdio.h>
int main() {
int year; printf("input the year: "); scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
printf("該年是閏年\n");
else
printf("該年是平年\n");
return 0;
}
步驟:① 累加前幾月天數(用 switch),② 加上當月日數,③ 若為閏年且月份>2,總天數+1。
#include <stdio.h>
int main() {
int day, month, year, sum = 0, leap = 0;
printf("please input year,month,day\n");
scanf("%d%d%d", &year, &month, &day);
switch (month) {
case 1: sum = 0; break; case 2: sum = 31; break;
case 3: sum = 59; break; case 4: sum = 90; break;
case 5: sum = 120; break; case 6: sum = 151; break;
case 7: sum = 181; break; case 8: sum = 212; break;
case 9: sum = 243; break; case 10: sum = 273; break;
case 11: sum = 304; break; case 12: sum = 334; break;
default: printf("data error"); return 1;
}
sum += day;
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) leap = 1;
if (leap && month > 2) sum++;
printf("It is the %dth day.\n", sum);
return 0;
}
| 錯誤 | 說明 | 正確做法 |
|---|---|---|
if (a = 5) | 賦值而非比較,永真 | if (a == 5) 或 if (5 == a) (防誤寫) |
| switch 表達式用 float | 編譯錯誤 | 轉換為整型或改用 if-else |
| 漏寫 break | 非預期貫穿 | 刻意貫穿需加註釋 // fall through |
| 巢狀 if 縮進混亂 | 邏輯難讀 | 善用 else if 攤平、提前 return |
分支語句是程式邏輯的骨架。熟練掌握 if-else 的靈活組合與 switch 的高效分派,並養成「邊界測試、防禦性編程」習慣,是寫出健壯 C 程式的關鍵。建議動手修改上述範例:加入輸入驗證、擴展等級區間、實現「下一個閏年」查詢等,深化理解。