Chapter 17 of 77

Switch Statement in Java

Sometimes we need to make a decision based on the value of a single variable. For example, imagine we have a menu where 1 means "Start", 2 means "Settings", and 3 means "Exit". We could use several if-else statements for this, but Java provides a cleaner option called the switch statement.

A switch statement allows us to compare one value with multiple possible values and execute the code that matches.

Basic Syntax

The basic structure of a switch statement looks like this:

switch (value) {
    case value1:
        // code
        break;

    case value2:
        // code
        break;

    default:
        // code
}

The switch expression is the value we want to check. Each case represents a possible value. When Java finds a matching case, it executes the code inside it.

The break statement is used to stop the switch after the matching case has been executed. The default block is used when none of the cases match.

A Simple Example

Let's say we have a number representing a day:

int day = 2;

switch (day) {
    case 1:
        System.out.println("Monday");
        break;

    case 2:
        System.out.println("Tuesday");
        break;

    case 3:
        System.out.println("Wednesday");
        break;

    default:
        System.out.println("Invalid day");
}

The value of day is 2, so Java finds case 2 and prints:

Tuesday

Why Do We Need break?

The break statement is important because it tells Java to exit the switch statement after executing a matching case.

For example:

int day = 2;

switch (day) {
    case 1:
        System.out.println("Monday");
        break;

    case 2:
        System.out.println("Tuesday");
        break;

    case 3:
        System.out.println("Wednesday");
        break;
}

When Java finds case 2, it prints Tuesday and then encounters break, so the switch ends.

If we don't use break in a traditional switch, Java can continue executing the following cases even though they don't match. This behavior is called fall-through.

For example:

int number = 1;

switch (number) {
    case 1:
        System.out.println("One");

    case 2:
        System.out.println("Two");

    case 3:
        System.out.println("Three");
}

Output:

One
Two
Three

This happens because there are no break statements, so Java continues into the following cases.

The default Case

The default case is executed when none of the cases match.

For example:

int day = 8;

switch (day) {
    case 1:
        System.out.println("Monday");
        break;

    case 2:
        System.out.println("Tuesday");
        break;

    case 3:
        System.out.println("Wednesday");
        break;

    default:
        System.out.println("Invalid day");
}

Since there is no case for 8, Java executes default:

Invalid day

The default case is optional, but it is often useful for handling unexpected values.

Multiple Cases with the Same Code

Sometimes multiple values should perform the same action.

For example, suppose we want Saturday and Sunday to both display "Weekend".

We can write:

int day = 6;

switch (day) {
    case 6:
    case 7:
        System.out.println("Weekend");
        break;

    default:
        System.out.println("Weekday");
}

If day is 6 or 7, the program prints:

Weekend

This is useful when several choices should lead to the same result.

Switch with Strings

A switch isn't limited to numbers. We can also use it with String values.

For example:

String fruit = "Apple";

switch (fruit) {
    case "Apple":
        System.out.println("You selected Apple.");
        break;

    case "Banana":
        System.out.println("You selected Banana.");
        break;

    case "Orange":
        System.out.println("You selected Orange.");
        break;

    default:
        System.out.println("Unknown fruit.");
}

Output:

You selected Apple.

This can be useful when a program needs to respond to different text choices.

A classic switch works with byte, short, char, int, their wrapper classes (Byte, Short, Character, Integer), String, and enum values. It does not work with long, float, double, or boolean. Also, each case label must be a constant value, and two cases cannot have the same value.

Switch vs if-else

Both switch and if-else can be used to make decisions, but they are useful in slightly different situations.

If you are checking specific values, switch can make the code cleaner:

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

But if you need to check ranges or complex conditions, if-else is usually more suitable:

if (marks >= 90) {
    System.out.println("A+");
} else if (marks >= 80) {
    System.out.println("A");
}

So, a simple rule is:

Specific values → switch
Conditions and ranges → if-else

A Real-World Example

Let's create a simple menu:

int choice = 2;

switch (choice) {
    case 1:
        System.out.println("Starting the game...");
        break;

    case 2:
        System.out.println("Opening settings...");
        break;

    case 3:
        System.out.println("Exiting the game...");
        break;

    default:
        System.out.println("Invalid choice.");
}

Since choice is 2, the output is:

Opening settings...

This is the basic idea behind a switch statement. It is especially useful when you have one value and several specific choices that need different actions.

Modern Switch (Java 14 and Later)

Since Java 14, Java also supports a shorter arrow form of switch. With case ... ->, there is no fall-through, so you don't need break, and you can list several values in one case separated by commas:

int day = 6;

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

Output:

Weekend

A switch can also be used as an expression that produces a value. This is called a switch expression:

int dayNumber = 3;

String dayName = switch (dayNumber) {
    case 1 -> "Monday";
    case 2 -> "Tuesday";
    case 3 -> "Wednesday";
    default -> "Unknown";
};

System.out.println(dayName);

Output:

Wednesday

Notice the semicolon after the closing brace, because the whole switch is part of an assignment statement. A switch expression must cover every possible value, which is why default is needed here. If a case needs more than one statement, put them in a block and use yield to give back the value:

case 7 -> {
    System.out.println("Good work!");
    yield "B";
}

You will still see the classic case: / break style in a lot of existing code, so it is important to understand both forms.