If... Else π§ΒΆ
Prerequisites: Java Booleans
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
elsestatements in oneif?' Answer: NO. You can have manyelse ifstatements, but only oneelse(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