Your app has a menu: 1=New, 2=Open, 3=Save, 4=Quit. With if-else you'd write four conditions. With switch, you list each option clearly - the code mirrors the menu structure perfectly.

Day number to name

Convert a number (1-7) to the day name.

day
DayName.java
Replay: real traced execution (multi-file project)
public class DayName {
    public static void main(String[] args) {
        int day = 3;
        String name;

        switch (day) {
            case 1:
                name = "Monday";
                break;
            case 2:
                name = "Tuesday";
                break;
            case 3:
                name = "Wednesday";
                break;
            case 4:
                name = "Thursday";
                break;
            case 5:
                name = "Friday";
                break;
            case 6:
                name = "Saturday";
                break;
            case 7:
                name = "Sunday";
                break;
            default:
                name = "Invalid day";
        }

        System.out.println("Day " + day + " is " + name);
    }
}
public class DayName {
    public static void main(String[] args) {
        int day = 1;
        String name;

        switch (day) {
            case 1:
                name = "Monday";
                break;
            case 2:
                name = "Tuesday";
                break;
            case 3:
                name = "Wednesday";
                break;
            case 4:
                name = "Thursday";
                break;
            case 5:
                name = "Friday";
                break;
            case 6:
                name = "Saturday";
                break;
            case 7:
                name = "Sunday";
                break;
            default:
                name = "Invalid day";
        }

        System.out.println("Day " + day + " is " + name);
    }
}
public class DayName {
    public static void main(String[] args) {
        int day = 5;
        String name;

        switch (day) {
            case 1:
                name = "Monday";
                break;
            case 2:
                name = "Tuesday";
                break;
            case 3:
                name = "Wednesday";
                break;
            case 4:
                name = "Thursday";
                break;
            case 5:
                name = "Friday";
                break;
            case 6:
                name = "Saturday";
                break;
            case 7:
                name = "Sunday";
                break;
            default:
                name = "Invalid day";
        }

        System.out.println("Day " + day + " is " + name);
    }
}
public class DayName {
    public static void main(String[] args) {
        int day = 7;
        String name;

        switch (day) {
            case 1:
                name = "Monday";
                break;
            case 2:
                name = "Tuesday";
                break;
            case 3:
                name = "Wednesday";
                break;
            case 4:
                name = "Thursday";
                break;
            case 5:
                name = "Friday";
                break;
            case 6:
                name = "Saturday";
                break;
            case 7:
                name = "Sunday";
                break;
            default:
                name = "Invalid day";
        }

        System.out.println("Day " + day + " is " + name);
    }
}
  1. day ← 3

    1public class DayName {2    public static void main(String[] args) {3        int day→ 3 = 3;  //@day=1, 5, 74        String name;
  2. switch (day)

    6switch (day3) {7    case 1:8        name = "Monday";
  3. name ← Wednesday

    12    break;13case 3:14    name→ Wednesday = "Wednesday";15    break;16case 4:
  4. System.out.println("Day " + day + " is " + name);

    32    System.out.println("Day " + day3 + " is " + nameWednesday);33}
    outputDay 3 is Wednesday
  1. day ← 1

    1public class DayName {2    public static void main(String[] args) {3        int day→ 1 = 1;4        String name;
  2. switch (day)

    6switch (day1) {7    case 1:8        name = "Monday";
  3. name ← Monday

    6switch (day) {7    case 1:8        name→ Monday = "Monday";9        break;10    case 2:
  4. System.out.println("Day " + day + " is " + name);

    32    System.out.println("Day " + day1 + " is " + nameMonday);33}
    outputDay 1 is Monday
  1. day ← 5

    1public class DayName {2    public static void main(String[] args) {3        int day→ 5 = 5;4        String name;
  2. switch (day)

    6switch (day5) {7    case 1:8        name = "Monday";
  3. name ← Friday

    18    break;19case 5:20    name→ Friday = "Friday";21    break;22case 6:
  4. System.out.println("Day " + day + " is " + name);

    32    System.out.println("Day " + day5 + " is " + nameFriday);33}
    outputDay 5 is Friday
  1. day ← 7

    1public class DayName {2    public static void main(String[] args) {3        int day→ 7 = 7;4        String name;
  2. switch (day)

    6switch (day7) {7    case 1:8        name = "Monday";
  3. name ← Sunday

    24    break;25case 7:26    name→ Sunday = "Sunday";27    break;28default:
  4. System.out.println("Day " + day + " is " + name);

    32    System.out.println("Day " + day7 + " is " + nameSunday);33}
    outputDay 7 is Sunday

Each case handles one value. Don't forget break or execution falls through!

switch Multi-way branch based on a value. Cleaner than long if-else chains.
case A specific value to match: `case 1:`, `case 'A':`, `case "yes":`
break Exit the switch. Without it, execution "falls through" to next case.

Menu option handler

Handle user menu selections with switch.

choice
Menu.java
Replay: real traced execution (multi-file project)
public class Menu {
    public static void main(String[] args) {
        int choice = 2;

        System.out.println("=== Main Menu ===");
        System.out.println("1. New Game");
        System.out.println("2. Load Game");
        System.out.println("3. Settings");
        System.out.println("4. Exit");
        System.out.println("Choice: " + choice);
        System.out.println();

        switch (choice) {
            case 1:
                System.out.println("Starting new game...");
                break;
            case 2:
                System.out.println("Loading saved game...");
                break;
            case 3:
                System.out.println("Opening settings...");
                break;
            case 4:
                System.out.println("Goodbye!");
                break;
            default:
                System.out.println("Invalid option. Please try again.");
        }
    }
}
public class Menu {
    public static void main(String[] args) {
        int choice = 1;

        System.out.println("=== Main Menu ===");
        System.out.println("1. New Game");
        System.out.println("2. Load Game");
        System.out.println("3. Settings");
        System.out.println("4. Exit");
        System.out.println("Choice: " + choice);
        System.out.println();

        switch (choice) {
            case 1:
                System.out.println("Starting new game...");
                break;
            case 2:
                System.out.println("Loading saved game...");
                break;
            case 3:
                System.out.println("Opening settings...");
                break;
            case 4:
                System.out.println("Goodbye!");
                break;
            default:
                System.out.println("Invalid option. Please try again.");
        }
    }
}
public class Menu {
    public static void main(String[] args) {
        int choice = 3;

        System.out.println("=== Main Menu ===");
        System.out.println("1. New Game");
        System.out.println("2. Load Game");
        System.out.println("3. Settings");
        System.out.println("4. Exit");
        System.out.println("Choice: " + choice);
        System.out.println();

        switch (choice) {
            case 1:
                System.out.println("Starting new game...");
                break;
            case 2:
                System.out.println("Loading saved game...");
                break;
            case 3:
                System.out.println("Opening settings...");
                break;
            case 4:
                System.out.println("Goodbye!");
                break;
            default:
                System.out.println("Invalid option. Please try again.");
        }
    }
}
public class Menu {
    public static void main(String[] args) {
        int choice = 4;

        System.out.println("=== Main Menu ===");
        System.out.println("1. New Game");
        System.out.println("2. Load Game");
        System.out.println("3. Settings");
        System.out.println("4. Exit");
        System.out.println("Choice: " + choice);
        System.out.println();

        switch (choice) {
            case 1:
                System.out.println("Starting new game...");
                break;
            case 2:
                System.out.println("Loading saved game...");
                break;
            case 3:
                System.out.println("Opening settings...");
                break;
            case 4:
                System.out.println("Goodbye!");
                break;
            default:
                System.out.println("Invalid option. Please try again.");
        }
    }
}
public class Menu {
    public static void main(String[] args) {
        int choice = 9;

        System.out.println("=== Main Menu ===");
        System.out.println("1. New Game");
        System.out.println("2. Load Game");
        System.out.println("3. Settings");
        System.out.println("4. Exit");
        System.out.println("Choice: " + choice);
        System.out.println();

        switch (choice) {
            case 1:
                System.out.println("Starting new game...");
                break;
            case 2:
                System.out.println("Loading saved game...");
                break;
            case 3:
                System.out.println("Opening settings...");
                break;
            case 4:
                System.out.println("Goodbye!");
                break;
            default:
                System.out.println("Invalid option. Please try again.");
        }
    }
}
  1. choice ← 2

    1public class Menu {2    public static void main(String[] args) {3        int choice→ 2 = 2;  //@choice=1, 3, 4, 94        5        System.out.println("=== Main Menu ===");6        System.out.println("1. New Game");7        System.out.println("2. Load Game");8        System.out.println("3. Settings");9        System.out.println("4. Exit");10        System.out.println("Choice: " + choice2);11        System.out.println();
    output=== Main Menu ===
    1. New Game
    2. Load Game
    3. Settings
    4. Exit
    Choice: 2
  2. switch (choice)

    13switch (choice2) {14    case 1:15        System.out.println("Starting new game...");
  3. case 2:

    16    break;17case 2:18    System.out.println("Loading saved game...");19    break;20case 3:
    outputLoading saved game...
  1. choice ← 1

    1public class Menu {2    public static void main(String[] args) {3        int choice→ 1 = 1;4        5        System.out.println("=== Main Menu ===");6        System.out.println("1. New Game");7        System.out.println("2. Load Game");8        System.out.println("3. Settings");9        System.out.println("4. Exit");10        System.out.println("Choice: " + choice1);11        System.out.println();
    output=== Main Menu ===
    1. New Game
    2. Load Game
    3. Settings
    4. Exit
    Choice: 1
  2. switch (choice)

    13switch (choice1) {14    case 1:15        System.out.println("Starting new game...");
  3. case 1:

    13switch (choice) {14    case 1:15        System.out.println("Starting new game...");16        break;17    case 2:
    outputStarting new game...
  1. choice ← 3

    1public class Menu {2    public static void main(String[] args) {3        int choice→ 3 = 3;4        5        System.out.println("=== Main Menu ===");6        System.out.println("1. New Game");7        System.out.println("2. Load Game");8        System.out.println("3. Settings");9        System.out.println("4. Exit");10        System.out.println("Choice: " + choice3);11        System.out.println();
    output=== Main Menu ===
    1. New Game
    2. Load Game
    3. Settings
    4. Exit
    Choice: 3
  2. switch (choice)

    13switch (choice3) {14    case 1:15        System.out.println("Starting new game...");
  3. case 3:

    19    break;20case 3:21    System.out.println("Opening settings...");22    break;23case 4:
    outputOpening settings...
  1. choice ← 4

    1public class Menu {2    public static void main(String[] args) {3        int choice→ 4 = 4;4        5        System.out.println("=== Main Menu ===");6        System.out.println("1. New Game");7        System.out.println("2. Load Game");8        System.out.println("3. Settings");9        System.out.println("4. Exit");10        System.out.println("Choice: " + choice4);11        System.out.println();
    output=== Main Menu ===
    1. New Game
    2. Load Game
    3. Settings
    4. Exit
    Choice: 4
  2. switch (choice)

    13switch (choice4) {14    case 1:15        System.out.println("Starting new game...");
  3. case 4:

    22    break;23case 4:24    System.out.println("Goodbye!");25    break;26default:
    outputGoodbye!
  1. choice ← 9

    1public class Menu {2    public static void main(String[] args) {3        int choice→ 9 = 9;4        5        System.out.println("=== Main Menu ===");6        System.out.println("1. New Game");7        System.out.println("2. Load Game");8        System.out.println("3. Settings");9        System.out.println("4. Exit");10        System.out.println("Choice: " + choice9);11        System.out.println();
    output=== Main Menu ===
    1. New Game
    2. Load Game
    3. Settings
    4. Exit
    Choice: 9
  2. switch (choice)

    13switch (choice9) {14    case 1:15        System.out.println("Starting new game...");
  3. default:

    25        break;26    default:27        System.out.println("Invalid option. Please try again.");28}
    outputInvalid option. Please try again.

Switch is perfect for menu systems - each option maps to an action.

Grade to description

Convert letter grades to descriptions.

grade
Grade.java
Replay: real traced execution (multi-file project)
public class Grade {
    public static void main(String[] args) {
        char grade = 'B';
        String description;

        switch (grade) {
            case 'A':
                description = "Excellent work!";
                break;
            case 'B':
                description = "Good job!";
                break;
            case 'C':
                description = "Satisfactory";
                break;
            case 'D':
                description = "Needs improvement";
                break;
            case 'F':
                description = "Please see instructor";
                break;
            default:
                description = "Invalid grade";
        }

        System.out.println("Grade: " + grade);
        System.out.println("Feedback: " + description);
    }
}
public class Grade {
    public static void main(String[] args) {
        char grade = 'A';
        String description;

        switch (grade) {
            case 'A':
                description = "Excellent work!";
                break;
            case 'B':
                description = "Good job!";
                break;
            case 'C':
                description = "Satisfactory";
                break;
            case 'D':
                description = "Needs improvement";
                break;
            case 'F':
                description = "Please see instructor";
                break;
            default:
                description = "Invalid grade";
        }

        System.out.println("Grade: " + grade);
        System.out.println("Feedback: " + description);
    }
}
public class Grade {
    public static void main(String[] args) {
        char grade = 'C';
        String description;

        switch (grade) {
            case 'A':
                description = "Excellent work!";
                break;
            case 'B':
                description = "Good job!";
                break;
            case 'C':
                description = "Satisfactory";
                break;
            case 'D':
                description = "Needs improvement";
                break;
            case 'F':
                description = "Please see instructor";
                break;
            default:
                description = "Invalid grade";
        }

        System.out.println("Grade: " + grade);
        System.out.println("Feedback: " + description);
    }
}
public class Grade {
    public static void main(String[] args) {
        char grade = 'F';
        String description;

        switch (grade) {
            case 'A':
                description = "Excellent work!";
                break;
            case 'B':
                description = "Good job!";
                break;
            case 'C':
                description = "Satisfactory";
                break;
            case 'D':
                description = "Needs improvement";
                break;
            case 'F':
                description = "Please see instructor";
                break;
            default:
                description = "Invalid grade";
        }

        System.out.println("Grade: " + grade);
        System.out.println("Feedback: " + description);
    }
}
public class Grade {
    public static void main(String[] args) {
        char grade = 'X';
        String description;

        switch (grade) {
            case 'A':
                description = "Excellent work!";
                break;
            case 'B':
                description = "Good job!";
                break;
            case 'C':
                description = "Satisfactory";
                break;
            case 'D':
                description = "Needs improvement";
                break;
            case 'F':
                description = "Please see instructor";
                break;
            default:
                description = "Invalid grade";
        }

        System.out.println("Grade: " + grade);
        System.out.println("Feedback: " + description);
    }
}
  1. grade ← B

    1public class Grade {2    public static void main(String[] args) {3        char grade→ B = 'B';  //@grade='A', 'C', 'F', 'X'4        String description;
  2. switch (grade)

    6switch (gradeB) {7    case 'A':8        description = "Excellent work!";
  3. description ← Good job!

    9    break;10case 'B':11    description→ Good job! = "Good job!";12    break;13case 'C':
  4. System.out.println("Grade: " + grade);

    26    System.out.println("Grade: " + gradeB);27    System.out.println("Feedback: " + descriptionGood job!);28}
    outputGrade: B
    Feedback: Good job!
  1. grade ← A

    1public class Grade {2    public static void main(String[] args) {3        char grade→ A = 'A';4        String description;
  2. switch (grade)

    6switch (gradeA) {7    case 'A':8        description = "Excellent work!";
  3. description ← Excellent work!

    6switch (grade) {7    case 'A':8        description→ Excellent work! = "Excellent work!";9        break;10    case 'B':
  4. System.out.println("Grade: " + grade);

    26    System.out.println("Grade: " + gradeA);27    System.out.println("Feedback: " + descriptionExcellent work!);28}
    outputGrade: A
    Feedback: Excellent work!
  1. grade ← C

    1public class Grade {2    public static void main(String[] args) {3        char grade→ C = 'C';4        String description;
  2. switch (grade)

    6switch (gradeC) {7    case 'A':8        description = "Excellent work!";
  3. description ← Satisfactory

    12    break;13case 'C':14    description→ Satisfactory = "Satisfactory";15    break;16case 'D':
  4. System.out.println("Grade: " + grade);

    26    System.out.println("Grade: " + gradeC);27    System.out.println("Feedback: " + descriptionSatisfactory);28}
    outputGrade: C
    Feedback: Satisfactory
  1. grade ← F

    1public class Grade {2    public static void main(String[] args) {3        char grade→ F = 'F';4        String description;
  2. switch (grade)

    6switch (gradeF) {7    case 'A':8        description = "Excellent work!";
  3. description ← Please see instructor

    18    break;19case 'F':20    description→ Please see instructor = "Please see instructor";21    break;22default:
  4. System.out.println("Grade: " + grade);

    26    System.out.println("Grade: " + gradeF);27    System.out.println("Feedback: " + descriptionPlease see instructor);28}
    outputGrade: F
    Feedback: Please see instructor
  1. grade ← X

    1public class Grade {2    public static void main(String[] args) {3        char grade→ X = 'X';4        String description;
  2. switch (grade)

    6switch (gradeX) {7    case 'A':8        description = "Excellent work!";
  3. description ← Invalid grade

    21        break;22    default:23        description→ Invalid grade = "Invalid grade";24}
  4. System.out.println("Grade: " + grade);

    26    System.out.println("Grade: " + gradeX);27    System.out.println("Feedback: " + descriptionInvalid grade);28}
    outputGrade: X
    Feedback: Invalid grade

default handles any value not matched by a case.

default Handles any value not matched by a case. Like the final `else`.

Calculator operator

Implement a basic calculator using switch.

example
Calculator.java
Replay: real traced execution (multi-file project)
public class Calculator {
    public static void main(String[] args) {
        double a = 10;
        double b = 3;
        char op = '/';

        double result;
        boolean valid = true;

        switch (op) {
            case '+':
                result = a + b;
                break;
            case '-':
                result = a - b;
                break;
            case '*':
                result = a * b;
                break;
            case '/':
                if (b != 0) {
                    result = a / b;
                } else {
                    System.out.println("Error: Division by zero");
                    result = 0;
                    valid = false;
                }
                break;
            case '%':
                result = a % b;
                break;
            default:
                System.out.println("Unknown operator: " + op);
                result = 0;
                valid = false;
        }

        if (valid) {
            System.out.println(a + " " + op + " " + b + " = " + result);
        }
    }
}
public class Calculator {
    public static void main(String[] args) {
        double a = 20;
        double b = 3;
        char op = '/';

        double result;
        boolean valid = true;

        switch (op) {
            case '+':
                result = a + b;
                break;
            case '-':
                result = a - b;
                break;
            case '*':
                result = a * b;
                break;
            case '/':
                if (b != 0) {
                    result = a / b;
                } else {
                    System.out.println("Error: Division by zero");
                    result = 0;
                    valid = false;
                }
                break;
            case '%':
                result = a % b;
                break;
            default:
                System.out.println("Unknown operator: " + op);
                result = 0;
                valid = false;
        }

        if (valid) {
            System.out.println(a + " " + op + " " + b + " = " + result);
        }
    }
}
public class Calculator {
    public static void main(String[] args) {
        double a = 15.5;
        double b = 3;
        char op = '/';

        double result;
        boolean valid = true;

        switch (op) {
            case '+':
                result = a + b;
                break;
            case '-':
                result = a - b;
                break;
            case '*':
                result = a * b;
                break;
            case '/':
                if (b != 0) {
                    result = a / b;
                } else {
                    System.out.println("Error: Division by zero");
                    result = 0;
                    valid = false;
                }
                break;
            case '%':
                result = a % b;
                break;
            default:
                System.out.println("Unknown operator: " + op);
                result = 0;
                valid = false;
        }

        if (valid) {
            System.out.println(a + " " + op + " " + b + " = " + result);
        }
    }
}
public class Calculator {
    public static void main(String[] args) {
        double a = 10;
        double b = 2;
        char op = '/';

        double result;
        boolean valid = true;

        switch (op) {
            case '+':
                result = a + b;
                break;
            case '-':
                result = a - b;
                break;
            case '*':
                result = a * b;
                break;
            case '/':
                if (b != 0) {
                    result = a / b;
                } else {
                    System.out.println("Error: Division by zero");
                    result = 0;
                    valid = false;
                }
                break;
            case '%':
                result = a % b;
                break;
            default:
                System.out.println("Unknown operator: " + op);
                result = 0;
                valid = false;
        }

        if (valid) {
            System.out.println(a + " " + op + " " + b + " = " + result);
        }
    }
}
public class Calculator {
    public static void main(String[] args) {
        double a = 10;
        double b = 4;
        char op = '/';

        double result;
        boolean valid = true;

        switch (op) {
            case '+':
                result = a + b;
                break;
            case '-':
                result = a - b;
                break;
            case '*':
                result = a * b;
                break;
            case '/':
                if (b != 0) {
                    result = a / b;
                } else {
                    System.out.println("Error: Division by zero");
                    result = 0;
                    valid = false;
                }
                break;
            case '%':
                result = a % b;
                break;
            default:
                System.out.println("Unknown operator: " + op);
                result = 0;
                valid = false;
        }

        if (valid) {
            System.out.println(a + " " + op + " " + b + " = " + result);
        }
    }
}
public class Calculator {
    public static void main(String[] args) {
        double a = 10;
        double b = 3;
        char op = '+';

        double result;
        boolean valid = true;

        switch (op) {
            case '+':
                result = a + b;
                break;
            case '-':
                result = a - b;
                break;
            case '*':
                result = a * b;
                break;
            case '/':
                if (b != 0) {
                    result = a / b;
                } else {
                    System.out.println("Error: Division by zero");
                    result = 0;
                    valid = false;
                }
                break;
            case '%':
                result = a % b;
                break;
            default:
                System.out.println("Unknown operator: " + op);
                result = 0;
                valid = false;
        }

        if (valid) {
            System.out.println(a + " " + op + " " + b + " = " + result);
        }
    }
}
public class Calculator {
    public static void main(String[] args) {
        double a = 10;
        double b = 3;
        char op = '-';

        double result;
        boolean valid = true;

        switch (op) {
            case '+':
                result = a + b;
                break;
            case '-':
                result = a - b;
                break;
            case '*':
                result = a * b;
                break;
            case '/':
                if (b != 0) {
                    result = a / b;
                } else {
                    System.out.println("Error: Division by zero");
                    result = 0;
                    valid = false;
                }
                break;
            case '%':
                result = a % b;
                break;
            default:
                System.out.println("Unknown operator: " + op);
                result = 0;
                valid = false;
        }

        if (valid) {
            System.out.println(a + " " + op + " " + b + " = " + result);
        }
    }
}
public class Calculator {
    public static void main(String[] args) {
        double a = 10;
        double b = 3;
        char op = '*';

        double result;
        boolean valid = true;

        switch (op) {
            case '+':
                result = a + b;
                break;
            case '-':
                result = a - b;
                break;
            case '*':
                result = a * b;
                break;
            case '/':
                if (b != 0) {
                    result = a / b;
                } else {
                    System.out.println("Error: Division by zero");
                    result = 0;
                    valid = false;
                }
                break;
            case '%':
                result = a % b;
                break;
            default:
                System.out.println("Unknown operator: " + op);
                result = 0;
                valid = false;
        }

        if (valid) {
            System.out.println(a + " " + op + " " + b + " = " + result);
        }
    }
}
public class Calculator {
    public static void main(String[] args) {
        double a = 10;
        double b = 3;
        char op = '%';

        double result;
        boolean valid = true;

        switch (op) {
            case '+':
                result = a + b;
                break;
            case '-':
                result = a - b;
                break;
            case '*':
                result = a * b;
                break;
            case '/':
                if (b != 0) {
                    result = a / b;
                } else {
                    System.out.println("Error: Division by zero");
                    result = 0;
                    valid = false;
                }
                break;
            case '%':
                result = a % b;
                break;
            default:
                System.out.println("Unknown operator: " + op);
                result = 0;
                valid = false;
        }

        if (valid) {
            System.out.println(a + " " + op + " " + b + " = " + result);
        }
    }
}
  1. a ← 10.0, b ← 3.0, op ← /, valid ← true

    1public class Calculator {2    public static void main(String[] args) {3        double a→ 10.0 = 10;   //@a=20, 15.54        double b→ 3.0 = 3;    //@b=4, 25        char op→ / = '/';   //@op='+', '-', '*', '%'6        7        double result;8        boolean valid→ true = true;
  2. switch (op)

    10switch (op/) {11    case '+':12        result = a + b;
  3. result ← 3.3333333333333335

    20case '/':21    if (b3.0 != 0) {22        result→ 3.3333333333333335 = a10.0 / b3.0;23    } else {
  4. break;

    27    }28    break;29case '%':
  5. if (valid)

    38if (validtrue) {39    System.out.println(a10.0 + " " + op/ + " " + b3.0 + " = " + result3.3333333333333335);40}
    output10.0 / 3.0 = 3.3333333333333335
  1. a ← 20.0, b ← 3.0, op ← /, valid ← true

    1public class Calculator {2    public static void main(String[] args) {3        double a→ 20.0 = 20;4        double b→ 3.0 = 3;5        char op→ / = '/';6        7        double result;8        boolean valid→ true = true;
  2. switch (op)

    10switch (op/) {11    case '+':12        result = a + b;
  3. result ← 6.666666666666667

    20case '/':21    if (b3.0 != 0) {22        result→ 6.666666666666667 = a20.0 / b3.0;23    } else {
  4. break;

    27    }28    break;29case '%':
  5. if (valid)

    38if (validtrue) {39    System.out.println(a20.0 + " " + op/ + " " + b3.0 + " = " + result6.666666666666667);40}
    output20.0 / 3.0 = 6.666666666666667
  1. a ← 15.5, b ← 3.0, op ← /, valid ← true

    1public class Calculator {2    public static void main(String[] args) {3        double a→ 15.5 = 15.5;4        double b→ 3.0 = 3;5        char op→ / = '/';6        7        double result;8        boolean valid→ true = true;
  2. switch (op)

    10switch (op/) {11    case '+':12        result = a + b;
  3. result ← 5.166666666666667

    20case '/':21    if (b3.0 != 0) {22        result→ 5.166666666666667 = a15.5 / b3.0;23    } else {
  4. break;

    27    }28    break;29case '%':
  5. if (valid)

    38if (validtrue) {39    System.out.println(a15.5 + " " + op/ + " " + b3.0 + " = " + result5.166666666666667);40}
    output15.5 / 3.0 = 5.166666666666667
  1. a ← 10.0, b ← 2.0, op ← /, valid ← true

    1public class Calculator {2    public static void main(String[] args) {3        double a→ 10.0 = 10;4        double b→ 2.0 = 2;5        char op→ / = '/';6        7        double result;8        boolean valid→ true = true;
  2. switch (op)

    10switch (op/) {11    case '+':12        result = a + b;
  3. result ← 5.0

    20case '/':21    if (b2.0 != 0) {22        result→ 5.0 = a10.0 / b2.0;23    } else {
  4. break;

    27    }28    break;29case '%':
  5. if (valid)

    38if (validtrue) {39    System.out.println(a10.0 + " " + op/ + " " + b2.0 + " = " + result5.0);40}
    output10.0 / 2.0 = 5.0
  1. a ← 10.0, b ← 4.0, op ← /, valid ← true

    1public class Calculator {2    public static void main(String[] args) {3        double a→ 10.0 = 10;4        double b→ 4.0 = 4;5        char op→ / = '/';6        7        double result;8        boolean valid→ true = true;
  2. switch (op)

    10switch (op/) {11    case '+':12        result = a + b;
  3. result ← 2.5

    20case '/':21    if (b4.0 != 0) {22        result→ 2.5 = a10.0 / b4.0;23    } else {
  4. break;

    27    }28    break;29case '%':
  5. if (valid)

    38if (validtrue) {39    System.out.println(a10.0 + " " + op/ + " " + b4.0 + " = " + result2.5);40}
    output10.0 / 4.0 = 2.5
  1. a ← 10.0, b ← 3.0, op ← +, valid ← true

    1public class Calculator {2    public static void main(String[] args) {3        double a→ 10.0 = 10;4        double b→ 3.0 = 3;5        char op→ + = '+';6        7        double result;8        boolean valid→ true = true;
  2. switch (op)

    10switch (op+) {11    case '+':12        result = a + b;
  3. result ← 13.0

    10switch (op) {11    case '+':12        result→ 13.0 = a10.0 + b3.0;13        break;14    case '-':
  4. if (valid)

    38if (validtrue) {39    System.out.println(a10.0 + " " + op+ + " " + b3.0 + " = " + result13.0);40}
    output10.0 + 3.0 = 13.0
  1. a ← 10.0, b ← 3.0, op ← -, valid ← true

    1public class Calculator {2    public static void main(String[] args) {3        double a→ 10.0 = 10;4        double b→ 3.0 = 3;5        char op→ - = '-';6        7        double result;8        boolean valid→ true = true;
  2. switch (op)

    10switch (op-) {11    case '+':12        result = a + b;
  3. result ← 7.0

    13    break;14case '-':15    result→ 7.0 = a10.0 - b3.0;16    break;17case '*':
  4. if (valid)

    38if (validtrue) {39    System.out.println(a10.0 + " " + op- + " " + b3.0 + " = " + result7.0);40}
    output10.0 - 3.0 = 7.0
  1. a ← 10.0, b ← 3.0, op ← *, valid ← true

    1public class Calculator {2    public static void main(String[] args) {3        double a→ 10.0 = 10;4        double b→ 3.0 = 3;5        char op→ * = '*';6        7        double result;8        boolean valid→ true = true;
  2. switch (op)

    10switch (op*) {11    case '+':12        result = a + b;
  3. result ← 30.0

    16    break;17case '*':18    result→ 30.0 = a10.0 * b3.0;19    break;20case '/':
  4. if (valid)

    38if (validtrue) {39    System.out.println(a10.0 + " " + op* + " " + b3.0 + " = " + result30.0);40}
    output10.0 * 3.0 = 30.0
  1. a ← 10.0, b ← 3.0, op ← %, valid ← true

    1public class Calculator {2    public static void main(String[] args) {3        double a→ 10.0 = 10;4        double b→ 3.0 = 3;5        char op→ % = '%';6        7        double result;8        boolean valid→ true = true;
  2. switch (op)

    10switch (op%) {11    case '+':12        result = a + b;
  3. result ← 1.0

    28    break;29case '%':30    result→ 1.0 = a10.0 % b3.0;31    break;32default:
  4. if (valid)

    38if (validtrue) {39    System.out.println(a10.0 + " " + op% + " " + b3.0 + " = " + result1.0);40}
    output10.0 % 3.0 = 1.0

Switch on char works just like switching on int.

Multiple cases, same action

Group cases that should do the same thing.

example
Grouped.java
Replay: real traced execution (multi-file project)
public class Grouped {
    public static void main(String[] args) {
        int month = 4;
        String season;

        switch (month) {
            case 12:
            case 1:
            case 2:
                season = "Winter";
                break;
            case 3:
            case 4:
            case 5:
                season = "Spring";
                break;
            case 6:
            case 7:
            case 8:
                season = "Summer";
                break;
            case 9:
            case 10:
            case 11:
                season = "Autumn";
                break;
            default:
                season = "Invalid month";
        }

        System.out.println("Month " + month + " is in " + season);

        // Another example: weekday vs weekend
        int day = 6;
        switch (day) {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                System.out.println("Day " + day + ": Weekday");
                break;
            case 6:
            case 7:
                System.out.println("Day " + day + ": Weekend!");
                break;
        }
    }
}
public class Grouped {
    public static void main(String[] args) {
        int month = 1;
        String season;

        switch (month) {
            case 12:
            case 1:
            case 2:
                season = "Winter";
                break;
            case 3:
            case 4:
            case 5:
                season = "Spring";
                break;
            case 6:
            case 7:
            case 8:
                season = "Summer";
                break;
            case 9:
            case 10:
            case 11:
                season = "Autumn";
                break;
            default:
                season = "Invalid month";
        }

        System.out.println("Month " + month + " is in " + season);

        // Another example: weekday vs weekend
        int day = 6;
        switch (day) {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                System.out.println("Day " + day + ": Weekday");
                break;
            case 6:
            case 7:
                System.out.println("Day " + day + ": Weekend!");
                break;
        }
    }
}
public class Grouped {
    public static void main(String[] args) {
        int month = 6;
        String season;

        switch (month) {
            case 12:
            case 1:
            case 2:
                season = "Winter";
                break;
            case 3:
            case 4:
            case 5:
                season = "Spring";
                break;
            case 6:
            case 7:
            case 8:
                season = "Summer";
                break;
            case 9:
            case 10:
            case 11:
                season = "Autumn";
                break;
            default:
                season = "Invalid month";
        }

        System.out.println("Month " + month + " is in " + season);

        // Another example: weekday vs weekend
        int day = 6;
        switch (day) {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                System.out.println("Day " + day + ": Weekday");
                break;
            case 6:
            case 7:
                System.out.println("Day " + day + ": Weekend!");
                break;
        }
    }
}
public class Grouped {
    public static void main(String[] args) {
        int month = 11;
        String season;

        switch (month) {
            case 12:
            case 1:
            case 2:
                season = "Winter";
                break;
            case 3:
            case 4:
            case 5:
                season = "Spring";
                break;
            case 6:
            case 7:
            case 8:
                season = "Summer";
                break;
            case 9:
            case 10:
            case 11:
                season = "Autumn";
                break;
            default:
                season = "Invalid month";
        }

        System.out.println("Month " + month + " is in " + season);

        // Another example: weekday vs weekend
        int day = 6;
        switch (day) {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                System.out.println("Day " + day + ": Weekday");
                break;
            case 6:
            case 7:
                System.out.println("Day " + day + ": Weekend!");
                break;
        }
    }
}
public class Grouped {
    public static void main(String[] args) {
        int month = 4;
        String season;

        switch (month) {
            case 12:
            case 1:
            case 2:
                season = "Winter";
                break;
            case 3:
            case 4:
            case 5:
                season = "Spring";
                break;
            case 6:
            case 7:
            case 8:
                season = "Summer";
                break;
            case 9:
            case 10:
            case 11:
                season = "Autumn";
                break;
            default:
                season = "Invalid month";
        }

        System.out.println("Month " + month + " is in " + season);

        // Another example: weekday vs weekend
        int day = 1;
        switch (day) {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                System.out.println("Day " + day + ": Weekday");
                break;
            case 6:
            case 7:
                System.out.println("Day " + day + ": Weekend!");
                break;
        }
    }
}
public class Grouped {
    public static void main(String[] args) {
        int month = 4;
        String season;

        switch (month) {
            case 12:
            case 1:
            case 2:
                season = "Winter";
                break;
            case 3:
            case 4:
            case 5:
                season = "Spring";
                break;
            case 6:
            case 7:
            case 8:
                season = "Summer";
                break;
            case 9:
            case 10:
            case 11:
                season = "Autumn";
                break;
            default:
                season = "Invalid month";
        }

        System.out.println("Month " + month + " is in " + season);

        // Another example: weekday vs weekend
        int day = 3;
        switch (day) {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                System.out.println("Day " + day + ": Weekday");
                break;
            case 6:
            case 7:
                System.out.println("Day " + day + ": Weekend!");
                break;
        }
    }
}
public class Grouped {
    public static void main(String[] args) {
        int month = 4;
        String season;

        switch (month) {
            case 12:
            case 1:
            case 2:
                season = "Winter";
                break;
            case 3:
            case 4:
            case 5:
                season = "Spring";
                break;
            case 6:
            case 7:
            case 8:
                season = "Summer";
                break;
            case 9:
            case 10:
            case 11:
                season = "Autumn";
                break;
            default:
                season = "Invalid month";
        }

        System.out.println("Month " + month + " is in " + season);

        // Another example: weekday vs weekend
        int day = 7;
        switch (day) {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                System.out.println("Day " + day + ": Weekday");
                break;
            case 6:
            case 7:
                System.out.println("Day " + day + ": Weekend!");
                break;
        }
    }
}
  1. month ← 4

    1public class Grouped {2    public static void main(String[] args) {3        int month→ 4 = 4;  //@month=1, 6, 114        String season;
  2. switch (month)

    6switch (month4) {7    case 12:8    case 1:
  3. season ← Spring

    13case 4:14case 5:15    season→ Spring = "Spring";16    break;17case 6:
  4. day ← 6

    31System.out.println("Month " + month4 + " is in " + seasonSpring);3233// Another example: weekday vs weekend34int day→ 6 = 6;  //@day=1, 3, 735switch (day) {
    outputMonth 4 is in Spring
  5. switch (day)

    34int day = 6;  //@day=1, 3, 735switch (day6) {36    case 1:37    case 2:
  6. case 7:

    43    case 6:44    case 7:45        System.out.println("Day " + day6 + ": Weekend!");46        break;47}
    outputDay 6: Weekend!
  1. month ← 1

    1public class Grouped {2    public static void main(String[] args) {3        int month→ 1 = 1;4        String season;
  2. switch (month)

    6switch (month1) {7    case 12:8    case 1:
  3. season ← Winter

    8case 1:9case 2:10    season→ Winter = "Winter";11    break;12case 3:
  4. day ← 6

    31System.out.println("Month " + month1 + " is in " + seasonWinter);3233// Another example: weekday vs weekend34int day→ 6 = 6;35switch (day) {
    outputMonth 1 is in Winter
  5. switch (day)

    34int day = 6;35switch (day6) {36    case 1:37    case 2:
  6. case 7:

    43    case 6:44    case 7:45        System.out.println("Day " + day6 + ": Weekend!");46        break;47}
    outputDay 6: Weekend!
  1. month ← 6

    1public class Grouped {2    public static void main(String[] args) {3        int month→ 6 = 6;4        String season;
  2. switch (month)

    6switch (month6) {7    case 12:8    case 1:
  3. season ← Summer

    18case 7:19case 8:20    season→ Summer = "Summer";21    break;22case 9:
  4. day ← 6

    31System.out.println("Month " + month6 + " is in " + seasonSummer);3233// Another example: weekday vs weekend34int day→ 6 = 6;35switch (day) {
    outputMonth 6 is in Summer
  5. switch (day)

    34int day = 6;35switch (day6) {36    case 1:37    case 2:
  6. case 7:

    43    case 6:44    case 7:45        System.out.println("Day " + day6 + ": Weekend!");46        break;47}
    outputDay 6: Weekend!
  1. month ← 11

    1public class Grouped {2    public static void main(String[] args) {3        int month→ 11 = 11;4        String season;
  2. switch (month)

    6switch (month11) {7    case 12:8    case 1:
  3. season ← Autumn

    23case 10:24case 11:25    season→ Autumn = "Autumn";26    break;27default:
  4. day ← 6

    31System.out.println("Month " + month11 + " is in " + seasonAutumn);3233// Another example: weekday vs weekend34int day→ 6 = 6;35switch (day) {
    outputMonth 11 is in Autumn
  5. switch (day)

    34int day = 6;35switch (day6) {36    case 1:37    case 2:
  6. case 7:

    43    case 6:44    case 7:45        System.out.println("Day " + day6 + ": Weekend!");46        break;47}
    outputDay 6: Weekend!
  1. month ← 4

    1public class Grouped {2    public static void main(String[] args) {3        int month→ 4 = 4;4        String season;
  2. switch (month)

    6switch (month4) {7    case 12:8    case 1:
  3. season ← Spring

    13case 4:14case 5:15    season→ Spring = "Spring";16    break;17case 6:
  4. day ← 1

    31System.out.println("Month " + month4 + " is in " + seasonSpring);3233// Another example: weekday vs weekend34int day→ 1 = 1;35switch (day) {
    outputMonth 4 is in Spring
  5. switch (day)

    34int day = 1;35switch (day1) {36    case 1:37    case 2:
  6. case 5:

    39case 4:40case 5:41    System.out.println("Day " + day1 + ": Weekday");42    break;43case 6:
    outputDay 1: Weekday
  1. month ← 4

    1public class Grouped {2    public static void main(String[] args) {3        int month→ 4 = 4;4        String season;
  2. switch (month)

    6switch (month4) {7    case 12:8    case 1:
  3. season ← Spring

    13case 4:14case 5:15    season→ Spring = "Spring";16    break;17case 6:
  4. day ← 3

    31System.out.println("Month " + month4 + " is in " + seasonSpring);3233// Another example: weekday vs weekend34int day→ 3 = 3;35switch (day) {
    outputMonth 4 is in Spring
  5. switch (day)

    34int day = 3;35switch (day3) {36    case 1:37    case 2:
  6. case 5:

    39case 4:40case 5:41    System.out.println("Day " + day3 + ": Weekday");42    break;43case 6:
    outputDay 3: Weekday
  1. month ← 4

    1public class Grouped {2    public static void main(String[] args) {3        int month→ 4 = 4;4        String season;
  2. switch (month)

    6switch (month4) {7    case 12:8    case 1:
  3. season ← Spring

    13case 4:14case 5:15    season→ Spring = "Spring";16    break;17case 6:
  4. day ← 7

    31System.out.println("Month " + month4 + " is in " + seasonSpring);3233// Another example: weekday vs weekend34int day→ 7 = 7;35switch (day) {
    outputMonth 4 is in Spring
  5. switch (day)

    34int day = 7;35switch (day7) {36    case 1:37    case 2:
  6. case 7:

    43    case 6:44    case 7:45        System.out.println("Day " + day7 + ": Weekend!");46        break;47}
    outputDay 7: Weekend!

Fall-through (no break) lets multiple cases share code.

Exercise: EnhancedSwitch.java

Explore enhanced switch (Java 14+): arrow syntax, no fall-through, returns values