Nested Switch Statements in Java

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Java Statements: Break, Continue & Return

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
Your next lesson will play in 10 seconds
  • 0:03 Nested Switch Statements
  • 2:15 Breaking in Switch Statements
  • 3:04 Use of Defaults
  • 3:41 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Sudha Aravindan

Sudha has a Doctor of Education Degree and is currently working as a Information Technology Specialist.

In Java, it's possible to use nested switch statements to provide the possibility of a whole new set of choices within the initial choice. In this lesson, we'll learn more about nested switch statements.

Nested Switch Statements

A nested switch statement is a switch statement within another switch statement. A switch statement in Java has the following structure:

switch(input variable) {
  case "Option 1":
   /* This is the first option */
   do something;
  case "Option 2":
   /* This is the second option */
   do something;
   /* If the input is neither option 1 nor option 2 */
   do something;

Let us consider an example. You order a sandwich and you're asked what kind of sandwich you would like: chicken, beef, pork, or vegetarian. You select vegetarian and you're asked to select from a choice of three vegetarian combinations:

  1. Tomato and mozzarella
  2. Eggplant and parmesan
  3. Cucumber and Swiss cheese

In this case when you choose chicken, beef, or pork, you have no more choices to make. But when you choose vegetarian, you'd then have to choose what kind of vegetarian sandwich you like. This is a good example for using a nested switch statement. The initial choices for chicken, beef, pork, or vegetarian would be inside as a set of switch statements. Then within the choice for vegetarian is another set of choices that can be listed with another set of switch statements. Since there is a set of switch statements listed within the initial set of switch statements, it is called a nested switch statement.

You can write code in the Java programming language to indicate the sandwiches and the choices using a nested switch statement:

public static void main(String[] args) {
  switch(sandwich-type) {
   case: "Chicken":
    System.out.println("Chicken Sandwich");
   case: "Beef":
    System.out.println("Beef Sandwich");
   case "Pork":
    System.out.println("Pork Sandwich");
   case "Vegetarian":
    switch(filling-type) {
     case: "Tomato and Mozzarella":
      System.out.println("Tomato and Mozzarella filling");
     case: "Eggplant and Parmesan":
      System.out.println("Eggplant and Parmesan filling");
     case: "Cucumber and Swiss cheese":
      System.out.println("Cucumber and Swiss cheese filling");
      System.out.println("Choice was not any of the above.");
    System.out.println("Choice was not Chicken, Beef, Pork, or Vegetarian");

Now, you go to a drive-through that is using a Java program and make an order. The waiter would ask you: 'What kind of sandwich would you like?'

  • If you say 'Chicken'
    • The computer display will say 'Chicken Sandwich'
  • If you say 'Beef'
    • The computer display will say 'Beef Sandwich'
  • If you say 'Pork'
    • The computer display will say 'Pork Sandwich'
  • If you say 'Vegetarian'

The waiter will then ask you the question: 'What kind of filling would you like?'

  • If you say 'Tomato and Mozzarella'
    • The computer display will say 'Tomato and Mozzarella filling'
  • If you say 'Eggplant and Parmesan'
    • The computer display will say 'Eggplant and Parmesan filling'
  • If you say 'Cucumber and Swiss Cheese'
    • The computer display will say 'Cucumber and Swiss Cheese filling'

So you can see how the switch statement works. When the customer makes the choice for a vegetarian sandwich there are other choices to be made, and these choices are listed with a switch statement block that is within the original switch statement.

Breaking in Switch Statements

Within a switch statement where a number of choices are presented, the use of the break statement is critical. If the break statement is left out then the program will continue to the next case (or choice) until it encounters another break statement. Consider the following code excerpt from a nested switch statement:

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account