Java switch Statement



In this tutorial, we will learn how to use the switch statement in Java to control program execution flow with the help of examples.


Java switch Statement

The switch statement is used to select one of many code blocks to be executed.

The syntax of the switch statement can be given as follows:

switch (expression) {

    case value_1:
        // code
        break;

    case value_2:
        // code
        break;

    ...
    ...
    default:
        //  default statements
}

How does the switch statement work?

  • The switch expression is evaluated once.
  • The expression value is compared with the values of each case.
  • If expression is matched with one of the values, the associated block of code is executed.
  • If there is no match, the default case code is executed.

Example: Java switch Statement

In the following example, we will see how to use the switch statement to calculate the weekday name by passing the weekday number.

public class Main {
    public static void main(String[] args) {

        int numDay = 7;

        switch(numDay) {
            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:

Sunday

In the above program, we have used the switch statement to return the weekday name corresponding to the day number. Here, we have a variable numDay. The variable is compared with the value of each statement.

Since the value matches with 7, the code of case 7 is executed.

System.out.println("Sunday");
break;

Flowchart of switch Statement

The following illustration shows the flowchart of the Java switch statement.

Flowchart of the Java switch statement

The break keyword in Java switch-case

In the example above, we used break in each case block.

...
    case 4:
        System.out.println("Thursday");
        break;
...

The break keyword is used to terminate the switch-case statement.

If the break keyword is not used, all the cases after the matching case are also executed. For example,

public class Main {
    public static void main(String[] args) {

        int numDay = 4;

        switch(numDay) {
            case 1:
                System.out.println("Monday");
            case 2:
                System.out.println("Tuesday");
            case 3:
                System.out.println("Wednesday");

            //  matching case
            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:

Thursday
Friday
Saturday
Sunday

In the above, the numDay matches with case 4. Here, we haven't used the break statement after each case. So, all the cases after case 4 are executed.

The break keyword is needed to be used to terminate the switch-case statement.

Note: Using a break keyword can save a lot of execution time because it ignores the execution of all the rest of the code in the switch block.


The default keyword in Java switch-case

The switch statement offers an optional default case.

The default keyword specifies code to run if the expression doesn't match any of the cases. For example,

public class Main {
    public static void main(String[] args) {

        int numDay = 10;

        switch(numDay) {
            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 case
            default: 
                System.out.println("Unknown Day");
        }
    }
}

Output:

Unknown Day

In the program above, the value of the numDay doesn't match any of the cases. So, the code inside the default case is executed.

default: 
    System.out.println("Unknown Day");


ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.