🔀 Control Flow · Beginner

Classic switch statement in Java

case labels, fall-through without break, default.

🧩 The mysteryThe player picks level 1: Easy. The game prints "Easy" AND "Hard". Nobody pressed anything else. Welcome to fall-through.

A jump table

A switch looks at one value and **jumps straight to the matching case label**. It's a tidy replacement for a long chain of if (day == 1) ... else if (day == 2) ....

switch (day) {
    case 1:
        System.out.println("Mon");
        break;
    case 2:
        System.out.println("Tue");
        break;
}

Labels are doors, not walls

With classic colon labels (case 1:), a label is only an entry point. After jumping in, Java keeps running every following line, straight through other case labels, until it hits a break or the end of the switch. This is called fall-through.

case 1:
    System.out.println("Easy");
    // no break: keeps going!
case 2:
    System.out.println("Hard");
    break;
🔮 Predict it

Your turn

n is 1. What prints?

int n = 1;
switch (n) {
    case 1: System.out.println("one");
    case 2: System.out.println("two");
        break;
    case 3: System.out.println("three");
}
  1. one
  2. one two
  3. one two three
  4. two
Show the answer

one then two. Java jumps to case 1, falls through into case 2 (no break), and finally stops at the break. three never runs.

default and shared labels

default runs when no case matches. It's optional and may appear anywhere, not just last, and it falls through like any other label. Several labels can share one body: stack them (case 6: case 7:) or list them (case 6, 7:). Fall-through on purpose!

switch (day) {
    case 6:
    case 7:
        System.out.println("Weekend");
        break;
    default:
        System.out.println("Weekday");
}
🔮 Predict it

default in the middle

c is 9, which matches no case. What prints?

int c = 9;
switch (c) {
    case 1: System.out.println("low");
        break;
    default: System.out.println("unknown");
    case 2: System.out.println("two");
    case 3: System.out.println("three");
        break;
}
  1. unknown
  2. unknown two three
  3. unknown two
  4. low unknown
Show the answer

Nothing matches 9, so execution starts at default. default isn't special about fall-through: it continues into case 2 and case 3 and stops at that break.

⚠️ The trap

Labels must be constant and unique

Every case label must be a compile-time constant (like 1, 'A' or a final constant), and no two labels may be equal. A repeated case 1 is a compile error: duplicate case label.

switch (x) {
    case 1: System.out.println("a"); break;
    case 1: System.out.println("b"); break;
} // compile error: duplicate case label
💼 In the real world

In real projects

Accidental fall-through is such a common bug that javac -Xlint:fallthrough warns about it, and teams mark intentional fall-through with a // fall through comment. Most modern Java code avoids the problem entirely with arrow labels, coming up next.

Key takeaways

  1. Forgetting break makes execution fall through into the next case
  2. default is optional and may appear anywhere, not just last
  3. Case labels must be constants and unique
  4. Several labels can share one body: case 6: case 7: or case 6, 7:

💡 Old switch is like a slide with entry doors: you enter at the matching door and slide all the way down unless a break catches you.

🤯 Did you know?

In 1983 Tom Duff used C's switch fall-through to jump into the *middle* of a loop to speed up copying. "Duff's device" is perfectly legal C, and famously horrifying to read.

Practice questions

What does this print?

int n = 2;
switch (n) {
    case 1: System.out.println("one");
    case 2: System.out.println("two");
    case 3: System.out.println("three");
    default: System.out.println("other");
}
  1. two
  2. two three
  3. two three other
  4. one two three other
Check your answer

two three other. Execution jumps to case 2, then falls through every later label because there's no break, including default.

What does this print?

int x = 1;
switch (x) {
    case 1: System.out.println("a"); break;
    case 1: System.out.println("b"); break;
}
  1. a
  2. a b
  3. Compile error
  4. b
Check your answer

Compile error. Case labels must be unique. The compiler rejects the second case 1 with duplicate case label.

Next: Java 14 rebuilt switch so it can hand back a value, and fall-through is gone for good.