Course contentsShow
← Java
Lesson 152 of 2,8704. Control Flow StructuresFree lesson

Multiple Case Labels in One Branch

Learn how to handle multiple values in a single case using comma-separated labels in both traditional and modern syntax.

Multiple Case Labels in One Branch

You'll learn how to handle several different case values in a single branch, making your switch statements cleaner and less repetitive.

The Core Idea

Sometimes you want to execute the same code for multiple values. Instead of duplicating code or using fall-through, Java lets you list multiple case labels together, separated by commas.

Think of it like a restaurant menu: "All breakfast items come with coffee." Instead of listing each item separately (pancakes → coffee, eggs → coffee, toast → coffee), you group them: "pancakes, eggs, or toast → coffee."

Traditional Syntax with Fall-Through

Before this feature, you'd stack cases to achieve the same effect:

switch (day) {
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
        System.out.println("Weekday");
        break;
    case 6:
    case 7:
        System.out.println("Weekend");
        break;
}

Modern Syntax with Comma-Separated Labels (Java 14+)

Now you can write this more concisely:

switch (day) {
    case 1, 2, 3, 4, 5 -> System.out.println("Weekday");
    case 6, 7 -> System.out.println("Weekend");
}

Or in a switch expression:

String dayType = switch (day) {
    case 1, 2, 3, 4, 5 -> "Weekday";
    case 6, 7 -> "Weekend";
    default -> "Invalid";
};

Why This Matters

This feature eliminates repetitive code and makes your intent crystal clear: "all these values share the same logic." It works with any type you can use in a switch: int, byte, short, char, String, and enums.

Key Takeaway: Use comma-separated case labels to handle multiple values in a single branch—it's clearer than stacking cases or duplicating code, and works in both traditional and arrow-syntax switch statements.