Java Switch

Selecting from multiple options in Java

🔄 What is Java Switch?

Java Switch statement allows you to choose from multiple options based on a variable's value. It's a cleaner alternative to multiple if-else statements when comparing one variable.


// Simple switch statement
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("Other day");
}
                                    

Output:

Wednesday

Switch Statement Components

🔍

Switch Expression

Variable to compare against cases

switch (variable) {
    // cases here
}
📋

Case Labels

Possible values to match

case 1:
    System.out.println("One");
    break;
🛑

Break Statement

Exit the switch block

case 2:
    System.out.println("Two");
    break; // Important!
🎯

Default Case

Fallback when no case matches

default:
    System.out.println("Other");
    break;

🔹 Basic Switch Example

Using switch to handle different day numbers:

public class SwitchExample {
    public static void main(String[] args) {
        int dayNumber = 5;
        String dayName;
        
        switch (dayNumber) {
            case 1:
                dayName = "Monday";
                break;
            case 2:
                dayName = "Tuesday";
                break;
            case 3:
                dayName = "Wednesday";
                break;
            case 4:
                dayName = "Thursday";
                break;
            case 5:
                dayName = "Friday";
                break;
            case 6:
                dayName = "Saturday";
                break;
            case 7:
                dayName = "Sunday";
                break;
            default:
                dayName = "Invalid day";
                break;
        }
        
        System.out.println("Day " + dayNumber + " is " + dayName);
    }
}

Output:

Day 5 is Friday

🔹 Switch with Characters

Switch statements can work with characters too:

public class CharacterSwitch {
    public static void main(String[] args) {
        char grade = 'B';
        
        switch (grade) {
            case 'A':
                System.out.println("Excellent! Score: 90-100");
                break;
            case 'B':
                System.out.println("Good! Score: 80-89");
                break;
            case 'C':
                System.out.println("Average! Score: 70-79");
                break;
            case 'D':
                System.out.println("Below Average! Score: 60-69");
                break;
            case 'F':
                System.out.println("Failed! Score: Below 60");
                break;
            default:
                System.out.println("Invalid grade");
                break;
        }
    }
}

Output:

Good! Score: 80-89

🔹 Switch with Strings

Modern Java allows switch with String values:

public class StringSwitch {
    public static void main(String[] args) {
        String month = "January";
        int days;
        
        switch (month) {
            case "January":
            case "March":
            case "May":
            case "July":
            case "August":
            case "October":
            case "December":
                days = 31;
                break;
            case "April":
            case "June":
            case "September":
            case "November":
                days = 30;
                break;
            case "February":
                days = 28; // Not considering leap year
                break;
            default:
                days = 0;
                System.out.println("Invalid month");
                break;
        }
        
        if (days > 0) {
            System.out.println(month + " has " + days + " days");
        }
    }
}

Output:

January has 31 days

🔹 Fall-through Behavior

What happens when you forget the break statement:

public class FallThrough {
    public static void main(String[] args) {
        int number = 2;
        
        System.out.println("Without break statements:");
        switch (number) {
            case 1:
                System.out.println("One");
                // No break - falls through
            case 2:
                System.out.println("Two");
                // No break - falls through
            case 3:
                System.out.println("Three");
                break; // Finally stops here
            default:
                System.out.println("Other");
        }
        
        System.out.println("\nWith proper break statements:");
        switch (number) {
            case 1:
                System.out.println("One");
                break;
            case 2:
                System.out.println("Two");
                break;
            case 3:
                System.out.println("Three");
                break;
            default:
                System.out.println("Other");
        }
    }
}

Output:

Without break statements:

Two

Three

With proper break statements:

Two

🧠 Test Your Knowledge

What happens if you forget the 'break' statement in a switch case?