Thuta Learning
C
BasicProgrammingအခြေခံ

If...Else

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

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

  • if / else if / else chain တစ်ခုမှာ ဘယ် branch run မလဲ ကြိုတွက်နိုင်ဖို့
  • Condition တွေကို အတင်းကျပ်ဆုံးကနေ စီပြီး boundary value (80 လို) တွေကို မှန်မှန် ကိုင်တွယ်နိုင်ဖို့
  • Braces အမြဲ သုံးရတဲ့ အကြောင်းရင်းနဲ့ dangling else ကို ရှင်းပြနိုင်ဖို့
  • Guard clause နဲ့ value နှစ်ခုထဲက တစ်ခု ရွေးတဲ့ ?: operator ကို သုံးနိုင်ဖို့

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

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

if...else ဆိုတာ program ထဲက လမ်းဆုံ တစ်ခုပါ။ "Password မှန်ရင် dashboard ကို ပြ၊ မမှန်ရင် error ပြ"၊ "Score 80 နဲ့ အထက်ဆို A၊ 60 နဲ့ အထက်ဆို B၊ ကျန်တာ ထပ်ကြိုးစား" လို ဆုံးဖြတ်ချက်တွေကို code နဲ့ ရေးတာပါ။ Condition က true ဖြစ်ရင် if block ကို run ပြီး false ဖြစ်ရင် else block ကို run ပါတယ်။

လမ်းကြောင်း နှစ်ခုထက် များရင် else if တွေကို ဆက်ပြီး chain လုပ်ပါတယ်။ Program က condition တွေကို အပေါ်ကနေ အောက်ကို တစ်ခုချင်း စစ်ပြီး ပထမဆုံး true ဖြစ်တဲ့ block တစ်ခုတည်းကိုပဲ run ပါတယ်။

Problem → Why → Solution

ပြဿနာ: indentation က လိမ်ညာတဲ့အခါ

Online shop တစ်ခုမှာ stock ရှိမှ "In stock" ပြဖို့ braces မပါဘဲ if တစ်ကြောင်း ရေးထားပါတယ်။ နောက်တစ်လ ကြာတော့ team member တစ်ယောက်က "Added to cart" message ကို အဲဒီ if ရဲ့ အောက်မှာ indentation တူတူနဲ့ ထပ်ထည့်လိုက်ပါတယ်။ Code ကို မြင်ရတာ ဒုတိယ line ကလည်း if ထဲမှာ ရှိနေသလိုပါပဲ၊ ဒါပေမယ့် stock သုည ဖြစ်နေတဲ့အခါမှာတောင် ဖောက်သည်တွေက Added to cart ကို မြင်နေရပါတယ်။

cart.c
int stock = 0;

if (stock > 0)
    printf("In stock\n");
    printf("Added to cart\n");   /* looks guarded, but always runs */

Compiler က indentation ကို မကြည့်ပါဘူး

Braces မပါရင် if က သူ့နောက်က statement တစ်ခုတည်းကိုပဲ ထိန်းပါတယ်။ C compiler အတွက် space နဲ့ tab တွေက အဓိပ္ပာယ် မရှိလို့ ဒုတိယ printf က if ပြီးနောက် ရိုးရိုး statement တစ်ခုပဲ ဖြစ်ပါတယ်။ GCC -Wall က this 'if' clause does not guard... လို့ သတိပေးနိုင်ပေမယ့် အကောင်းဆုံး ကာကွယ်နည်းကတော့ branch တိုင်းမှာ { } ကို အမြဲ ရေးတာပါ။

Core concept: branch တွေ ဘယ်လို ရွေးချယ်ခံရလဲ

if (condition) { ... } မှာ condition က ဘယ် scalar expression မဆို ဖြစ်နိုင်ပြီး zero မဟုတ်ရင် block ကို run ပါတယ်။ else block ကတော့ condition false ဖြစ်မှ run ပါတယ်။ if / else if / else chain မှာ condition တွေကို အပေါ်ကနေ အောက်ကို စစ်ပြီး ပထမဆုံး true ဖြစ်တဲ့ branch ကို run ပြီးတာနဲ့ ကျန်တဲ့ branch တွေကို လုံးဝ ကျော်သွားပါတယ်။ ဒါကြောင့် အစဉ်က အရေးကြီးပါတယ် — score >= 60 ကို score >= 80 ရဲ့ အပေါ်မှာ ထားရင် 95 မှတ် ရတဲ့သူလည်း B ရသွားပါမယ်၊ ပထမ စစ်တဲ့ condition က သူ့ကို အရင် ဖမ်းလိုက်လို့ပါ။

Braces မပါရင် if နဲ့ else က statement တစ်ခုတည်းကိုပဲ ထိန်းပါတယ်။ if တွေကို braces မပါဘဲ အထပ်ထပ် ထည့်ရင် dangling else ပြဿနာ ပေါ်ပါတယ် — else က indentation ဘယ်လိုပဲ ရှိရှိ အနီးဆုံး else မရှိသေးတဲ့ if နဲ့ပဲ တွဲပါတယ်။ GCC က suggest explicit braces to avoid ambiguous 'else' လို့ သတိပေးပါတယ်။ Branch တစ်ခုမှာ line တစ်ကြောင်းပဲ ရှိရင်တောင် braces ကို အမြဲ ရေးတာက ဒီ bug အမျိုးအစား နှစ်ခုလုံးကို ဖယ်ရှားပေးပါတယ်။

Boundary value တွေကို သတိထားပါ။ >= နဲ့ > ကွာခြားချက်က score 80 အတိအကျ ရတဲ့သူ A ရမလား B ရမလား ဆုံးဖြတ်ပါတယ်။ Range တစ်ခုကို စစ်ဖို့ score >= 60 && score < 80 လို && နဲ့ ပေါင်းရေးပြီး range တွေကြားမှာ ကွက်လပ် ဒါမှမဟုတ် ထပ်နေတာ မရှိအောင် စစ်ပါ။ မမှန်ကန်တဲ့ input (အနုတ် score, 100 ကျော်တာ) ကို chain ရဲ့ အစမှာ guard clause အဖြစ် အရင် ဖယ်ထုတ်လိုက်ရင် ကျန်တဲ့ branch တွေက valid data ကိုပဲ စဉ်းစားရပါတော့တယ်။

Value နှစ်ခုထဲက တစ်ခုကို ရွေးရုံပဲ ဆိုရင် conditional operator (?:) က if...else ထက် တိုပါတယ် — int bonus = (attempts == 1) ? 5 : 0; ဆိုတာ "ပထမအကြိမ်နဲ့ အောင်ရင် 5၊ မဟုတ်ရင် 0" ပါ။ သူက statement မဟုတ်ဘဲ value ပြန်ပေးတဲ့ expression ဖြစ်လို့ assignment နဲ့ printf argument တွေထဲမှာ သုံးလို့ ရပါတယ်။ ?: တွေကို အထပ်ထပ် nest လုပ်ရင် ဖတ်ရခက်လို့ အဲဒီအခါမှာ if...else ကို ပြန်သုံးပါ။

Mental model: အပေါ်ကနေ အောက်ကို စစ်ပြီး ပထမ true မှာ ရပ်
  1. ပထမ condition ကို စစ်
  2. False ဆို နောက် else if ကို ဆက်စစ်
  3. ပထမ true branch ကိုပဲ run
  4. ကျန်တာ ကျော်၊ ဘာမှ မမှန်ရင် else

Chain တစ်ခုမှာ branch တစ်ခုထက် ပိုပြီး ဘယ်တော့မှ မ run ပါဘူး — ဒါကြောင့် ပိုတင်းကျပ်တဲ့ condition ကို အပေါ်မှာ ထားပါ။

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

c
if (first_condition) {
  /* runs when first_condition is true */
} else if (second_condition) {
  /* runs when only second_condition is true */
} else {
  /* runs when nothing above was true */
}

result = condition ? value_if_true : value_if_false;

else if တွေကို လိုသလောက် ထည့်နိုင်ပြီး else ကတော့ optional ဖြစ်ကာ chain ရဲ့ အဆုံးမှာ တစ်ခုပဲ ရှိနိုင်ပါတယ်။ နောက်ဆုံး line က value နှစ်ခုထဲက တစ်ခုကို ရွေးတဲ့ conditional operator ပါ။

Example: score ကနေ grade ရွေးခြင်း

ဒီ program က score 72 ကို grade တစ်ခုအဖြစ် ခွဲခြားပါတယ်။ Run မလုပ်ခင် condition တစ်ခုချင်းကို 72 နဲ့ အစားထိုးပြီး ဘယ် branch ကို ရောက်မလဲ ခန့်မှန်းကြည့်ပါ။ ပြီးရင် score ကို 80၊ 60၊ 59 လို boundary value တွေနဲ့ ပြောင်းပြီး ထပ် run ကြည့်ပါ။

c
#include <stdio.h>

int main() {
  int score = 72;

  if (score >= 80) {
    printf("Grade A");
  } else if (score >= 60) {
    printf("Grade B");
  } else {
    printf("Try again");
  }
  return 0;
}

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

  1. Line 4int score = 72;

    စစ်မယ့် value ပါ။ Chain ကို ပြောင်းစရာ မလိုဘဲ ဒီ line ကိုပဲ ပြောင်းပြီး branch တိုင်းကို စမ်းနိုင်ပါတယ်။

  2. Line 6if (score >= 80) {

    အတင်းကျပ်ဆုံး condition ကို အရင်စစ်ပါတယ်။ 72 >= 80 က false ဖြစ်လို့ Grade A block ကို ကျော်ပြီး နောက် condition ကို ဆက်သွားပါတယ်။ 80 အတိအကျ ဆိုရင် >= ကြောင့် ဒီမှာ A ရပါတယ်။

  3. Line 8} else if (score >= 60) {

    ဒီ condition ကို ရောက်လာတယ်ဆိုတာ score < 80 ဖြစ်ပြီးသားလို့ ဆိုလိုပါတယ်။ ဒါကြောင့် score >= 60 && score < 80 လို့ ထပ်ရေးစရာ မလိုပါဘူး။ 72 >= 60 က true ဖြစ်လို့ Grade B ကို print ထုတ်ပါတယ်။

  4. Lines 10-12} else {

    အပေါ်က condition တွေ တစ်ခုမှ မမှန်မှ else block run ပါတယ်။ Grade B branch run ပြီးသွားလို့ ဒီ block ကို ကျော်ပြီး return 0; ကို တန်းရောက်ပါတယ်။

You should see
Grade B
score = 72 ရဲ့ လမ်းကြောင်း
  1. score >= 80 ကို စစ် (false)
  2. score >= 60 ကို စစ် (true)
  3. Grade B ကို print ထုတ်
  4. else ကို ကျော်ပြီး chain ကနေ ထွက်

score >= 80

Grade A

60 <= score < 80

Grade B

ကျန်တာ အားလုံး

Try again

Guard clause နဲ့ conditional operator

အောက်က version မှာ chain ရဲ့ ထိပ်ဆုံးမှာ invalid score ကို ဖယ်ထုတ်တဲ့ guard ပါပါတယ်။ score 105 က 100 ထက် ကြီးလို့ Invalid score ကို print ထုတ်ပြီး grade branch တွေကို လုံးဝ မရောက်ပါဘူး — guard မပါရင် 105 က Grade A ရသွားမှာပါ။ နောက်ဆုံး line တွေက bonus ကို ?: နဲ့ ရွေးပါတယ်၊ attempts က 2 ဖြစ်လို့ 0 ရပါတယ်။

grade_guard.c
#include <stdio.h>

int main(void) {
  int score = 105;
  int attempts = 2;

  if (score < 0 || score > 100) {
    printf("Invalid score: %d\n", score);
  } else if (score >= 80) {
    printf("Grade A\n");
  } else if (score >= 60) {
    printf("Grade B\n");
  } else {
    printf("Try again\n");
  }

  int bonus = (attempts == 1) ? 5 : 0;
  printf("Bonus points: %d\n", bonus);
  return 0;
}

Dangling else ကို ကိုယ်တိုင် စမ်းကြည့်ပါ

a = 1, b = 0 နဲ့ if (a) if (b) printf("X"); else printf("Y"); ကို braces မပါဘဲ ရေးပြီး run ကြည့်ပါ။ else က အတွင်း if (b) နဲ့ တွဲလို့ Y ထွက်ပါတယ်။ အပြင် if နဲ့ တွဲစေချင်ရင် if (a) { if (b) { ... } } else { ... } လို့ braces နဲ့ ရေးပါ။

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

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

Real C code မှာ အတွေ့ရအများဆုံး if က error စစ်တာပါ — file ဖွင့်တာ၊ memory ယူတာ၊ network connect လုပ်တာ မအောင်မြင်ရင် if နဲ့ စစ်ပြီး error message ပြကာ function ကနေ စောစော ထွက်ပါတယ်။ ဒီ guard-clause ပုံစံကြောင့် အဓိက logic က indentation အဆင့်ဆင့် မဝင်ဘဲ ညီညီညာညာ ရှိနေပါတယ်။ Validation code တွေမှာလည်း မမှန်တဲ့ input ကို အရင် ဖယ်ပြီးမှ ကျန်တာကို ဆက်လုပ်ပါတယ်။

Braces မပါတဲ့ if ရဲ့ အန္တရာယ်ကို အကြီးအကျယ် ပြခဲ့တဲ့ ဥပမာ တစ်ခုက 2014 က Apple ရဲ့ "goto fail" bug ပါ။ SSL certificate စစ်တဲ့ code ထဲမှာ braces မပါတဲ့ if တစ်ခုရဲ့ အောက်မှာ goto fail; line က နှစ်ခါ ပါသွားခဲ့ပြီး ဒုတိယ line က အမြဲ run ကာ signature စစ်ဆေးမှုကို ကျော်သွားခဲ့ပါတယ်။ ဒီလို ဖြစ်ရပ်တွေကြောင့် MISRA C နဲ့ CERT C လို coding standard တွေက if body တိုင်းမှာ braces ရေးဖို့ သတ်မှတ်ထားပြီး CI pipeline အတော်များများက -Wall -Werror နဲ့ build လုပ်ကြပါတယ်။

အမှားများ

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

1. if (...) ရဲ့ နောက်မှာ semicolon ထည့်မိတာ

if (score > 90); { printf("Excellent\n"); } မှာ ; က empty statement ဖြစ်ပြီး if က အဲဒါကိုပဲ ထိန်းပါတယ်။ နောက်က block က score ဘယ်လောက်ပဲ ဖြစ်ဖြစ် အမြဲ run ပါတယ်။ GCC -Wextra က suggest braces around empty body in an 'if' statement လို့ သတိပေးပါတယ်။

2. Condition အစဉ် မှားတာ

if (score >= 60) ကို if (score >= 80) ရဲ့ အပေါ်မှာ ထားရင် 95 ရတဲ့ student က Grade B ရပါတယ်၊ ပထမ condition က true ဖြစ်ပြီး chain က ရပ်သွားလို့ပါ။ Compiler က ဒါကို မဖမ်းနိုင်ပါဘူး၊ ဒါကြောင့် အတင်းကျပ်ဆုံး condition ကို အပေါ်ဆုံးမှာ ထားပါ။

3. Boundary မှာ ကွက်လပ် ကျန်တာ

if (score > 80) { A } else if (score < 80) { B } ဆိုရင် 80 အတိအကျက ဘယ် branch နဲ့မှ မကိုက်ဘဲ else ဒါမှမဟုတ် ဘာမှ မလုပ်တဲ့ဘက်ကို ရောက်သွားပါတယ်။ Boundary value (80, 60, 0, 100) တွေနဲ့ အမြဲ စမ်းပြီး >= / < ကို တစ်သမတ်တည်း သုံးပါ။

Best Practices

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

  • Branch တိုင်းမှာ line တစ်ကြောင်းပဲ ရှိရင်တောင် { } ကို ရေးပါ။
  • Invalid input နဲ့ error case တွေကို chain ရဲ့ အစမှာ guard clause အဖြစ် အရင် ကိုင်တွယ်ပါ။
  • ရှုပ်ထွေးတဲ့ condition ကို bool isEligible = ...; လို နာမည်ပေးထားတဲ့ variable ထဲ အရင် သိမ်းပါ။
  • Branch တိုင်းနဲ့ boundary တိုင်းကို အနည်းဆုံး တစ်ခါ run ကြည့်ပြီးမှ ယုံပါ။

Mini challenge

Mini challenge: ပို့ဆောင်ခ တွက်စက်

Package အလေးချိန်အလိုက် shipping fee ကို ရွေးပြီး order total $50 နဲ့ အထက် ဒါမှမဟုတ် member ဖြစ်ရင် အခမဲ့ ပို့ပေးတဲ့ program ရေးပါ။ မမှန်ကန်တဲ့ အလေးချိန်ကို guard clause နဲ့ အရင် ဖယ်ပါ။

  • weight_kg <= 0 ဆိုရင် error message ပြပြီး return 1; နဲ့ ထွက်ရမယ်
  • 1.0 kg ထိ $3.50၊ 5.0 kg ထိ $7.00၊ အဲဒီထက် ပိုရင် $12.00 (boundary တွေမှာ <= သုံး)
  • order_total >= 50.0 ဒါမှမဟုတ် is_member ဆိုရင် free shipping ဖြစ်ရမယ်
  • Branch တိုင်းမှာ braces ပါရမယ်၊ ပေးရမယ့် ငွေကို ?: နဲ့ ရွေးရမယ်
shipping.c
#include <stdio.h>
#include <stdbool.h>

int main(void) {
  double weight_kg = 3.2;
  double order_total = 64.0;
  bool is_member = false;
  double fee;

  /* TODO: guard clause for weight_kg <= 0.0 (print, return 1) */

  /* TODO: if / else if / else chain that sets fee by weight */

  /* TODO: bool free_shipping = ... */
  /* TODO: print the base fee, "Free shipping: yes/no" and the amount to pay */
  return 0;
}

ရလဒ်: 3.2 kg နဲ့ $64 order အတွက်: Base fee for 3.2 kg: $7.00 Free shipping: yes To pay: $0.00 weight_kg ကို 1.0, 5.0, 5.1 နဲ့ -1 ပြောင်းပြီး boundary တွေကိုလည်း စမ်းပါ။

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

Guard clause ပြီးသွားရင် ကျန်တဲ့ code မှာ weight_kg က အမြဲ 0 ထက် ကြီးတယ်လို့ ယူဆလို့ ရပါတယ်။ ပေးရမယ့် ငွေအတွက် free_shipping ? 0.0 : fee ကို printf ထဲမှာ တိုက်ရိုက် သုံးနိုင်ပါတယ်။

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

int main(void) {
  double weight_kg = 3.2;
  double order_total = 64.0;
  bool is_member = false;
  double fee;

  if (weight_kg <= 0.0) {
    printf("Invalid weight: %.1f kg\n", weight_kg);
    return 1;
  }

  if (weight_kg <= 1.0) {
    fee = 3.50;
  } else if (weight_kg <= 5.0) {
    fee = 7.00;
  } else {
    fee = 12.00;
  }

  bool free_shipping = order_total >= 50.0 || is_member;
  printf("Base fee for %.1f kg: $%.2f\n", weight_kg, fee);
  printf("Free shipping: %s\n", free_shipping ? "yes" : "no");
  printf("To pay: $%.2f\n", free_shipping ? 0.0 : fee);
  return 0;
}

Quick Quiz 1 / 3

Example ရဲ့ chain မှာ score = 80 ဆိုရင် ဘာ print ထုတ်မလဲ။

Quick Quiz 2 / 3

int x = 0; if (x > 5); { printf("big\n"); } က ဘာ ဖြစ်မလဲ။

Quick Quiz 3 / 3

int a = 1, b = 0; if (a) if (b) printf("X"); else printf("Y"); (braces မပါ) က ဘာ print ထုတ်မလဲ။

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

  • if / else if / else chain က condition တွေကို အပေါ်ကနေ အောက်ကို စစ်ပြီး ပထမ true branch တစ်ခုတည်းကို run ပါတယ်။
  • အတင်းကျပ်ဆုံး condition ကို အပေါ်မှာ ထားပြီး boundary value တွေ (>= နဲ့ >) ကို သေချာ စမ်းပါ။
  • Braces မပါရင် if က statement တစ်ခုတည်းကိုပဲ ထိန်းပြီး else က အနီးဆုံး if နဲ့ တွဲလို့ branch တိုင်းမှာ { } ရေးပါ။
  • Invalid input ကို guard clause နဲ့ အရင် ဖယ်ပြီး value နှစ်ခုထဲက တစ်ခု ရွေးဖို့ ?: ကို သုံးပါ။

GNU C compiler diagnostics — GNU Project

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

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

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

If...Else | Thuta Learning