Java switch Statement

In this lesson of our Java course, we will learn about the Java switch statement. The Java Switch Statement is a control statement that allows a programmer to control the flow of a program based on multiple conditions. It is like the if-else statement, but it is more concise and efficient when there are multiple conditions that need to be evaluated.

The switch statement evaluates an expression and then compares its value to the values of each case. When a match is found, the code block associated with that case is executed. If no match is found, the default case, if present, is executed.

1. Java switch Statement

The switch statement in Java is used to check if a condition under the case matches the statements inside the case block will be executed.

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

The expression is evaluated, and its value is compared to the values of each case. If a match is found, the code block associated with that case is executed, and the program jumps out of the switch statement. If no match is found, the default case, if present, is executed.

The break statement is used to exit the switch statement and prevent the execution of any subsequent cases. If the break statement is not used, the program will continue to execute the code block of the next case that matches the value of the expression.

2. How Java switch Statement Works?

The flowchart of a Java Switch Statement is like that of an if-else statement. The expression is evaluated first, and then the value is compared to the values of each case. If a match is found, the code block associated with that case is executed, and the program jumps out of the switch statement. If no match is found, the default case, if present, is executed.

Java switch statement
How Java switch statement works?

2.1. switch statement Example

public class JavaSwitchStatement {
    public static void main(String[] args) {
        int day = 2;
        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");
                break;
        }
    }
}

In this example, the value of the variable day is 2. The switch statement evaluates the value of the variable and compares it to the values of each case. When a match is found, the code block associated with that case is executed, and the program jumps out of the switch statement. Here, the output will be “Tuesday”.

It’s worth noting that the java switch statement only works with certain data types, including int, short, byte, char, and enum. Starting with Java 14, it also works with the type String.

Let’s look at another example for better understanding.

import java.time.DayOfWeek;

public class SwitchExample {
    public static void main(String[] args) {
        DayOfWeek today = DayOfWeek.SATURDAY;
        switch (today) {
            case MONDAY:
                System.out.println("Today is Monday, time to go back to work.");
                break;
            case TUESDAY:
            case WEDNESDAY:
            case THURSDAY:
                System.out.println("Today is a weekday, time to work.");
                break;
            case FRIDAY:
                System.out.println("Today is Friday, time to finish up and relax.");
                break;
            case SATURDAY:
            case SUNDAY:
                System.out.println("Today is the weekend, time to have fun!");
                break;
            default:
                System.out.println("Invalid day of the week.");
                break;
        }
    }
}

In this example, the variable today is set to DayOfWeek.SATURDAY and then passed to the switch statement. The switch statement compares today to the various cases and runs the corresponding block of code when a match is found.

The break statement is used to exit the switch statement after a match is found, preventing any further cases from being executed. The default case is optional, but is usually used to handle unexpected input or to provide a default behavior when no other case matches.

Summary

Java Switch Statement is a useful control statement that allows a programmer to control the flow of a program based on multiple conditions. It is more concise and efficient than the if-else statement when there are multiple conditions that need to be evaluated. As always, the source code for this course is available on our Github repository.