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
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