Skip to content

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!


1. Why Use a Switch Statement?

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.


2. The Better Way: Switch Statement

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.");
}

3. How Does a Switch Statement Work?

  • 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, default runs (like else in an if-else statement).

Example Outputs:

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

4. What Happens Without break;? 🚨

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.");
}

Input: 2

Wrong Output (Without break;):

You chose Taro Tea.  
You chose Matcha Latte.  
You chose Brown Sugar Boba.  
Invalid choice.  

Fix: Always Add break;!


5. Using Switch with Strings 📝

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!


6. When to Use switch vs if-else?

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 `

7. Quick Recap – Switch Statement Cheat Sheet

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

Clone this wiki locally