Skip to content

3.2. Switch Statements

Switch statements in Java provide an alternative to using multiple if-else-if statements. They are particularly useful when you need to compare a single variable against multiple constant values.

Syntax of Switch Statements

The basic syntax of a switch statement is:

java
switch (expression) {
    case value1:
        // Code to execute if expression equals value1
        break;
    case value2:
        // Code to execute if expression equals value2
        break;
    // Add more cases as needed
    default:
        // Code to execute if no case matches
}

Key Points:

  1. The expression must evaluate to a value of type byte, short, int, char, String, or an enum.
  2. Each case represents a possible value for the expression.
  3. The break statement prevents fall-through to the next case.
  4. The default case is optional and executes if no other case matches.

Example:

java
int day = 3;

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");
}

In this example, the output will be Wednesday because day equals 3.

Fall-Through Behavior

If you omit the break statement, execution will continue into the next case. This is called "fall-through."

Example:

java
int number = 2;

switch (number) {
    case 1:
        System.out.println("One");
    case 2:
        System.out.println("Two");
    case 3:
        System.out.println("Three");
    default:
        System.out.println("Default");
}

Output:

Two
Three
Default

To avoid fall-through, always include break unless intentional.

Using Strings in Switch Statements

Java allows String values in switch statements.

Example:

java
String fruit = "Apple";

switch (fruit) {
    case "Apple":
        System.out.println("You chose Apple.");
        break;
    case "Banana":
        System.out.println("You chose Banana.");
        break;
    default:
        System.out.println("Unknown fruit.");
}

Switch with Enums

Switch statements work seamlessly with enum types.

Example:

java
enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

Day today = Day.WEDNESDAY;

switch (today) {
    case MONDAY:
        System.out.println("Start of the work week.");
        break;
    case FRIDAY:
        System.out.println("Almost the weekend!");
        break;
    case SATURDAY:
    case SUNDAY:
        System.out.println("Weekend!");
        break;
    default:
        System.out.println("Midweek day.");
}

Common Pitfalls and Best Practices

  1. Forgetting break: Always use break unless you want fall-through behavior.
  2. Default Case: Include a default case to handle unexpected values.
  3. Complex Logic: Avoid using switch for complex conditions; use if-else instead.
  4. Readability: Use enums or constants for better readability.

Example Code Snippet

java
public class SwitchExamples {
    public static void main(String[] args) {
        int month = 7;

        switch (month) {
            case 1:
                System.out.println("January");
                break;
            case 2:
                System.out.println("February");
                break;
            case 3:
                System.out.println("March");
                break;
            case 4:
                System.out.println("April");
                break;
            case 5:
                System.out.println("May");
                break;
            case 6:
                System.out.println("June");
                break;
            case 7:
                System.out.println("July");
                break;
            case 8:
                System.out.println("August");
                break;
            case 9:
                System.out.println("September");
                break;
            case 10:
                System.out.println("October");
                break;
            case 11:
                System.out.println("November");
                break;
            case 12:
                System.out.println("December");
                break;
            default:
                System.out.println("Invalid month");
        }
    }
}

Switch statements are a powerful tool for simplifying code when dealing with multiple discrete values.