In this topic, we see how we can use the nested switch. How does work the nested switch statements java? Let’s discuss the nested switch in java programs Before moving further we will recommend you please read the switch statement. There are some rules or important points that help you to understand it better.
Here is the table content of the article will we will cover this topic.
1. switch statement in Java?
2. Rules of the switch statement Java
3. Nested switch statements in Java?
4. Does Java switch multiple cases?
5. switch case without a break statement?
6. switch case with char?
7. switch case with wrapper classes?
8. Why does float values are not allowed in the switch statement?
9. Why the break statement is used in the switch statement?
Youtube video available in the Hindi language
We can use a switch statement inside another switch statement. This is known as the Nested switch-case statements in Java. The inner switch statement will be part of any case of an outer switch. The inner switch statement will be executed only if the outer switch statement condition is true.
switch(variable/expression)
{
case value1 :
// code inside the case value1
break; // optional
case value2 :
// code inside the case value2
break; // optional
case value3 :
switch(variable/expression)
{
case valueOne :
// code inside the case valueOne
break; // optional
case valueTwo :
// code inside the case valueTwo
break; // optional
.
.
.
default :
// code inside the default case .
}
.
.
.
default :
// code inside the default case .
}

You can see in above syntax the inner switch statement only accessible if outer switch statement’s condition evaluated true.
Step1: In first step condition of outer switch statement will be check. If it return true then control flow enter to outer Switch.
Step2: In second step the block statement is executed with the matched case.
Step3: In third step condition of inner switch statement will be check. If it return true then control flow enter to inner Switch.
Important point of Nested Switch statements in Java
1. The inner switch statement must be part of any case of outer switch statement. You can’t write inner switch statement outside the cases.
2. You can use inner switch statement in default case of outer switch statement.
public class ExampleOfNestedSwitch
{
public static void main(String[] args)
{
//Declaring a variable for switch expression
int year = 3;
int marks = 80;
switch(year) //Switch expression
{
//Case statements
case 1:
System.out.println("First year students are not eligible for scholarship ");
break;
case 2:
System.out.println("Second year students are not eligible for scholarship");
break;
case 3:
switch(marks)
{
case 50:
System.out.println("You are not eligible for scholarship");
break;
case 80:
System.out.println("Congrats!!!!! you are eligible for scholarship");
break;
}
break;
//Default case statement
default: System.out.println("Please enter valid year");
}
}
}
Output: Congrats!!!!! you are eligible for scholarship
1. In the given program we have initialized two variables: year and marks
2. An outer switch statement is used to check which year of student falling in category of
student.
3. If the block statement is executed with the matched case, then an inner switch statement
is used to check the marks of student. If no student having marks matched with case the
default case of inner switch statement executed.
4. Otherwise, the switch case will trigger the default case


1. Quiz, Read the below code and do answer.
public class ExampleOfNestedSwitch { public static void main(String[] args) { //Declaring a variable for switch expression int year = 3; int marks = 60; switch(year) //Switch expression { //Case statements case 1: System.out.println("First year students are not eligible for scholarship "); break; case 2: System.out.println("Second year students are not eligible for scholarship"); break; case 3: switch(marks) { case 50: System.out.println("You are not eligible for scholarship"); break; case 80: System.out.println("Congrats!!!!! you are eligible for scholarship"); break; } break; //Default case statement default: System.out.println("Please enter valid year"); } } }Click on anyone to know the answer.