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.

Example:

Nested Switch Statement

Java
// Java Program to Demonstrate
// Nested Switch Case Statement

// Class
public class GFG {

    // Main driver method
    public static void main(String[] args)
    {
        // Custom input string
        String Branch = "CSE";
        int year = 2;

        // Switch case
        switch (year) {

        // Case
        case 1:
            System.out.println(
                "elective courses : Advance english, Algebra");

            // Break statement to hault execution here
            // itself if case is matched
            break;

            // Case
        case 2:

            // Switch inside a switch
            // Nested Switch
            switch (Branch) {

            // Nested case
            case "CSE":
            case "CCE":
                System.out.println(
                    "elective courses : Machine Learning, Big Data");
                break;

            // Case
            case "ECE":
                System.out.println(
                    "elective courses : Antenna Engineering");
                break;

                // default case
                // It will execute if above cases does not
                // execute
            default:

                // Print statement
                System.out.println(
                    "Elective courses : Optimization");
            }
        }
    }
}

Output
elective courses : Machine Learning, Big Data

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...