-
Notifications
You must be signed in to change notification settings - Fork 0
Switch Statements – A Faster Way to Make Decisions
Seanti edited this page Mar 24, 2025
·
1 revision
When making decisions in Java, sometimes if-else statements can get too long and messy. That’s where switch statements come in!
Imagine you’re ordering bubble tea:
- 1 → Milk Tea 🥤
- 2 → Taro Tea 🍠
- 3 → Matcha Latte 🍵
- 4 → Brown Sugar Boba 🍯
- Any other number → "Invalid choice" ❌
With if-else, the code looks like this:
int choice = 2;
if (choice == 1) {
System.out.println("You chose Milk Tea.");
} else if (choice == 2) {
System.out.println("You chose Taro Tea.");
} else if (choice == 3) {
System.out.println("You chose Matcha Latte.");
} else if (choice == 4) {
System.out.println("You chose Brown Sugar Boba.");
} else {
System.out.println("Invalid choice.");
}✅ This works, but it’s repetitive.
We can simplify this using switch:
int choice = 2;
switch (choice) {
case 1:
System.out.println("You chose Milk Tea.");
break;
case 2:
System.out.println("You chose Taro Tea.");
break;
case 3:
System.out.println("You chose Matcha Latte.");
break;
case 4:
System.out.println("You chose Brown Sugar Boba.");
break;
default:
System.out.println("Invalid choice.");
}- Java checks the value of
choice. - It matches the case number (
1, 2, 3, or 4). - It executes the code inside that case.
- The
break;statement stops the switch from checking other cases. - If no case matches,
defaultruns (likeelsein an if-else statement).
Input (choice) |
Output |
|---|---|
1 |
"You chose Milk Tea." |
2 |
"You chose Taro Tea." |
3 |
"You chose Matcha Latte." |
4 |
"You chose Brown Sugar Boba." |
7 |
"Invalid choice." |
If we forget to add break;, Java keeps running the next case!
int choice = 2;
switch (choice) {
case 1:
System.out.println("You chose Milk Tea.");
case 2:
System.out.println("You chose Taro Tea.");
case 3:
System.out.println("You chose Matcha Latte.");
case 4:
System.out.println("You chose Brown Sugar Boba.");
default:
System.out.println("Invalid choice.");
}Wrong Output (Without break;):
You chose Taro Tea.
You chose Matcha Latte.
You chose Brown Sugar Boba.
Invalid choice.
Switch statements also work with Strings!
String drink = "Taro";
switch (drink) {
case "Milk":
System.out.println("You chose Milk Tea.");
break;
case "Taro":
System.out.println("You chose Taro Tea.");
break;
case "Matcha":
System.out.println("You chose Matcha Latte.");
break;
case "Brown Sugar":
System.out.println("You chose Brown Sugar Boba.");
break;
default:
System.out.println("Invalid choice.");
}✅ Works just like numbers but with words!
Use switch when... |
Use if-else when... |
|---|---|
| You are checking specific values (like menu options, grades, or choices). | You have complex conditions (x > 10 && y < 5). |
| You have many cases (like 5+ options). | You are checking ranges (like x >= 1 && x <= 10). |
| The values are numbers or Strings. | The values are **booleans, decimals, or conditions involving logic (&& or ` |
| Part | Description |
|---|---|
switch(expression) |
Checks the value of the expression. |
case value: |
Runs the code if expression matches value. |
break; |
Stops the switch after a case runs. |
default: |
Runs if no case matches (like else). |