switch statement in Java with examples

The "switch" statement lets developers write code that reacts differently depending on an expression's value.

In other words, we can say that a "switch" statement in Java is a control flow clause that enables you to carry out various operations depending on the value of a variable or expression. It offers a substitute for using numerous if-else statements.

Java switch statement syntax

The following is the general form of the "switch" statement in Java.

switch (expression) {
    case value1:
        // code to execute if "expression" evaluates to "value1"
        break;
    case value2:
        // code to execute if "expression" evaluates to "value2"
        break;
    case value3:
        // code to execute if "expression" evaluates to "value3"
        break;
    :
    default:
        // code to execute if "expression" does not match any of the values
}

Each "case" label is compared to the "expression" after it has been evaluated once. If the expression matches a case label, the code block immediately following that label is executed until the "break" statement is reached, at which point the switch statement is terminated. If there is no match, the code block following the "default" label (if it exists) is executed.

Java switch statement example

Consider the following Java code as an example demonstrating the "switch" statement:

Java Code
public class JavaProgram {
    public static void main(String args[]) {
        
        int i;
        
        for (i = 0; i < 10; i++) {
            switch (i) {
                case 0:
                    System.out.println("i is zero.");
                    break;
                case 1:
                    System.out.println("i is one.");
                    break;
                case 2:
                    System.out.println("i is two.");
                    break;
                case 3:
                    System.out.println("i is three.");
                    break;
                case 4:
                    System.out.println("i is four.");
                    break;
                case 5:
                    System.out.println("i is five.");
                    break;
                default:
                    System.out.println("i is greater than 5.");
            }
        }
    }
}
Output
i is zero.
i is one.
i is two.
i is three.
i is four.
i is five.
i is greater than 5.
i is greater than 5.
i is greater than 5.
i is greater than 5.

This Java program employs a "switch" statement to determine the value of the variable "i" and run various blocks of code in accordance with that value. Java's "switch" control flow statement enables the program to run various code blocks depending on the result of an expression.

The expression being evaluated in this program is the variable "i." There are several cases in the "switch" statement, each of which corresponds to a different value of "i." If the value of "i" corresponds to a case, the code contained within that case is executed. If none of the cases correspond to the value of "i" the "default" case is executed.

When the value of "i" is zero, one, two, three, four, or five. The code block associated with these case values is run. In contrast, the code associated with "default" is executed in other values of "i."

As you can see, each time through the loop, the statements associated with the case constant that matches i are executed. All the others are bypassed. After i is greater than 5, no case statements match, so the default statement is executed.

The "break" statement is not required. If the "break" is omitted, execution will proceed to the next "case." It is sometimes desirable to have multiple cases with no "break" statements in between. Take, for example, the following program:

Java Code
public class JavaProgram {
    public static void main(String args[]) {
        
        int i;
        
        for (i = 0; i < 15; i++) {
            switch (i) {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                    System.out.println("i is less than 5.");
                    break;
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    System.out.println("i is less than 10.");
                    break;
                default:
                    System.out.println("i is 10 or more.");
            }
        }
    }
}
Output
i is less than 5.
i is less than 5.
i is less than 5.
i is less than 5.
i is less than 5.
i is less than 10.
i is less than 10.
i is less than 10.
i is less than 10.
i is less than 10.
i is 10 or more.
i is 10 or more.
i is 10 or more.
i is 10 or more.
i is 10 or more.

The Java program iterates from 0 to 14, and a "switch" statement is used to evaluate the value of "i" during each iteration. The "switch" statement will execute different blocks of code and print different messages to the console depending on the value of "i."

Advantages of the "switch" statement in Java

Disadvantages of the "switch" statement in Java

Java Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!