Thuta Learning
C
BasicProgrammingbeginner

If...Else

if...else ကို condition အပေါ်မူတည်ပြီး မတူညီတဲ့ code block တွေ run ဖို့သုံးပါတယ်။ Login ဝင်ခွင့်စစ်တာ၊ score အလိုက် grade သတ်မှတ်တာ၊ stock ရှိမရှိစစ်တာတွေအားလုံး ဒီ logic ပုံစံနဲ့တူပါတယ်။

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;
}

Program က condition တွေကို အပေါ်ကနေအောက်ကို စစ်ပါတယ်။ score >= 80 မမှန်လို့ နောက် condition ကိုဆက်စစ်ပြီး score >= 60 မှန်တဲ့အတွက် Grade B ကိုထုတ်ပါတယ်။

You should see
Grade B

Info

ပိုတင်းကျပ်တဲ့ condition ကို အပေါ်မှာထားပါ။ Grade logic မှာ score >= 60 ကိုအရင်စစ်ပြီးရင် 80 ကျော်သူတွေလည်း B ထဲဝင်သွားနိုင်ပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • else if အစီအစဉ်မှားတာက logic bug ဖြစ်စေပါတယ်။ Compiler error မပြဘဲ result မှားနိုင်တာကြောင့် သတိထားပါ။
If...Else | Thuta Learning