Thuta Learning
Java
BasicProgrammingအခြေခံ

If...Else

Javaသင်ခန်းစာ 925 မိနစ်

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

  • if / else if / else chain မှာ ပထမဆုံး true ဖြစ်တဲ့ branch တစ်ခုတည်းပဲ run တယ်ဆိုတာ ခန့်မှန်းနိုင်ဖို့
  • ထပ်နေတဲ့ (overlapping) range condition တွေကို တင်းကျပ်တာကနေ စပြီး မှန်ကန်တဲ့ အစဉ်နဲ့ စီနိုင်ဖို့
  • Java condition တွေက boolean ပဲ ဖြစ်ရတယ်ဆိုတာနဲ့ brace မပါရင် ဖြစ်တတ်တဲ့ bug ကို ရှင်းပြနိုင်ဖို့
  • Value နှစ်ခုထဲက တစ်ခု ရွေးဖို့ ? : နဲ့ type စစ်ဖို့ instanceof pattern ကို သုံးနိုင်ဖို့

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

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

လမ်းဆုံတစ်ခုမှာ ရပ်ပြီး "မိုးရွာနေရင် ထီးယူ၊ မဟုတ်ရင် ဦးထုပ်ဆောင်း" လို့ ဆုံးဖြတ်သလိုပါပဲ။ if က မေးခွန်းတစ်ခု မေးပြီး အဖြေ true ဆိုရင် ပထမ လမ်းကို လိုက်၊ false ဆိုရင် else လမ်းကို လိုက်ပါတယ်။ else if တွေ ထပ်ထည့်ရင် လမ်းဆုံမှာ လမ်းကြောင်း သုံးလေးခု ဖြစ်လာပါတယ်။

အရေးကြီးတာက program က လမ်းတစ်ခုတည်းကိုပဲ လိုက်တာပါ။ အပေါ်ကနေ အောက်ကို မေးခွန်းတွေ တစ်ခုချင်း မေးသွားပြီး ပထမဆုံး "ဟုတ်တယ်" ဆိုတဲ့ လမ်းကို ရွေးလိုက်တာနဲ့ ကျန်တဲ့ မေးခွန်းတွေကို လုံးဝ မမေးတော့ပါဘူး။

Problem → Why → Solution

ပြဿနာ: 95 မှတ်ရတဲ့သူကို "Pass" လို့ပဲ ပြတယ်

ဆရာတစ်ယောက်က grade program ရေးလိုက်ပြီး 30 နဲ့ 60 မှတ်နဲ့ စမ်းတော့ Fail နဲ့ Pass မှန်မှန် ထွက်လို့ ကျေနပ်သွားပါတယ်။ ဒါပေမယ့် 95 မှတ်ရတဲ့ ကျောင်းသားက Distinction ရရမယ့်အစား "Pass" ပဲ ရပါတယ်။ Code မှာ syntax error မရှိပါဘူး၊ condition တစ်ခုချင်းကလည်း မှန်ပါတယ် — ပြဿနာက အစဉ်ပါ။

Main.java
public class Main {
  public static void main(String[] args) {
    int mark = 95;

    if (mark >= 40) {
      System.out.println("Pass");          // 95 >= 40 is true: stops here
    } else if (mark >= 80) {
      System.out.println("Distinction");   // can never be reached
    } else {
      System.out.println("Fail");
    }
  }
}

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

else if chain က ပထမဆုံး true ဖြစ်တဲ့ branch ကို run ပြီး ကျန်တာတွေကို ကျော်ပါတယ်။ 80 အထက် ရတဲ့ ဂဏန်းတိုင်းက 40 အထက်လည်း ဖြစ်လို့ mark >= 40 ကို အရင် စစ်ရင် Distinction branch ကို ဘယ်တော့မှ ရောက်မှာ မဟုတ်ပါဘူး။ Compiler က ဒီလို logic အမှားကို မဖမ်းပေးနိုင်လို့ range တွေကို တင်းကျပ်တာ (>= 80) ကနေ စပြီး စီရပါမယ်။

Core concept: boolean တစ်ခု၊ လမ်းကြောင်း တစ်ခု

if ရဲ့ ကွင်းထဲက condition က boolean (true / false) ဖြစ်ရပါမယ်။ JavaScript ဒါမှမဟုတ် Python လို 0, "" ဒါမှမဟုတ် null ကို false အဖြစ် မယူပါဘူး — int count = 3; if (count) {...} က "incompatible types: int cannot be converted to boolean" ဆိုတဲ့ compile error ပါ။ ဒါကြောင့် count > 0, name != null, list.isEmpty() လို boolean ထွက်တဲ့ expression တစ်ခုကို အမြဲ ရေးရပါတယ်။

if / else if / else chain မှာ Java က condition တွေကို အပေါ်ကနေ အောက် စစ်ပြီး ပထမဆုံး true ဖြစ်တဲ့ block တစ်ခုတည်းကို run ပါတယ်။ ဘာမှ true မဖြစ်ရင် else block ကို run ပြီး else မပါရင် ဘာမှ မလုပ်ပါဘူး။ Range တွေ ထပ်နေတဲ့အခါ (>= 80 က >= 40 ထဲမှာ ပါနေတာမျိုး) တင်းကျပ်တဲ့ condition ကို အပေါ်မှာ ထားရပါမယ်။ Condition နှစ်ခုကို && / || နဲ့ ပေါင်းနိုင်ပြီး != null လို guard ကို ဘယ်ဘက်မှာ ထားပါ။

Brace {} တွေက optional ဖြစ်ပေမယ့် brace မပါရင် if က နောက်က statement တစ်ကြောင်းကိုပဲ ထိန်းပါတယ်။ Indent လုပ်ထားတဲ့ ဒုတိယ line က if အပြင်မှာ ရှိပြီး အမြဲ run ပါတယ် — Java က indentation ကို လုံးဝ ဂရုမစိုက်ပါဘူး။ ဒါကြောင့် one-line if တွေမှာပါ brace ကို အမြဲ ထည့်တာ ဘေးကင်းပါတယ်။

Value နှစ်ခုထဲက တစ်ခုကို ရွေးရုံပဲ ဆိုရင် ternary operator condition ? a : b က if / else ထက် တိုပါတယ်: String label = stock > 0 ? "In stock" : "Sold out";။ ဒါပေမယ့် ternary တွေကို အထပ်ထပ် nest လုပ်ရင် ဖတ်ရခက်လို့ ရိုးရှင်းတဲ့ ရွေးချယ်မှုမှာပဲ သုံးပါ။ Variable တစ်ခုတည်းကို ပုံသေ value များစွာနဲ့ နှိုင်းယှဉ်ရမယ်ဆိုရင်တော့ နောက် lesson က switch က ပိုသင့်တော်ပါတယ်။

Block တစ်ခုထဲမှာ ကြေညာတဲ့ variable က အဲဒီ block ထဲမှာပဲ ရှိပါတယ်။ if block ထဲမှာ String message = "Pass"; လို့ ကြေညာရင် block အပြင်မှာ message ကို သုံးလို့ မရပါဘူး — "cannot find symbol" error ရပါတယ်။ Branch တိုင်းက value ထည့်ရမယ့် variable ကို chain မတိုင်ခင် ကြေညာပြီး branch တစ်ခုချင်းမှာ assign လုပ်ပါ။ Java compiler က branch တိုင်းမှာ assign လုပ်ထားမှ (definite assignment) အဲဒီ variable ကို နောက်မှာ ဖတ်ခွင့် ပေးပါတယ်။

Main.java
public class Main {
  public static void main(String[] args) {
    int stock = 0;
    String label = stock > 0 ? "In stock" : "Sold out";
    System.out.println(label);

    String role = "editor";
    boolean loggedIn = true;
    if (!loggedIn) {
      System.out.println("Please log in");
    } else if ("admin".equals(role) || "editor".equals(role)) {
      System.out.println("Can edit");
    } else {
      System.out.println("Read only");
    }
  }
}
else if chain ကို ဘယ်လို ဖြတ်သန်းလဲ
  1. ပထမ condition ကို တွက်
  2. true ဆိုရင် အဲဒီ block ကို run ပြီး ထွက်
  3. false ဆိုရင် နောက် condition ကို ဆင်း
  4. ဘာမှ မမှန်ရင် else ကို run

Chain တစ်ခုမှာ block တစ်ခုထက် ပို မ run ပါဘူး — ဒါကြောင့် အစဉ်က logic ရဲ့ အစိတ်အပိုင်းပါ။

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

java
if (strictestCondition) {
  // runs when the first condition is true
} else if (nextCondition) {
  // runs only if every condition above was false
} else {
  // runs when nothing above was true (optional)
}

Type value = condition ? valueIfTrue : valueIfFalse;

Condition တိုင်းက boolean ဖြစ်ရပြီး ကွင်း () ထဲမှာ ရေးရပါတယ်။ else if ကို လိုသလောက် ထည့်နိုင်ပြီး else က optional ပါ။ Block တစ်ခုချင်းကို brace နဲ့ ပိတ်ပါ။ နောက်ဆုံး line က value နှစ်ခုထဲက တစ်ခု ရွေးဖို့ ternary ပုံစံပါ။

Example: အမှတ်ကနေ ရလဒ် ဆုံးဖြတ်ခြင်း

ဒီ example က problem section က code ကို ပြင်ထားတဲ့ ပုံစံပါ — တင်းကျပ်တဲ့ >= 80 ကို အပေါ်ဆုံးမှာ ထားပြီး >= 40 ကို နောက်မှ စစ်ပါတယ်။ mark = 75 နဲ့ run ရင် ဘယ် line တွေ အလုပ်လုပ်လဲ လိုက်ကြည့်ရအောင်။

java
public class Main {
  public static void main(String[] args) {
    int mark = 75;

    if (mark >= 80) {
      System.out.println("Distinction");
    } else if (mark >= 40) {
      System.out.println("Pass");
    } else {
      System.out.println("Fail");
    }
  }
}

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

  1. Line 3int mark = 75;

    စစ်မယ့် value ပါ။ 85, 40, 39 လို value တွေနဲ့ ပြောင်းစမ်းကြည့်ရင် branch သုံးခုလုံးကို မြင်ရပါမယ်။

  2. Line 5if (mark >= 80) {

    75 >= 80 က false ဖြစ်လို့ Distinction block ကို ကျော်ပြီး နောက် condition ကို ဆင်းပါတယ်။ အတင်းကျပ်ဆုံး range ဖြစ်လို့ အပေါ်ဆုံးမှာ ရှိရပါမယ်။

  3. Line 7} else if (mark >= 40) {

    ဒီ condition ကို ရောက်လာတယ်ဆိုတာ mark < 80 ဆိုတာ သေချာပြီးသားပါ။ ဒါကြောင့် ဒီ branch က တကယ်တော့ "40 ကနေ 79 ထိ" ကို ဆိုလိုပါတယ်။ 75 >= 40 က true မို့ Pass ကို print ထုတ်ပါတယ်။

  4. Line 9} else {

    အပေါ်က condition နှစ်ခုလုံး false ဖြစ်မှ (mark < 40) ဒီ block ကို ရောက်ပါတယ်။ ဒီ run မှာတော့ Pass branch က run ပြီးသားမို့ else ကို လုံးဝ မစစ်တော့ပါဘူး။

You should see
Pass
mark = 75 အတွက် ဆုံးဖြတ်ချက် လမ်းကြောင်း
  1. mark >= 80 ? → false
  2. mark >= 40 ? → true
  3. "Pass" ကို print
  4. Chain ကနေ ထွက်

mark 80 နဲ့ အထက်

Distinction

mark 40 ကနေ 79

Pass

mark 40 အောက်

Fail

instanceof pattern matching

Object တစ်ခုရဲ့ type ကို စစ်ပြီး အဲဒီ type အဖြစ် သုံးချင်ရင် Java 16 ကစပြီး instanceof pattern ကို သုံးလို့ရပါတယ်။ input instanceof String s က input က String ဟုတ်ရင် true ပေးပြီး s ဆိုတဲ့ String variable ကိုပါ ဖန်တီးပေးလို့ cast ကို ကိုယ်တိုင် ရေးစရာ မလိုပါဘူး။ && ရဲ့ ညာဘက်မှာ s ကို ချက်ချင်း သုံးလို့ရပါတယ်၊ ဘယ်ဘက် true ဖြစ်မှ ညာဘက်ကို တွက်လို့ပါ။

Main.java
public class Main {
  static String describe(Object input) {
    if (input instanceof Integer n) {
      return "Integer, next is " + (n + 1);
    } else if (input instanceof String s && !s.isBlank()) {
      return "Text with " + s.strip().length() + " characters";
    } else {
      return "Unsupported input";
    }
  }

  public static void main(String[] args) {
    System.out.println(describe(41));
    System.out.println(describe("  hello "));
    System.out.println(describe("   "));
    System.out.println(describe(3.5));
  }
}

Output က Integer, next is 42၊ Text with 5 characters၊ နဲ့ Unsupported input နှစ်ကြောင်း ဖြစ်ပါတယ် — space တွေပဲ ပါတဲ့ String က isBlank() စစ်ချက်ကြောင့်၊ 3.5 က Double ဖြစ်လို့ပါ။

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

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

Backend method တွေမှာ "guard clause" ပုံစံကို အများဆုံး တွေ့ရပါတယ် — method အစမှာ မမှန်တဲ့ input တွေကို if နဲ့ စစ်ပြီး ချက်ချင်း return ဒါမှမဟုတ် exception ပစ်လိုက်တာပါ: if (order == null) return;၊ if (amount <= 0) throw ...။ ဒါမှ အဓိက logic က nested if အထပ်ထပ် မဖြစ်ဘဲ ညာဘက်ကို မရွေ့သွားပါဘူး။ Payment approve/decline, age restriction, shipping fee tier, discount tier တွေကတော့ else if chain ဖြစ်ပြီး အစဉ်မှားရင် customer ကို ဈေးမှားကောက်မိနိုင်ပါတယ်။

Tier logic တွေကို test လုပ်တဲ့အခါ boundary value တွေ (39, 40, 79, 80) ကို တမင်ရွေးပြီး စမ်းတာက team တွေရဲ့ အလေ့အထပါ၊ >= နဲ့ > မှားတာ၊ အစဉ်မှားတာတွေက အဲဒီ နယ်နိမိတ်တွေမှာပဲ ပေါ်လို့ပါ။ instanceof pattern ကတော့ JSON ကနေ parse လုပ်ထားတဲ့ Object value တွေ၊ event handler တွေမှာ type အလိုက် ကိုင်တွယ်ဖို့ သုံးပါတယ်။

အမှားများ

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

1. ထပ်နေတဲ့ range တွေကို အစဉ်မှား စီတာ

mark >= 40 ကို mark >= 80 မတိုင်ခင် စစ်ရင် Distinction branch က dead code ဖြစ်သွားပါတယ်။ Compiler က သတိမပေးပါဘူး။ တင်းကျပ်တာကနေ စပြီး စီပါ၊ ဒါမှမဟုတ် mark >= 40 && mark < 80 လို range ကို အပြည့်အစုံ ရေးပါ။

2. Brace မပါဘဲ line နှစ်ကြောင်း indent လုပ်တာ

if (x > 10) နောက်မှာ indent လုပ်ထားတဲ့ line နှစ်ကြောင်း ရှိရင် ပထမ line ပဲ if ရဲ့ အပိုင်းဖြစ်ပြီး ဒုတိယ line က x ဘယ်လောက်ပဲ ဖြစ်ဖြစ် အမြဲ run ပါတယ်။ Java က indentation ကို မကြည့်ပါဘူး။ Block တိုင်းမှာ { } ကို ထည့်ပါ။

3. boolean ကို = true နဲ့ စစ်တာ

if (isAdmin = true) က compile ဖြစ်ပြီး isAdmin ထဲ true ကို ထည့်လိုက်လို့ branch က အမြဲ run ပါတယ်။ if (isAdmin) နဲ့ if (!isAdmin) လို့ပဲ ရေးပါ — == true ကို ရေးစရာ မလိုသလို = typo ဖြစ်နိုင်ခြေလည်း မရှိတော့ပါဘူး။

Mini challenge

Mini challenge: ရုပ်ရှင်လက်မှတ် ဈေးနှုန်း

အသက်နဲ့ member ဟုတ်မဟုတ်ကို ကြည့်ပြီး ရုပ်ရှင်လက်မှတ် ဈေးနှုန်း တွက်ပေးတဲ့ program ရေးပါ။

  • အသက် 0 အောက် ဒါမှမဟုတ် 120 အထက်ဆိုရင် -1 ပြန်ပေးရမယ် (guard clause)
  • 12 အောက် 3000၊ 60 နဲ့ အထက် 3500၊ ကျန်တာ 5000 MMK
  • Member ဖြစ်ပြီး အသက် 12 နဲ့ အထက်ဆိုရင် 1000 လျှော့ရမယ်
  • show() က invalid age ကို သီးခြား message နဲ့ ပြရမယ်
Main.java
public class Main {
  static int ticketPrice(int age, boolean member) {
    // TODO: guard clause for impossible ages (return -1)
    // TODO: else if chain for the three age groups
    // TODO: member discount for ages 12 and over
    return 0;
  }

  static void show(int age, boolean member) {
    // TODO: "Age 30 (member): 4000 MMK" or "Age -3: invalid age"
  }

  public static void main(String[] args) {
    show(8, true);
    show(30, false);
    show(30, true);
    show(65, true);
    show(-3, false);
  }
}

ရလဒ်: Program က ဒီ line ငါးကြောင်းကို ထုတ်ရပါမယ်: Age 8 (member): 3000 MMK Age 30: 5000 MMK Age 30 (member): 4000 MMK Age 65 (member): 2500 MMK Age -3: invalid age

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

Guard clause ကို method အစမှာ ထားပါ။ ဈေးနှုန်းအတွက် int price; ကို ကြေညာပြီး chain ထဲမှာ branch တိုင်းက value ထည့်ပါ။ " (member)" စာသားကို ternary နဲ့ ထည့်လို့ရပါတယ်။

အဖြေ ကြည့်ရန်
Main.java
public class Main {
  static int ticketPrice(int age, boolean member) {
    if (age < 0 || age > 120) {
      return -1;
    }
    int price;
    if (age < 12) {
      price = 3000;
    } else if (age >= 60) {
      price = 3500;
    } else {
      price = 5000;
    }
    if (member && age >= 12) {
      price -= 1000;
    }
    return price;
  }

  static void show(int age, boolean member) {
    int price = ticketPrice(age, member);
    String who = "Age " + age + (member ? " (member)" : "");
    if (price < 0) {
      System.out.println(who + ": invalid age");
    } else {
      System.out.println(who + ": " + price + " MMK");
    }
  }

  public static void main(String[] args) {
    show(8, true);
    show(30, false);
    show(30, true);
    show(65, true);
    show(-3, false);
  }
}

Quick Quiz 1 / 3

int count = 3; if (count) { System.out.println("items"); } ကို compile လုပ်ရင် ဘာဖြစ်မလဲ။

Quick Quiz 2 / 3

int x = 5; if (x > 10) System.out.println("big"); System.out.println("checked"); (ဒုတိယ println ကို indent လုပ်ထား) — ဘာ print ထုတ်မလဲ။

Quick Quiz 3 / 3

Shipping fee: 100000 နဲ့ အထက် free၊ 50000 နဲ့ အထက် 2000၊ ကျန်တာ 4000။ ဘယ် condition အစဉ်က မှန်လဲ။

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

  • if condition က boolean ပဲ ဖြစ်ရပြီး Java မှာ 0 ဒါမှမဟုတ် null ကို false အဖြစ် မယူပါဘူး။
  • else if chain က ပထမဆုံး true ဖြစ်တဲ့ branch တစ်ခုတည်းကို run လို့ range တွေကို တင်းကျပ်တာကနေ စီပါ။
  • Block တိုင်းမှာ brace ထည့်ပါ — Java က indentation ကို ဂရုမစိုက်ပါဘူး။
  • Value နှစ်ခုထဲက ရွေးရင် ? : ကို၊ type စစ်ရင် instanceof pattern ကို သုံးပါ။

Java Language Specification, Java SE 25 — Oracle

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

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

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

If...Else | Thuta Learning