Thuta Learning
C
BasicProgrammingအခြေခံ

Switch

Cသင်ခန်းစာ 1225 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • switch က matching case ကို ခုန်သွားပြီး break အထိ ဆက် run ပုံကို ရှင်းပြနိုင်ဖို့
  • break မပါတဲ့ case ရဲ့ fall-through output ကို ကြိုတွက်နိုင်ဖို့
  • case တွေကို အုပ်စုဖွဲ့ပြီး ရည်ရွယ်ချက်ရှိတဲ့ fall-through ကို comment နဲ့ မှတ်သားနိုင်ဖို့
  • switch ရဲ့ ကန့်သတ်ချက် (integer type၊ constant label၊ range မရ) ကို သိပြီး if...else ကို ဘယ်အချိန် ပြန်သုံးရမလဲ ဆုံးဖြတ်နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

switch ကို ဓာတ်လှေကား ခလုတ်ခုံ တစ်ခုလို မြင်ကြည့်ပါ။ 4 ကို နှိပ်လိုက်တာနဲ့ ဓာတ်လှေကားက ကြားထပ်တွေကို တစ်ထပ်ချင်း မမေးဘဲ 4 ထပ်ကို တန်းသွားပါတယ်။ switch (day) ကလည်း day ရဲ့ value ကို ကြည့်ပြီး ကိုက်ညီတဲ့ case label ကို တိုက်ရိုက် ခုန်သွားပါတယ်။ ဘယ် case နဲ့မှ မကိုက်ရင် default ကို သွားပါတယ်။

Variable တစ်ခုတည်းကို == နဲ့ value အများကြီးနဲ့ နှိုင်းယှဉ်ရမယ့် if / else if chain ရှည်ကြီး — menu option၊ day number၊ status code၊ keyboard command စတာတွေ — အတွက် switch က ပိုတိုပြီး ဖတ်ရ ပိုလွယ်ပါတယ်။ သတိထားရမှာ တစ်ခုကတော့ case တစ်ခု ပြီးတိုင်း break ထည့်ဖို့ပါ။

Problem → Why → Solution

ပြဿနာ: break တစ်ခု လွတ်သွားတော့ ပစ္စည်း ပို့ပြီးသား ဖြစ်သွားတာ

Order system တစ်ခုမှာ status 1 က "ငွေရပြီ"၊ status 2 က "ပို့ပြီ" ဖြစ်ပါတယ်။ Developer က case 1 ရဲ့ အဆုံးမှာ break ထည့်ဖို့ မေ့သွားပါတယ်။ ငွေပေးပြီးခါစ order တိုင်းအတွက် program က Payment received ကို print ထုတ်ပြီးနောက် Package shipped ကိုပါ ဆက် print ထုတ်ပါတယ်။ Customer တွေက ပစ္စည်း မထွက်သေးခင် shipping email ရနေပြီး ဒါကို ဖမ်းမိဖို့ test တစ်ခုမှ fail မဖြစ်ခဲ့ပါဘူး။

order_status.c
int status = 1;                      /* 1 = paid, 2 = shipped */

switch (status) {
  case 1:
    printf("Payment received\n");
    /* break forgotten here */
  case 2:
    printf("Package shipped\n");      /* also runs when status is 1 */
    break;
  default:
    printf("Unknown status\n");
}

case label က ကန့်သတ်ထားတဲ့ block မဟုတ်ဘူး

case label တွေက ဝင်ပေါက် (jump target) တွေသာ ဖြစ်ပြီး case တစ်ခုချင်းကို သီးခြား block အဖြစ် ပိတ်မထားပါဘူး။ switch က ကိုက်ညီတဲ့ label ကို ခုန်ပြီးတာနဲ့ break ဒါမှမဟုတ် switch ရဲ့ အဆုံး } ကို မရောက်မချင်း နောက်က label တွေကို ဖြတ်ပြီး အောက်ကို ဆက် run ပါတယ်။ GCC -Wextra က this statement may fall through လို့ သတိပေးနိုင်ပါတယ်။

Core concept: jump, run, break

switch (expression) က expression ကို တစ်ခါပဲ တွက်ပါတယ်။ Expression ရဲ့ type က int, char, long, enum လို integer type ဖြစ်ရပါမယ် — double ဒါမှမဟုတ် string ကို ပေးရင် GCC က error: switch quantity not an integer လို့ ငြင်းပါတယ်။ ပြီးရင် value နဲ့ တူတဲ့ case label ကို ခုန်သွားပါတယ်။ ဘယ်ဟာနဲ့မှ မတူရင် default ကို ခုန်ပြီး default မရှိရင်တော့ switch ထဲက ဘာမှ မ run ပါဘူး။

case label တိုင်းက integer constant expression ဖြစ်ရပါမယ် — 4 ဒါမှမဟုတ် 'q' လို literal၊ enum constant၊ #define macro တွေပါ။ Variable နဲ့ const int ကို label အဖြစ် သုံးရင် error: case label does not reduce to an integer constant ရပြီး value တူတဲ့ label နှစ်ခု ရှိရင် error: duplicate case value ရပါတယ်။

Label တွေက ဝင်ပေါက် သက်သက်ဖြစ်လို့ run နေတာက break (switch ကနေ ထွက်) ဒါမှမဟုတ် switch ရဲ့ အဆုံးထိ အောက်ကို ဆက်သွားပါတယ် — ဒါကို fall-through လို့ ခေါ်ပါတယ်။ အများအားဖြင့် case တိုင်းကို break နဲ့ အဆုံးသတ်ရပါမယ်။ Fall-through ကို ရည်ရွယ်ချက်ရှိရှိ သုံးတဲ့ နည်း နှစ်မျိုး ရှိပါတယ် — case 6: case 7: လို label တွေကို ဆင့်ထားပြီး အုပ်စုဖွဲ့တာ (warning မပေးပါဘူး) နဲ့ အဆင့်ဆင့် ပေါင်းထည့်ရမယ့် အလုပ်တွေအတွက် /* fall through */ comment နဲ့ မှတ်ထားတာပါ။ GCC က အဲဒီ comment ကို သိလို့ warning မပေးတော့ပါဘူး၊ C23 မှာတော့ [[fallthrough]]; attribute ပါလာပါတယ်။

default က မမျှော်လင့်တဲ့ value တွေကို ဖမ်းဖို့ပါ။ ဘယ်နေရာမှာ ထားထား ရပေမယ့် နောက်ဆုံးမှာ ထားတာ convention ဖြစ်ပြီး နောက်ဆုံး မဟုတ်ရင် break လိုပါတယ်။ switch က တန်းတူညီမျှမှုကိုပဲ စစ်နိုင်လို့ score >= 80 လို range တွေနဲ့ ရှုပ်ထွေးတဲ့ condition တွေအတွက် if...else ကို ဆက်သုံးပါ။

လိုအပ်ချက်သုံးရမှာ
int, char, enum value တစ်ခုကို constant အများကြီးနဲ့ တိုက်စစ်switch
Range ဒါမှမဟုတ် && / || ပါတဲ့ conditionif...else
double ဒါမှမဟုတ် string နဲ့ နှိုင်းယှဉ်if...else
Mental model: ဝင်ပေါက်တွေ ပါတဲ့ စင်္ကြံ
  1. Expression ကို တစ်ခါ တွက်
  2. ကိုက်ညီတဲ့ case တံခါးကို ခုန်ဝင်
  3. break တွေ့တဲ့အထိ အောက်ကို ဆက်လျှောက်
  4. ဘာမှ မကိုက်ရင် default ကို ဝင်

case တွေက တံခါးတွေပဲ ဖြစ်ပြီး နံရံ မဟုတ်ပါဘူး — နံရံကို break နဲ့ ကိုယ်တိုင် ဆောက်ရပါတယ်။

အထွေထွေ ပုံစံ

c
switch (integer_expression) {
  case CONSTANT_A:
    /* statements for A */
    break;
  case CONSTANT_B:
  case CONSTANT_C:              /* B and C share the same code */
    /* statements for B or C */
    break;
  default:
    /* anything else */
    break;
}

Expression က integer type ဖြစ်ရပြီး CONSTANT တွေက literal, enum constant ဒါမှမဟုတ် macro ဖြစ်ရပါမယ်။ Label တွေကို ဆင့်ထားရင် code တစ်ခုတည်းကို မျှသုံးပြီး default ရဲ့ break က မလိုအပ်ပေမယ့် case အသစ် ထပ်ထည့်တဲ့အခါ ဘေးကင်းစေပါတယ်။

Example: day number ကနေ နေ့နာမည်

ဒီ program က day = 4 ကို နေ့နာမည် အဖြစ် ပြောင်းပါတယ်။ case 1 နဲ့ case 4 နှစ်ခုပဲ ရေးထားလို့ ကျန်တဲ့ value တွေ အားလုံး default ကို ရောက်ပါတယ်။ Run ပြီးရင် day ကို 1၊ 2၊ 9 ပြောင်းကြည့်ပါ၊ ပြီးတော့ case 4 ရဲ့ break ကို ဖြုတ်ပြီး output ဘယ်လို ပြောင်းလဲ ကြည့်ပါ။

c
#include <stdio.h>

int main() {
  int day = 4;

  switch (day) {
    case 1:
      printf("Monday");
      break;
    case 4:
      printf("Thursday");
      break;
    default:
      printf("Unknown day");
  }
  return 0;
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Line 6switch (day) {

    day ကို တစ်ခါ ဖတ်ပြီး value 4 နဲ့ ကိုက်တဲ့ label ကို ရှာပါတယ်။ day က int ဖြစ်လို့ switch ရဲ့ integer type လိုအပ်ချက်နဲ့ ကိုက်ပါတယ်။

  2. Lines 7-9case 1:

    day က 1 မဟုတ်လို့ ဒီ label ကို ကျော်ပါတယ်။ switch က label တွေကို if chain လို တစ်ခုချင်း run မကြည့်ဘဲ ကိုက်ညီတဲ့ label ကို တိုက်ရိုက် ခုန်တာပါ။

  3. Lines 10-12break;

    case 4 မှာ ဝင်ပြီး Thursday ကို print ထုတ်ပါတယ်။ break က switch ကနေ ချက်ချင်း ထွက်စေလို့ default ရဲ့ Unknown day ကို မရောက်ပါဘူး။ ဒီ break ကို ဖြုတ်ရင် ThursdayUnknown day လို့ ထွက်ပါလိမ့်မယ်။

  4. Lines 13-14default:

    1 နဲ့ 4 မဟုတ်တဲ့ value တိုင်း ဒီကို ရောက်ပါတယ်။ နောက်ဆုံး label ဖြစ်လို့ break မပါလည်း switch ရဲ့ အဆုံးကို ရောက်ပြီး ထွက်သွားပါတယ်။

You should see
Thursday

Case အုပ်စုဖွဲ့ခြင်းနဲ့ ရည်ရွယ်ချက်ရှိတဲ့ fall-through

ပထမ switch က case 6: နဲ့ case 7: ကို ဆင့်ထားလို့ day 6 ရော 7 ပါ Weekend ကို ရောက်ပါတယ်။ ဒုတိယ switch က enum Plan လို့ နာမည်ပေးထားတဲ့ enum type ကို သုံးပါတယ် (BASIC = 1 ဆိုတော့ STANDARD က 2၊ PREMIUM က 3)။ Plan အမြင့်က အနိမ့်ရဲ့ feature တွေကိုလည်း ရလို့ break မပါဘဲ /* fall through */ နဲ့ မှတ်ထားပါတယ် — STANDARD ကနေ ဝင်ရင် cloud backup နဲ့ email account နှစ်ခုလုံး ထွက်ပါတယ်။

grouped_cases.c
#include <stdio.h>

enum Plan { BASIC = 1, STANDARD, PREMIUM };

int main(void) {
  int day = 6;
  switch (day) {
    case 6:
    case 7:
      printf("Weekend\n");
      break;
    case 1: case 2: case 3: case 4: case 5:
      printf("Weekday\n");
      break;
    default:
      printf("Invalid day %d\n", day);
      break;
  }

  enum Plan plan = STANDARD;
  printf("Plan %d includes:\n", (int)plan);
  switch (plan) {
    case PREMIUM:
      printf("- priority support\n");
      /* fall through */
    case STANDARD:
      printf("- cloud backup\n");
      /* fall through */
    case BASIC:
      printf("- email account\n");
      break;
  }
  return 0;
}

enum နဲ့ switch က အတူတကွ ကောင်းကောင်း အလုပ်လုပ်တယ်

ဒုတိယ switch မှာ default မပါပေမယ့် enum value သုံးခုလုံးကို ကိုင်တွယ်ထားပါတယ်။ enum ထဲကို value အသစ် (ENTERPRISE လို) ထပ်ထည့်ပြီး case မထည့်မိရင် GCC -Wall က enumeration value 'ENTERPRISE' not handled in switch လို့ သတိပေးပါတယ်။ default ထည့်ထားရင် ဒီ warning ပျောက်သွားလို့ enum switch တွေမှာ default ကို ချန်ထားလေ့ ရှိပါတယ်။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Command-line tool တွေက option တွေကို char တစ်လုံးချင်း ဖတ်ပြီး switch (option) { case 'v': ... case 'o': ... } နဲ့ ခွဲပါတယ် — POSIX ရဲ့ getopt နဲ့ ရေးထားတဲ့ program အများစုမှာ ဒီပုံစံကို တွေ့ရပါတယ်။ Embedded firmware နဲ့ network protocol code တွေကတော့ state machine တွေကို switch (state) { case STATE_IDLE: ... case STATE_CONNECTING: ... } လို enum switch နဲ့ ရေးပြီး case တစ်ခုချင်းမှာ နောက် state ကို ဆုံးဖြတ်ပါတယ်။

Emulator နဲ့ interpreter တွေကလည်း instruction တစ်ခုချင်းရဲ့ opcode ပေါ်မူတည်ပြီး switch (opcode) နဲ့ အလုပ်ခွဲပါတယ်။ Case တွေ ကပ်လျက် ဖြစ်နေရင် compiler က switch ကို jump table အဖြစ် ပြောင်းပေးနိုင်လို့ case 3 ခု ဖြစ်ဖြစ် 300 ဖြစ်ဖြစ် မြန်နှုန်း သိပ်မကွာပါဘူး။ ဒီလို project တွေက break မေ့တာကို ဖမ်းဖို့ -Wimplicit-fallthrough (-Wextra ထဲမှာ ပါ) ကို ဖွင့်ထားပြီး ရည်ရွယ်ချက်ရှိတဲ့ fall-through တိုင်းကို comment ဒါမှမဟုတ် attribute နဲ့ မှတ်ပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. String ဒါမှမဟုတ် double ကို switch လုပ်တာ

switch (command) မှာ command က "add" လို string ဖြစ်နေရင် GCC က switch quantity not an integer နဲ့ pointers are not permitted as case values လို့ ငြင်းပါတယ်။ Command ကို 'a' လို char တစ်လုံး ဖြစ်အောင် ပြောင်းပါ၊ ဒါမှမဟုတ် if...else နဲ့ strcmp ကို သုံးပါ (string lesson မှာ လေ့လာပါမယ်)။

2. case ထဲမှာ range ရေးဖို့ ကြိုးစားတာ

case score >= 80: က error: case label does not reduce to an integer constant ဖြစ်ပါတယ်၊ label က variable ပါတဲ့ expression ဖြစ်နေလို့ပါ။ GCC က case 80 ... 100: ကို extension အဖြစ် လက်ခံပေမယ့် -pedantic နဲ့ဆိုရင် ISO C does not support range expressions in switch statements before C2Y လို့ ပြောပါတယ်။ Portable code မှာ range တွေကို if...else နဲ့ ရေးပါ။

3. case label ရဲ့ နောက်မှာ variable တိုက်ရိုက် ကြေညာတာ

C17 မှာ case 1: int total = 10; လို label ရဲ့ နောက်မှာ declaration တိုက်ရိုက် ရေးခွင့် မရှိပါဘူး (C23 ကျမှ ခွင့်ပြုပါတယ်)။ Recent GCC က -pedantic မပါရင် တိတ်တဆိတ် လက်ခံပေမယ့် compiler အဟောင်းတွေက a label can only be part of a statement and a declaration is not a statement လို့ ငြင်းပါတယ်။ case body ကို { } နဲ့ ပတ်ပါ၊ variable ရဲ့ scope ကိုလည်း အဲဒီ case ထဲမှာပဲ ကန့်သတ်ပေးပါတယ်။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • case အသစ် ရေးတိုင်း code မရေးခင် break ကို အရင် ရေးထားပါ။
  • ရည်ရွယ်ချက်ရှိတဲ့ fall-through တိုင်းကို /* fall through */ comment နဲ့ မှတ်ပြီး -Wextra ကို ဖွင့်ထားပါ။
  • int switch တွေမှာ မမျှော်လင့်တဲ့ value ကို report လုပ်တဲ့ default ထည့်ပါ၊ enum switch တွေမှာတော့ -Wswitch warning ကို အသုံးချဖို့ default ကို ချန်ထားနိုင်ပါတယ်။
  • case label တွေမှာ magic number မသုံးဘဲ enum constant တွေကို သုံးပါ။

Mini challenge

Mini challenge: လတစ်လရဲ့ ရက်အရေအတွက်

လ နံပါတ် (1-12) နဲ့ leap year ဟုတ်မဟုတ် flag ကို သုံးပြီး အဲဒီလမှာ ရက် ဘယ်နှစ်ရက် ရှိလဲ switch နဲ့ တွက်ပါ။ မမှန်ကန်တဲ့ လ နံပါတ်ကိုလည်း ကိုင်တွယ်ပါ။

  • 30 ရက်ရှိတဲ့ လတွေ (4, 6, 9, 11) ကို case label ဆင့်ပြီး အုပ်စုဖွဲ့ရမယ်
  • 31 ရက်ရှိတဲ့ လတွေကိုလည်း အုပ်စုတစ်ခုတည်းအဖြစ် ရေးရမယ်
  • ဖေဖော်ဝါရီ (2) က is_leap_year ပေါ်မူတည်ပြီး 29 ဒါမှမဟုတ် 28 ဖြစ်ရမယ်
  • default က invalid month ကို မှတ်ပြီး case တိုင်းမှာ break ပါရမယ်
month_days.c
#include <stdio.h>
#include <stdbool.h>

int main(void) {
  int month = 2;
  bool is_leap_year = true;
  int days;

  switch (month) {
    /* TODO: 30-day months, stacked labels */
    /* TODO: February, using is_leap_year */
    /* TODO: 31-day months, stacked labels */
    /* TODO: default for invalid months (set days to 0) */
  }

  /* TODO: print "Invalid month: N" or "Month N has D days" */
  return 0;
}

ရလဒ်: month = 2 နဲ့ leap year အတွက် Month 2 has 29 days ထွက်ရပါမယ်။ month ကို 4, 12, 13 ပြောင်းရင် Month 4 has 30 days, Month 12 has 31 days, Invalid month: 13 ရပါမယ်။

အရိပ်အမြွက် ကြည့်ရန်

case 4: case 6: case 9: case 11: လို label တွေကို တစ်ကြောင်းတည်းမှာ ဆင့်ရေးလို့ ရပါတယ်။ ဖေဖော်ဝါရီအတွက် is_leap_year ? 29 : 28 ကို သုံးပါ။

အဖြေ ကြည့်ရန်
month_days.c
#include <stdio.h>
#include <stdbool.h>

int main(void) {
  int month = 2;
  bool is_leap_year = true;
  int days;

  switch (month) {
    case 4: case 6: case 9: case 11:
      days = 30;
      break;
    case 2:
      days = is_leap_year ? 29 : 28;
      break;
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
      days = 31;
      break;
    default:
      days = 0;
      break;
  }

  if (days == 0) {
    printf("Invalid month: %d\n", month);
  } else {
    printf("Month %d has %d days\n", month, days);
  }
  return 0;
}

Quick Quiz 1 / 3

int n = 2; switch (n) { case 1: printf("A"); case 2: printf("B"); case 3: printf("C"); break; default: printf("D"); } က ဘာ print ထုတ်မလဲ။

Quick Quiz 2 / 3

switch ရဲ့ controlling expression အဖြစ် ဘယ်ဟာကို C17 က လက်ခံလဲ။

Quick Quiz 3 / 3

ဘယ် case နဲ့မှ မကိုက်ပြီး default လည်း မရှိရင် ဘာ ဖြစ်မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • switch က integer expression ကို တစ်ခါ တွက်ပြီး ကိုက်ညီတဲ့ case label ကို ခုန်ကာ ဘာမှ မကိုက်ရင် default ကို သွားပါတယ်။
  • case label တွေက integer constant expression (literal, enum constant, macro) ဖြစ်ရပြီး variable, range, string မရပါဘူး။
  • break မပါရင် နောက် case ထဲကို fall through ဖြစ်လို့ case တိုင်းကို break နဲ့ အဆုံးသတ်ပါ။
  • Label ဆင့်ပြီး case တွေကို အုပ်စုဖွဲ့ပါ၊ ရည်ရွယ်ချက်ရှိတဲ့ fall-through ကို /* fall through */ နဲ့ မှတ်ပါ။

GNU C compiler diagnostics — GNU Project

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Switch | Thuta Learning