Thuta Learning
ရှာဖွေရန်
BasicProgrammingbeginner

If...Else

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

if...else ကို condition တစ်ခုအပေါ်မူတည်ပြီး code path မတူအောင် ရွေးချယ် run လုပ်ဖို့ သုံးပါတယ်။ Login success/fail, mark pass/fail, stock ရှိ/မရှိ စတဲ့ decision တွေမှာ မရှိမဖြစ်လိုပါတယ်။

cpp
#include <iostream>
using namespace std;

int main() {
    int mark = 75;

    if (mark >= 80) {
        cout << "Excellent";
    } else if (mark >= 40) {
        cout << "Pass";
    } else {
        cout << "Fail";
    }
    return 0;
}

Program က condition တွေကို အပေါ်ကနေ အောက်ကို စစ်ပါတယ်။ mark >= 80 မမှန်လို့ နောက် condition ကိုသွားပြီး mark >= 40 မှန်တာကြောင့် Pass ထုတ်ပါတယ်။

You should see
Pass

Info

Condition order က အရေးကြီးပါတယ်။ အကြီးဆုံး range ကို အပေါ်မှာစစ်ပြီး တဖြည်းဖြည်း အောက်ကို ချတာက အများအားဖြင့် ရှင်းပါတယ်။

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

  • else if များစွာရေးတဲ့အခါ overlapping condition ဖြစ်နေလား စစ်ပါ။ ဥပမာ mark >= 40 ကို အပေါ်မှာရေးပြီး mark >= 80 ကို အောက်မှာရေးရင် Excellent ကို ဘယ်တော့မှ မရနိုင်တော့ပါ။
If...Else | Thuta Learning