Java Switch Case

Java Switch Case statement is used to execute a block of statements, selectively, from many different blocks (cases).

Java switch case statement could be related to a if-else-if ladder statement where a single expression is matched with different values in the if conditions.

In this tutorial, we will learn the syntax of switch statement and go through some example programs to understand the usage of switch statement.

Syntax

A switch statement contains an expression, one or more case blocks and an optional default block.

Following is the syntax of a switch statement in Java.

</>
Copy
switch (expression) {
    case value1:
        // statement(s)
        break;
    case value2:
        // statement(s)
        break;
    default:
        // statement(s)
 }

We may also use braces around statements inside case blocks.

</>
Copy
switch (expression) {
    case value1: {
        // statement(s)
        break;
    }
    case value2: {
        // statement(s)
        break;
    }
    default: {
        // statement(s)
    }
 }

Firstly, program execution evaluates the switch expression to a value. This value is matched against the case values value1, value2, etc., written inside switch statement.

If a case value matches with the expression value, then the corresponding block of statements are executed.

If there no match with any of the case values, then the default block at the end of the switch statement is executed. Therefore we write the code in default block that has to be executed when no case block is selected for execution.

Examples

In the following example, we execute one of the case-blocks based on the value of x.

Main.java

</>
Copy
public class Main {
    public static void main(String[] args) {
        int x = 2;
        switch (x) {
            case(0): {
                System.out.println("x is zero.");
                break;
            } 
            case(1): {
                System.out.println("x is one.");
                break;
            }
            case(2): {
                System.out.println("x is two.");
                break;
            }
            default: {
                System.out.println("default operation.");
            }
        }
    }
}

Output

x is two.

In this example, expression for switch statement evaluates to a String.

Main.java

</>
Copy
public class Main {

    public static void main(String[] args) {
        //someFunction("Student");
        someFunction("Teacher");
        //someFunction("parent");
        //someFunction("PRINCIPAL");
        //someFunction("guest");
    }

    public static void someFunction(String role) {
        switch (role.toLowerCase()) {
            case "student": {
                System.out.println("I'm student.");
                break;
            }
            case "teacher": {
                System.out.println("I'm teacher.");
                break;
            }
            case "principal": {
                System.out.println("I'm principal.");
                System.out.println("I'm head of school.");
                break;
            }
            default: {
                System.out.println("I'm default.");
            }
        }
        System.out.println();
    }

}

Output

I'm teacher.

Conclusion

In this Java Tutorial, we learned about Java Switch case statement, and how to use this statement in Java programs with examples.