ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
လမ်းဆုံတစ်ခုမှာ ရပ်ပြီး "မိုးရွာနေရင် ထီးယူ၊ မဟုတ်ရင် ဦးထုပ်ဆောင်း" လို့ ဆုံးဖြတ်သလိုပါပဲ။ 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 တစ်ခုချင်းကလည်း မှန်ပါတယ် — ပြဿနာက အစဉ်ပါ။
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 ကို နောက်မှာ ဖတ်ခွင့် ပေးပါတယ်။
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");
}
}
}- ပထမ condition ကို တွက်
- true ဆိုရင် အဲဒီ block ကို run ပြီး ထွက်
- false ဆိုရင် နောက် condition ကို ဆင်း
- ဘာမှ မမှန်ရင် else ကို run
Chain တစ်ခုမှာ block တစ်ခုထက် ပို မ run ပါဘူး — ဒါကြောင့် အစဉ်က logic ရဲ့ အစိတ်အပိုင်းပါ။
အထွေထွေ ပုံစံ
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 တွေ အလုပ်လုပ်လဲ လိုက်ကြည့်ရအောင်။
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 ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 3
int mark = 75;စစ်မယ့် value ပါ။ 85, 40, 39 လို value တွေနဲ့ ပြောင်းစမ်းကြည့်ရင် branch သုံးခုလုံးကို မြင်ရပါမယ်။
- Line 5
if (mark >= 80) {75 >= 80 က false ဖြစ်လို့ Distinction block ကို ကျော်ပြီး နောက် condition ကို ဆင်းပါတယ်။ အတင်းကျပ်ဆုံး range ဖြစ်လို့ အပေါ်ဆုံးမှာ ရှိရပါမယ်။
- Line 7
} else if (mark >= 40) {ဒီ condition ကို ရောက်လာတယ်ဆိုတာ mark < 80 ဆိုတာ သေချာပြီးသားပါ။ ဒါကြောင့် ဒီ branch က တကယ်တော့ "40 ကနေ 79 ထိ" ကို ဆိုလိုပါတယ်။ 75 >= 40 က true မို့ Pass ကို print ထုတ်ပါတယ်။
- Line 9
} else {အပေါ်က condition နှစ်ခုလုံး false ဖြစ်မှ (mark < 40) ဒီ block ကို ရောက်ပါတယ်။ ဒီ run မှာတော့ Pass branch က run ပြီးသားမို့ else ကို လုံးဝ မစစ်တော့ပါဘူး။
Pass- mark >= 80 ? → false
- mark >= 40 ? → true
- "Pass" ကို print
- 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 ဖြစ်မှ ညာဘက်ကို တွက်လို့ပါ။
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 နဲ့ ပြရမယ်
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 နဲ့ ထည့်လို့ရပါတယ်။
အဖြေ ကြည့်ရန်
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
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- if condition က boolean ပဲ ဖြစ်ရပြီး Java မှာ 0 ဒါမှမဟုတ် null ကို false အဖြစ် မယူပါဘူး။
- else if chain က ပထမဆုံး true ဖြစ်တဲ့ branch တစ်ခုတည်းကို run လို့ range တွေကို တင်းကျပ်တာကနေ စီပါ။
- Block တိုင်းမှာ brace ထည့်ပါ — Java က indentation ကို ဂရုမစိုက်ပါဘူး။
- Value နှစ်ခုထဲက ရွေးရင် ? : ကို၊ type စစ်ရင် instanceof pattern ကို သုံးပါ။