🌱BeginnerControl Flow

switch Statement

Match a single value against multiple cases using the switch statement - cleaner than long else-if chains for exact-value comparisons.

Overview

The switch statement evaluates a single expression and jumps to the matching case label. It is most useful when you need to compare one variable against many specific values (like menu choices, day numbers, or command names). Java's original switch statement requires a break at the end of each case to prevent "fall-through" into the next case. Java 14 introduced switch expressions with the arrow (→) syntax, which automatically prevents fall-through and can return a value.

Key Points

  • •switch (expr) matches the expression against each case value
  • •break; at the end of each case prevents fall-through into the next case
  • •default: is the catch-all if no case matches (like else in if/else)
  • •Fall-through (omitting break) lets multiple cases share code - use intentionally and comment it
  • •switch works with: int, char, String, and enum (not double or boolean)
  • •Java 14+ switch expressions use -> and return a value without needing break

Examples

Classic switch with break

public class Main {
    public static void main(String[] args) {
        int dayNumber = 3;

        switch (dayNumber) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            case 4:
                System.out.println("Thursday");
                break;
            case 5:
                System.out.println("Friday");
                break;
            default:
                System.out.println("Weekend");
        }
    }
}

Without break, execution falls through into the next case. Always include break unless fall-through is intentional.

Switch with String (Java 7+)

public class Main {
    public static void main(String[] args) {
        String command = "quit";

        switch (command) {
            case "start":
                System.out.println("Starting...");
                break;
            case "stop":
            case "quit":   // two cases sharing one block (intentional fall-through)
                System.out.println("Stopping...");
                break;
            default:
                System.out.println("Unknown command");
        }
    }
}

Two case labels before a single block is intentional fall-through - "stop" and "quit" both print "Stopping..."

Modern switch expression (Java 14+)

public class Main {
    public static void main(String[] args) {
        int day = 3;

        // Switch expression with arrow syntax - no break needed, returns a value
        String name = switch (day) {
            case 1 -> "Monday";
            case 2 -> "Tuesday";
            case 3 -> "Wednesday";
            case 4 -> "Thursday";
            case 5 -> "Friday";
            case 6 -> "Saturday";
            case 7 -> "Sunday";
            default -> "Invalid";
        };

        System.out.println(name); // Wednesday
    }
}

The -> switch expression is more concise, safer (no fall-through), and can be assigned to a variable.

Common Mistakes

  • Forgetting break: the code falls through into the next case, executing code you didn't intend
  • Using double or boolean in a switch - Java only accepts int, char, String, and enum
  • Comparing Strings with == in switch: switch on Strings uses .equals() internally - it is safe, unlike using == yourself

Note

Prefer the modern switch expression (Java 14+) with -> when possible. It is safer (no accidental fall-through), more concise, and can be used as a value in an assignment or return statement.

Exercises

Write Java code and click Run to execute it instantly. When your output looks correct, mark as done.

Loading editor…

Completed: 0 / 4 exercises

Knowledge Check

Question 1 of 4

What happens if you omit `break;` at the end of a switch case?