Skip to content

If... Else ๐Ÿง

Mentor's Note: If-Else is the "Choice Maker" of programming. Without it, a computer is just a calculator. With it, it becomes a Smart Machine that can make decisions based on different situations! ๐Ÿ’ก


๐ŸŒŸ The Scenario: The ATM Withdrawal ๐Ÿง

Imagine you are using an ATM to withdraw money.

  • The "If" (The Check): The ATM checks: "IF your balance is greater than the withdrawal amount, dispense the cash." ๐Ÿ’ต
  • The "Else" (The Backup): "ELSE (Otherwise), show a message saying 'Insufficient Funds'." ๐Ÿšซ
  • The "Else If" (The Multiple Choices): "ELSE IF you have a 'VIP Account', allow the withdrawal even if the balance is low." ๐Ÿ›ก๏ธ
  • The Result: The ATM makes a logical decision based on your specific situation. โœ…

๐ŸŽจ Visual Logic: The Decision Tree

graph TD
    A[Start: Check Balance โ“] --> B{Balance >= Amount?}
    B -- Yes โœ… --> C[Dispense Cash ๐Ÿ’ต]
    B -- No โŒ --> D{Is it a VIP Account?}
    D -- Yes โœ… --> E[Dispense Cash ๐Ÿ›ก๏ธ]
    D -- No โŒ --> F[Show Error ๐Ÿšซ]

๐Ÿ“– Concept Explanation

1. The if Statement โ“

The most basic decision. If the condition is true, the code inside {} runs. If it's false, Java skips it.

2. The else Statement ๐Ÿšซ

The "Otherwise" plan. It only runs if the if condition was false.

3. The else if Ladder ๐Ÿง—

Used when you have 3 or more options. Java checks them one by one. As soon as it finds a true condition, it runs that block and skips the rest.

4. The Ternary Operator (? :) โšก

A shorthand for a simple if-else. It fits on one line! - String result = (20 > 18) ? "Yes" : "No";


๐Ÿ’ป Implementation: The ATM Lab

// ๐Ÿ›’ Scenario: Basic ATM Withdrawal
// ๐Ÿš€ Action: Checking balance and VIP status

public class Main {
    public static void main(String[] args) {
        int balance = 5000;
        int withdrawAmount = 6000;
        boolean isVIP = true;

        // ๐Ÿง— The Decision Ladder
        if (balance >= withdrawAmount) {
            System.out.println("Withdrawal Successful! ๐Ÿ’ต");
        } 
        else if (isVIP) {
            System.out.println("VIP Overdraft Allowed. Withdrawal Successful! ๐Ÿ›ก๏ธ");
        } 
        else {
            System.out.println("Insufficient Funds. ๐Ÿšซ");
        }

        // โšก Ternary Shorthand
        String msg = (isVIP) ? "Welcome VIP!" : "Welcome Member!";
        System.out.println(msg);
    }
}

๐Ÿ“Š Sample Dry Run (Logic)

Step Action Logic Result
1 if (balance >= 6000) Is 5000 >= 6000? false โŒ
2 else if (isVIP) Is VIP true? true โœ…
3 System.out... Run the VIP block "VIP Allowed" ๐Ÿ›ก๏ธ
4 else Skip (Already found a true block) Ignored ๐Ÿค–

๐Ÿ“ˆ Technical Analysis: The Braces Rule โš ๏ธ

In Java, if you only have ONE line of code inside an if, you don't need {}. - if (x > 10) System.out.println("Hi"); - However, professional coders ALWAYS use curly braces to avoid bugs if they add more lines later. ๐Ÿ›ก๏ธ


๐ŸŽฏ Practice Lab ๐Ÿงช

Task: The Grade Calculator

Task: Create a variable score. - If score >= 90, print "Grade A". - Else If score >= 80, print "Grade B". - Else, print "Grade C". Goal: Use the if-else if-else structure correctly. ๐Ÿ’ก


๐Ÿ’ก Interview Tip ๐Ÿ‘”

"Interviewers often ask: 'Can we have multiple else statements in one if?' Answer: NO. You can have many else if statements, but only one else (the final backup) at the very end."


๐Ÿ’ก Pro Tip: "Always check for the most specific condition first (the hardest one to meet) at the top of your ladder!" - Vishnu Damwala


โ† Back: Booleans | Next: Switch โ†’