Module 3 ¡ Lesson 9 If-Else Statements
Module 3 - Lesson 9

If-Else Statements: Making Decisions in Java

Control flow ရဲ့ အခြေခံ - conditions တွေအပေါ် မူတည်ပြီး code က ဘယ်လို run မလဲ ဆုံးဖြတ်နည်းကို လေ့လာကြမယ်။

⏱️ 45 minutes 📚 Intermediate 💻 Hands-on Practice

🎯 သင် သင်ယူမည့် အရာများ

ဒီ lesson မှာ သင် သင်ယူမည့် အရာများ:

  • If statement ရဲ့ syntax နဲ့ အလုပ်လုပ်ပုံ
  • If-else နဲ့ if-else-if အသုံးပြုနည်း
  • Nested if statements (if ထဲမှာ if)
  • Comparison operators (==, !=, >, <,>=, <=)< /li>
  • Logical operators (&&, ||, !) နဲ့ ပေါင်းစပ်အသုံးပြုခြင်း
  • Real-world examples များ

📖 If Statements ဆိုတာ ဘာလဲ?

If statement ဆိုတာ program မှာ decision making လုပ်ဖို့ အသုံးပြုတဲ့ control flow statement တစ်ခုပါ။ သင့် program က conditions တွေအပေါ် မူတည်ပြီး မတူညီတဲ့ code blocks တွေကို execute လုပ်နိုင်ပါတယ်။

နေ့စဉ်ဘဝမှာ ဥပမာ:

  • အကယ်၍ မိုးရွာနေရင် → ထီးကိုင်ပါ
  • အကယ်၍ အပူချိန် > 30°C ဖြစ်ရင် → AC ဖွင့်ပါ
  • အကယ်၍ score >= 50 ဖြစ်ရင် → Pass, မဟုတ်ရင် → Fail

Programming မှာလည်း အတူတူပါပဲ!

🔹 Simple If Statement

အရိုးရှင်းဆုံး if statement ရဲ့ syntax:

Java
if (condition) {
    // condition က true ဖြစ်ရင် ဒီ code block run မယ်
    statement;
}

Example: အသက် စစ်ဆေးခြင်း

Java
public class AgeCheck {
    public static void main(String[] args) {
        int age = 20;
        
        if (age >= 18) {
            System.out.println("You are an adult!");
        }
        
        System.out.println("Program finished.");
    }
}
Output:
You are an adult!
Program finished.

ရှင်းလင်းချက်:

  • age >= 18 က true ဖြစ်လို့ if block ထဲက code run တယ်
  • System.out.println("Program finished.") က if block အပြင်မှာ ရှိလို့ အမြဲ run တယ်

🔹 If-Else Statement

Condition က true မဟုတ်ရင် တခြား code block တစ်ခု run ချင်ရင် else သုံးပါ:

Java
if (condition) {
    // condition က true ဖြစ်ရင် run မယ်
} else {
    // condition က false ဖြစ်ရင် run မယ်
}

Example: Pass/Fail စစ်ဆေးခြင်း

Java
public class ExamResult {
    public static void main(String[] args) {
        int score = 45;
        
        if (score >= 50) {
            System.out.println("Congratulations! You passed!");
            System.out.println("Your score: " + score);
        } else {
            System.out.println("Sorry, you failed.");
            System.out.println("Your score: " + score);
            System.out.println("Try again!");
        }
    }
}
Output:
Sorry, you failed.
Your score: 45
Try again!

🔹 If-Else-If Ladder

Conditions အများကြီး စစ်ဆေးချင်ရင် else if သုံးပါ:

Java
if (condition1) {
    // condition1 true ဖြစ်ရင်
} else if (condition2) {
    // condition2 true ဖြစ်ရင်
} else if (condition3) {
    // condition3 true ဖြစ်ရင်
} else {
    // အားလုံး false ဖြစ်ရင်
}

Example: Grade System

Java
public class GradeSystem {
    public static void main(String[] args) {
        int marks = 75;
        String grade;
        
        if (marks >= 90) {
            grade = "A - Excellent!";
        } else if (marks >= 80) {
            grade = "B - Very Good!";
        } else if (marks >= 70) {
            grade = "C - Good!";
        } else if (marks >= 50) {
            grade = "D - Pass";
        } else {
            grade = "F - Fail";
        }
        
        System.out.println("Your marks: " + marks);
        System.out.println("Your grade: " + grade);
    }
}
Output:
Your marks: 75
Your grade: C - Good!

အရေးကြီးတဲ့ အချက်:

  • If-else-if ladder က အပေါ်ကနေ အောက်ကို စစ်တယ်
  • ပထမ true condition တွေ့ရင် ကျန်တာတွေ မစစ်တော့ဘူး
  • Condition order က အရေးကြီးတယ် (အပေါ်မှာ ပိုတင်းကျပ်တဲ့ condition ထားပါ)

🔹 Comparison Operators

Conditions စစ်ဆေးဖို့ အသုံးပြုတဲ့ operators:

Operator ဆိုလိုချက် Example Result
== တူညီသည် 5 == 5 true
!= မတူညီသည် 5 != 3 true
> ပိုကြီးသည် 7 > 5 true
< ပိုသေးသည် 3 < 5 true
>= ပိုကြီးသည် သို့မဟုတ် တူညီသည် 5 >= 5 true
<= ပိုသေးသည် သို့မဟုတ် တူညီသည် 3 <= 5 true

⚠️ သတိပြုရန်: Assignment operator = နဲ့ comparison operator == ကို မရောနှောပါနဲ့!

  • x = 5 → value assign လုပ်တယ်
  • x == 5 → value စစ်ဆေးတယ် (true/false)

🔹 Logical Operators

Multiple conditions တွေကို ပေါင်းစပ်ဖို့:

Operator ဆိုလိုချက် Example Description
&& AND (နှစ်ခုလုံး) x > 5 && x < 10 နှစ်ခုလုံး true ဖြစ်မှ true
|| OR (တစ်ခုခု) x < 5 || x> 10 တစ်ခု true ဖြစ်ရင် true
! NOT (ဆန့်ကျင်ဘက်) !(x == 5) true ကို false, false ကို true လုပ်တယ်

Example: Age & License Check

Java
public class DriverCheck {
    public static void main(String[] args) {
        int age = 22;
        boolean hasLicense = true;
        
        // AND operator - နှစ်ခုလုံး true ဖြစ်ရမယ်
        if (age >= 18 && hasLicense) {
            System.out.println("You can drive!");
        } else {
            System.out.println("You cannot drive.");
            
            if (age < 18) {
                System.out.println("Reason: Too young");
            }
            if (!hasLicense) {
                System.out.println("Reason: No license");
            }
        }
    }
}

Example: Weekend or Holiday

Java
public class DayOff {
    public static void main(String[] args) {
        String day = "Saturday";
        boolean isHoliday = false;
        
        // OR operator - တစ်ခု true ဖြစ်ရင် enough
        if (day.equals("Saturday") || day.equals("Sunday") || isHoliday) {
            System.out.println("It's a day off! Enjoy!");
        } else {
            System.out.println("It's a working day.");
        }
    }
}

🔹 Nested If Statements

If statement တစ်ခုထဲမှာ နောက်ထပ် if statement ထည့်ခြင်း:

Java
public class TicketPrice {
    public static void main(String[] args) {
        int age = 25;
        boolean isStudent = true;
        int price;
        
        if (age < 18) {
            price = 500;  // Child price
        } else {
            // Adult category
            if (isStudent) {
                price = 800;  // Student discount
            } else {
                price = 1000;  // Regular adult
            }
        }
        
        System.out.println("Ticket price: " + price + " Kyats");
        System.out.println("Age: " + age);
        System.out.println("Student: " + isStudent);
    }
}
Output:
Ticket price: 800 Kyats
Age: 25
Student: true

Nested If ကို ဘယ်အချိန်သုံးရမလဲ:

  • Multiple levels of conditions စစ်ရတဲ့အခါ
  • Inner condition က outer condition အပေါ် မူတည်တဲ့အခါ
  • ဒါပေမယ့် nested if များလွန်းရင် code ရှုပ်သွားနိုင်တယ်
  • လွယ်လွယ်နဲ့ ရေးနိုင်ရင် if-else-if သုံးတာ ပိုကောင်းတယ်

💻 Complete Example: Bank Account System

အားလုံးကို ပေါင်းစပ်အသုံးပြုတဲ့ practical example:

Java
public class BankAccount {
    public static void main(String[] args) {
        double balance = 50000;
        double withdrawAmount = 30000;
        int accountAge = 6;  // months
        boolean isPremium = false;
        
        System.out.println("=== Bank Withdrawal System ===");
        System.out.println("Current Balance: " + balance + " Kyats");
        System.out.println("Withdraw Amount: " + withdrawAmount + " Kyats");
        System.out.println();
        
        // Check if account is active (at least 3 months old)
        if (accountAge < 3) {
            System.out.println("Error: Account too new!");
            System.out.println("Wait " + (3 - accountAge) + " more months.");
        } else {
            // Account is active, check withdrawal
            if (withdrawAmount > balance) {
                System.out.println("Error: Insufficient funds!");
                System.out.println("Available: " + balance);
            } else {
                // Sufficient balance
                double fee = 0;
                
                // Calculate fee based on account type
                if (isPremium) {
                    fee = 0;  // No fee for premium
                    System.out.println("Premium Account: No fees!");
                } else {
                    if (withdrawAmount > 100000) {
                        fee = withdrawAmount * 0.02;  // 2% fee
                    } else {
                        fee = 1000;  // Flat fee
                    }
                    System.out.println("Transaction fee: " + fee + " Kyats");
                }
                
                double total = withdrawAmount + fee;
                
                if (total <= balance) {
                    balance = balance - total;
                    System.out.println("✓ Withdrawal successful!");
                    System.out.println("Amount withdrawn: " + withdrawAmount);
                    System.out.println("New balance: " + balance + " Kyats");
                } else {
                    System.out.println("Error: Insufficient for amount + fee!");
                    System.out.println("Need: " + total);
                    System.out.println("Have: " + balance);
                }
            }
        }
        
        System.out.println("\n=== Transaction Complete ===");
    }
}
Output:
=== Bank Withdrawal System ===
Current Balance: 50000.0 Kyats
Withdraw Amount: 30000.0 Kyats

Transaction fee: 1000.0 Kyats
✓ Withdrawal successful!
Amount withdrawn: 30000.0
New balance: 19000.0 Kyats

=== Transaction Complete ===

🎯 အဓိက မှတ်သားရမည့် အချက်များ

  • If statement: Condition true ဖြစ်မှ code block execute လုပ်တယ်
  • If-else: True/false နှစ်မျိုး အတွက် မတူညီတဲ့ code run တယ်
  • If-else-if: Multiple conditions စစ်ဆေးနိုင်တယ် (ပထမ true တွေ့ရင် ရပ်တယ်)
  • Comparison operators: ==, !=, >, <,>=, <= (values ကို နှိုင်းယှဉ်တယ်)
  • Logical operators: && (AND), || (OR), ! (NOT) (conditions ပေါင်းတယ်)
  • Nested if: If ထဲမှာ if - ရှုပ်ထွေးတဲ့ logic အတွက်
  • Code style: Curly braces {} အမြဲသုံးပါ၊ indentation မှန်အောင်ထားပါ
  • သတိပြုရန်: = (assign) နဲ့ == (compare) မရောပါနဲ့!

✍️ Practice Exercise

Exercise: Temperature Advisor

အပူချိန်အပေါ် မူတည်ပြီး အကြံပြုချက် ပေးတဲ့ program ရေးပါ:

  • temp < 10: "It's freezing! Wear heavy coat."
  • temp 10-20: "It's cold. Wear a jacket."
  • temp 21-30: "Pleasant weather!"
  • temp 31-40: "It's hot! Stay hydrated."
  • temp > 40: "Extreme heat! Stay indoors."

Extra: If it's raining (boolean isRaining), add "Take an umbrella!"

💡 Solution ကြည့်ရန် (Try လုပ်ပြီးမှ ကြည့်ပါ)
Java
public class TemperatureAdvisor {
    public static void main(String[] args) {
        int temp = 35;
        boolean isRaining = true;
        
        System.out.println("Current temperature: " + temp + "°C");
        
        if (temp < 10) {
            System.out.println("It's freezing! Wear heavy coat.");
        } else if (temp <= 20) {
            System.out.println("It's cold. Wear a jacket.");
        } else if (temp <= 30) {
            System.out.println("Pleasant weather!");
        } else if (temp <= 40) {
            System.out.println("It's hot! Stay hydrated.");
        } else {
            System.out.println("Extreme heat! Stay indoors.");
        }
        
        if (isRaining) {
            System.out.println("Take an umbrella!");
        }
    }
}

📝 Quick Quiz

Question 1: ဒီ code က ဘာ output ပေးမလဲ?

int x = 10;
if (x > 5 && x < 15) {
    System.out.println("A");
} else {
    System.out.println("B");
}

Question 2: x = 5 ဆိုရင် ဘယ် output ထွက်မလဲ?

if (x >= 10) {
    System.out.println("Large");
} else if (x >= 5) {
    System.out.println("Medium");
} else {
    System.out.println("Small");
}

Question 3: ဘယ် operator က "not equal to" ကို ကိုယ်စားပြုလဲ?