選擇結構是程序決策的核心。本文系統講解 if 系列(單分支、雙分支、多重分支)、switch-case 多分支的語法、適用場景、典型案例(閏年判斷、元音識別、四則運算、分段函數、稅收計算、銀行利息、員工評分)與最佳實踐。
if (條件) { 語句; }
if (條件) { 語句1; } else { 語句2; }
if (條件1) { ... }
else if (條件2) { ... }
else if (條件3) { ... }
else { 默認情況; }
適用:區間判斷、復雜邏輯表達式、浮點數比較。
switch (整型表達式) {
case 常量1: 語句1; break;
case 常量2: 語句2; break;
...
default: 默認語句;
}
特點:
int/char/enum),不支持浮點/字符串。case 標簽必須為**編譯期常量**。break 會繼續執行下一 case(可利用合併情況)。int year, month;
scanf("%d%d", &year, &month);
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
printf("31天\n"); break;
case 4: case 6: case 9: case 11:
printf("30天\n"); break;
case 2:
if ((year%4==0 && year%100!=0) || year%400==0)
printf("閏年29天\n");
else
printf("平年28天\n");
break;
default:
printf("月份錯誤\n");
}
char c; scanf(" %c", &c);
switch (c) {
case 'A': case 'E': case 'I': case 'O': case 'U':
case 'a': case 'e': case 'i': case 'o': case 'u':
printf("元音\n"); break;
default: printf("非元音\n");
}
int a, b; char op;
scanf("%d %c %d", &a, &op, &b);
switch (op) {
case '+': printf("%d\n", a+b); break;
case '-': printf("%d\n", a-b); break;
case '*': printf("%d\n", a*b); break;
case '/':
if (b != 0) printf("%d\n", a/b);
else printf("除數不能為0\n");
break;
default: printf("運算符錯誤\n");
}
float x, y;
scanf("%f", &x);
if (x < 1) y = x;
else if (x < 10) y = 2*x - 1;
else y = 3*x - 11;
printf("y = %.3f\n", y);
float salary, taxable, tax;
scanf("%f", &salary);
taxable = salary - 300 - 3500; // 扣除費用與起征點
if (taxable > 0) {
if (taxable <= 1500) tax = taxable * 0.03;
else if (taxable <= 4500) tax = taxable * 0.10 - 105;
else if (taxable <= 9000) tax = taxable * 0.20 - 555;
else if (taxable <= 35000) tax = taxable * 0.25 - 1005;
else if (taxable <= 55000) tax = taxable * 0.30 - 2755;
else if (taxable <= 80000) tax = taxable * 0.35 - 5505;
else tax = taxable * 0.45 - 13505;
printf("應納稅: %.2f\n", tax);
} else printf("免稅\n");
float profit, bonus;
scanf("%f", &profit);
if (profit <= 10) bonus = profit * 0.10;
else if (profit <= 20) bonus = 10*0.10 + (profit-10)*0.075;
else if (profit <= 40) bonus = 10*0.10 + 10*0.075 + (profit-20)*0.05;
else if (profit <= 60) bonus = 10*0.10 + 10*0.075 + 20*0.05 + (profit-40)*0.03;
else if (profit <= 100) bonus = 10*0.10 + 10*0.075 + 20*0.05 + 20*0.03 + (profit-60)*0.015;
else bonus = 10*0.10 + 10*0.075 + 20*0.05 + 20*0.03 + 40*0.015 + (profit-100)*0.01;
printf("提成: %.4f 萬\n", bonus);
double money; char type; int term;
scanf("%lf %c", &money, &type);
if (type == 'h' || type == 'H') {
printf("活期利息: %.2f\n", money * 0.0035);
} else if (type == 'd' || type == 'D') {
scanf("%d", &term);
switch (term) {
case 1: printf("整存整取: %.2f\n", money * 0.0325); break;
case 2: printf("零存整取: %.2f\n", money * 0.0285); break;
case 3: printf("定活兩便: %.2f\n", money * 0.0325 * 0.6); break;
default: printf("定期類型錯誤\n");
}
} else printf("儲蓄類型錯誤\n");
float years; int edu, qual, score = 0;
scanf("%f %d %d", &years, &edu, &qual);
// 工齡
if (years == 0) score += 0;
else if (years < 1) score += 1;
else if (years < 3) score += 2;
else if (years < 7) score += 3;
else score += 4;
// 學歷
switch (edu) { case 1: score+=1; break; case 2: score+=2; break; case 3: score+=3; break; }
// 資格
switch (qual) { case 1: score+=0; break; case 2: score+=1; break; case 3: score+=2; break; case 4: score+=3; break; }
printf("總分: %d\n", score);
| 場景 | 推薦 | 理由 |
|---|---|---|
| 區間/範圍判斷 | if-else | 條件為關係表達式 |
| 離散值精確匹配 | switch | 跳轉表優化、代碼清晰、貫穿特性 |
| 字符串比較 | if-else + strcmp | switch 不支持字符串 |
| 浮點數判斷 | if-else | switch 僅整型 |
| 複雜邏輯組合 | if-else | 支持 && || ! 任意組合 |
fabs(a-b) < EPS。<= vs <,邊界值測試覆蓋。scanf(" %c", &c) 前加空格跳過空白。熟練掌握兩大選擇結構的語法細節、適用邊界與組合技巧,是編寫清晰、高效、無 Bug 決策邏輯的關鍵基礎。