Flowchart of Switch-Case Statement

This flowchart shows the control flow and working of switch statements:

Note: Java switch statement is a fall through statement that means it executes all statements if break keyword is not used, so it is highly essential to use break keyword inside each case.  

Example: Finding Day

Consider the following Java program, it declares an int named day whose value represents a day(1-7). The code displays the name of the day, based on the value of the day, using the switch statement.

Java
// Java program to Demonstrate Switch Case
// with Primitive(int) Data Type

// Class
public class GFG {

    // Main driver method
    public static void main(String[] args)
    {
        int day = 5;
        String dayString;

        // Switch statement with int data type
        switch (day) {

        // Case
        case 1:
            dayString = "Monday";
            break;

        // Case
        case 2:
            dayString = "Tuesday";
            break;

            // Case
        case 3:
            dayString = "Wednesday";
            break;

            // Case
        case 4:
            dayString = "Thursday";
            break;

        // Case
        case 5:
            dayString = "Friday";
            break;

            // Case
        case 6:
            dayString = "Saturday";
            break;

            // Case
        case 7:
            dayString = "Sunday";
            break;

        // Default case
        default:
            dayString = "Invalid day";
        }
        System.out.println(dayString);
    }
}

Output
Friday

Switch Statements in Java

The switch statement in Java is a multi-way branch statement. In simple words, the Java switch statement executes one statement from multiple conditions.

It is like an if-else-if ladder statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. The expression can be a byte, short, char, or int primitive data type. It tests the equality of variables against multiple values.

Note: Java switch expression must be of byte, short, int, long(with its Wrapper type), enums and string. Beginning with JDK7, it also works with enumerated types (Enums in java), the String class, and Wrapper classes.

Similar Reads

Syntax

switch(expression){ case value1 : // Statements break; // break is optional case value2 : // Statements break; // break is optional .... .... .... default : // default Statement}...

Flowchart of Switch-Case Statement

This flowchart shows the control flow and working of switch statements:...

break in switch case Statements

A break statement is optional. If we omit the break, execution will continue into the next case....

Java Nested Switch Statements

We can use a switch as part of the statement sequence of an outer switch. This is called a nested switch. Since a switch statement defines its block, no conflicts arise between the case constants in the inner switch and those in the outer switch....

Java Enum in Switch Statement

Enumerations (enums) are a powerful and clear way to represent a fixed set of named constants in Java....

default statement in Java Switch Case

default case in the Switch case specifies what code to run if no case matches....

Case label variations

Case label and switch arguments can be a constant expression. The switch argument can be a variable expression....

Java Wrapper in Switch Statements

Java provides four wrapper classes to use: Integer, Short, Byte, and Long in switch statements....

Exercise

To practice Java switch statements you can visit the page: Java Switch Case statement Practice...

Conclusion

Switch statements in Java are control flow structures, that allow you to execute certain block of code based on the value of a single expression. They can be considered as an alternative to if-else-if statements in programming....

Java Switch Statements- FAQs

How to use switch statements in Java...