Loading
The switch statement in Java is a smart way to handle multiple choices based on the value of a single variable or expression. It helps you write cleaner code when you have many possible options to check, such as days of the week, menu selections, or grades.


How Does the Switch Statement Work?

  • The switch statement checks the value of an expression.
  • It compares this value with each case.
  • When a match is found, the code inside that case runs.
  • The break keyword stops the execution of the switch after a case is executed.
  • The default block runs if none of the cases match.


Syntax:

switch (expression) {
    case value1:
        // Code block for value1
        break;
    case value2:
        // Code block for value2
        break;
    // ... more cases ...
    default:
        // Code block if no case matches
}


Example: Days of the Week

public class SwitchCase {
    public static void main(String[] args) {
        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;
            case 4:
                System.out.println("Thursday");
                break;
            case 5:
                System.out.println("Friday");
                break;
            case 6:
                System.out.println("Saturday");
                break;
            case 7:
                System.out.println("Sunday");
                break;
        }
    }
}


Output:

Wednesday


Why Use break in Switch?

The break keyword is important. Without it, Java will keep running all the following cases, even if they don’t match. This is called "fall-through."


Example: Without break

public class SwitchCase {
    public static void main(String[] args) {
        int day = 1;
        switch (day) {
            case 1:
                System.out.println("Monday");
            case 2:
                System.out.println("Tuesday");
            case 3:
                System.out.println("Wednesday");
            case 4:
                System.out.println("Thursday");
            case 5:
                System.out.println("Friday");
            case 6:
                System.out.println("Saturday");
            case 7:
                System.out.println("Sunday");
        }
    }
}

Output:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday


Example: With break at a Specific Point

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


Output:

Monday
Tuesday
Wednesday
Thursday

Execution stops after "Thursday" due to the break statement.


Using the default Case

The default case runs when none of the cases match the expression. It’s a good practice to include it for handling unexpected values.


Example:

public class SwitchCase {
    public static void main(String[] args) {
        int day = 10;
        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            case 4:
                System.out.println("Thursday");
                break;
            case 5:
                System.out.println("Friday");
                break;
            case 6:
                System.out.println("Saturday");
                break;
            case 7:
                System.out.println("Sunday");
                break;
            default:
                System.out.println("Enter valid input!!");
        }
    }
}


Output:

Enter valid input!!


Key Points

  • Use switch for multiple-choice decisions based on a single value.
  • Always use break to prevent unwanted execution of further cases.
  • The default block is optional but helpful for handling invalid or unexpected values.
  • switch works with int, char, String, and enum types.


Tips: Switch statements make your code more readable and efficient when you have many possible values to check for a single variable.