Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java Switch Statement
Multi-Way Decisions



Switch Statement in Java

When you're faced with multiple paths of logic based on the value of a single variable, the switch statement in Java shines as a clean, readable alternative to a long series of if-else-if blocks. It helps you route your program's behavior based on specific values—like menu choices, user input, or predefined constants.

Why Use a Switch Statement?

Imagine choosing what to eat at a food court. Based on the stall number, you want to print out the food item. You can write:

if (stall == 1) {
    System.out.println("Pizza");
} else if (stall == 2) {
    System.out.println("Burger");
} else if (stall == 3) {
    System.out.println("Sushi");
} else {
    System.out.println("Option not available");
}

Or you could write this more cleanly using switch:

switch (stall) {
    case 1:
        System.out.println("Pizza");
        break;
    case 2:
        System.out.println("Burger");
        break;
    case 3:
        System.out.println("Sushi");
        break;
    default:
        System.out.println("Option not available");
}
Pizza

Java Switch Statement Syntax

switch (expression) {
    case value1:
        // code block
        break;
    case value2:
        // code block
        break;
    ...
    default:
        // default code block
}

Key Points:

Basic Example with Integers

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

Fall-Through Behavior

If you omit break, Java continues to execute the next statements—even if the case matched!

int choice = 2;
switch (choice) {
    case 1:
        System.out.println("First");
    case 2:
        System.out.println("Second");
    case 3:
        System.out.println("Third");
}
Second
Third

This is known as fall-through, and while sometimes useful, it can also lead to bugs if not handled carefully.

Switch with Strings (Java 7+)

String fruit = "Apple";
switch (fruit) {
    case "Apple":
        System.out.println("Red fruit");
        break;
    case "Banana":
        System.out.println("Yellow fruit");
        break;
    default:
        System.out.println("Unknown fruit");
}
Red fruit

Switch Expressions (Java 14+)

Modern Java versions allow switch expressions that return values:

String result = switch (day) {
    case 1 -> "Monday";
    case 2 -> "Tuesday";
    case 3 -> "Wednesday";
    default -> "Invalid";
};
System.out.println(result);
Wednesday

Nested Switch Statements

int section = 1;
int shelf = 2;
switch (section) {
    case 1:
        switch (shelf) {
            case 1:
                System.out.println("Section 1 - Shelf 1");
                break;
            case 2:
                System.out.println("Section 1 - Shelf 2");
                break;
        }
        break;
    case 2:
        System.out.println("Section 2");
        break;
}
Section 1 - Shelf 2

Best Practices and Gotchas

Real-Life Example: Menu Selection

Scanner sc = new Scanner(System.in);
System.out.println("Enter your choice: 1. Start 2. Stop 3. Exit");
int input = sc.nextInt();

switch (input) {
    case 1 -> System.out.println("System Started...");
    case 2 -> System.out.println("System Stopped.");
    case 3 -> System.out.println("Exiting System.");
    default -> System.out.println("Invalid choice.");
}
System Started...

QUIZ

Question 1:What will be the output of the following code?

int choice = 2;
switch (choice) {
    case 1:
        System.out.println("First");
    case 2:
        System.out.println("Second");
    case 3:
        System.out.println("Third");
}

Question 2:The switch expression in Java can accept float or double types.

Question 3:In the following code, which value of fruit will trigger the default case?

String fruit = "Mango";
switch (fruit) {
    case "Apple":
        System.out.println("Red fruit");
        break;
    case "Banana":
        System.out.println("Yellow fruit");
        break;
    default:
        System.out.println("Unknown fruit");
}

Question 4:Which of the following statements about Java switch are correct?

Question 5:Switch statements are generally better than if-else chains when evaluating multiple discrete values.

Question 6:What will the following code print?

int section = 1;
int shelf = 2;
switch (section) {
    case 1:
        switch (shelf) {
            case 1:
                System.out.println("Section 1 - Shelf 1");
                break;
            case 2:
                System.out.println("Section 1 - Shelf 2");
                break;
        }
        break;
    case 2:
        System.out.println("Section 2");
        break;
}



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M