Fall-Through Behavior in Traditional switch
What You'll Learn
You'll understand how switch cases can "fall through" to the next case when you omit break, and when this behavior is intentional versus accidental.
The Core Idea
In a traditional switch statement, execution continues downward through subsequent cases after matching one, unless you explicitly stop it with break. This is called fall-through behavior.
Think of it like a water slide with multiple exits: once you enter at your matching case, you keep sliding down through all the lower cases until you hit a break (or reach the end). You don't automatically stop at the next case label.
How Fall-Through Works
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
case 2:
System.out.println("Tuesday"); // Matches here
case 3:
System.out.println("Wednesday"); // Falls through!
break;
case 4:
System.out.println("Thursday");
}
Output:
Tuesday
Wednesday
Notice how both "Tuesday" and "Wednesday" print, even though day is 2. Once case 2 matched, execution continued into case 3 until hitting break.
Intentional Fall-Through
Sometimes fall-through is exactly what you want—when multiple cases should execute the same code:
int month = 3;
switch (month) {
case 12:
case 1:
case 2:
System.out.println("Winter");
break;
case 3:
case 4:
case 5:
System.out.println("Spring"); // March falls through to here
break;
}
Here, months 3, 4, and 5 all print "Spring" because they fall through to the same action.
The Danger
Most fall-through is accidental—forgetting break causes bugs. Always add break unless you specifically intend fall-through (and add a comment explaining why).
Key Takeaway: In traditional switch, execution falls through to subsequent cases unless stopped by break. This can be intentional for grouping cases, but is usually a bug—always use break to prevent unintended fall-through.