Aral Code

Control Flow Lesson 11 of 21

Java switch Statement and Switch Expressions

Choose between exact values with switch, case, break and default, group cases, switch on Strings, and use modern arrow-style switch expressions.

When one variable is compared against many exact values, like a menu choice or a day number, a switch is cleaner than a long else if chain.

Classic switch

public class Main {
    public static void main(String[] args) {
        int choice = 2;
        switch (choice) {
            case 1:
                System.out.println("Pandesal - ₱25");
                break;
            case 2:
                System.out.println("Kape - ₱8");
                break;
            case 3:
                System.out.println("Itlog - ₱9");
                break;
            default:
                System.out.println("Invalid choice");
        }
    }
}

Output

Kape - ₱8
  • Java jumps to the case that matches choice.
  • break exits the switch.
  • default runs when nothing matches.

Fall-through: forgetting break

public class Main {
    public static void main(String[] args) {
        int level = 2;
        switch (level) {
            case 1:
                System.out.println("Bronze");
            case 2:
                System.out.println("Silver");
            case 3:
                System.out.println("Gold");
            default:
                System.out.println("Unknown");
        }
    }
}

Output

Silver
Gold
Unknown

Grouping cases

public class Main {
    public static void main(String[] args) {
        int month = 4;
        String season;
        switch (month) {
            case 12: case 1: case 2:
                season = "Cool dry season (Amihan)";
                break;
            case 3: case 4: case 5:
                season = "Hot dry season (Tag-init)";
                break;
            default:
                season = "Rainy season (Habagat)";
        }
        System.out.println(season);
    }
}

Output

Hot dry season (Tag-init)

Switch on Strings and chars

public class Main {
    public static void main(String[] args) {
        String day = "Fri";
        switch (day) {
            case "Mon":
                System.out.println("Flag ceremony");
                break;
            case "Fri":
                System.out.println("Coding club");
                break;
            default:
                System.out.println("Regular class");
        }
    }
}

Output

Coding club

Switch works with int, char, String and enums, but not with double or boolean, and not with ranges like "90 and above". For ranges, use if-else.

Modern switch expressions (Java 14+)

The new arrow syntax has no fall-through, needs no break, and can produce a value directly:

public class Main {
    public static void main(String[] args) {
        int choice = 3;
        String item = switch (choice) {
            case 1 -> "Pandesal";
            case 2 -> "Kape";
            case 3 -> "Itlog";
            default -> "Unknown";
        };
        System.out.println("You chose: " + item);

        char g = 'B';
        int points = switch (g) {
            case 'A', 'a' -> 4;
            case 'B', 'b' -> 3;
            case 'C', 'c' -> 2;
            default -> 0;
        };
        System.out.println("Points: " + points);
    }
}

Output

You chose: Itlog
Points: 3

Notice the semicolon after the closing brace: the whole switch is an expression assigned to a variable. The arrow style is safer and shorter; use it if your Java version is 14 or newer.

A calculator

public class Main {
    public static void main(String[] args) {
        double a = 12, b = 4;
        char op = '/';
        double result = switch (op) {
            case '+' -> a + b;
            case '-' -> a - b;
            case '*' -> a * b;
            case '/' -> a / b;
            default -> Double.NaN;
        };
        System.out.println(a + " " + op + " " + b + " = " + result);
    }
}

Output

12.0 / 4.0 = 3.0

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What happens if a case has no break?
2. Which type can NOT be used in a classic switch?
3. What does default do?
4. Do arrow-style cases (case 1 -> ...) need break?

By Sir Reyel · Last updated 2026-12-04