Switch Case & Nested Switch Case Statements in Java

Nested Switch Case Statment in Java

Introduction to Switch Case and Nested Switch Case Statements in Java

The switch statement in Java is a multi-way branch statement. It allows you to execute different blocks of code depending on the value of an expression. The syntax for the switch statement is as follows:


  switch (expression) {
    case value1:
      // code to execute if the expression is equal to value1
      break;
    case value2:
      // code to execute if the expression is equal to value2
      break;
    ...
    default:
      // code to execute if the expression does not match any of the case values
  }
 

The expression can be any expression that evaluates to a primitive data type (int, char, byte, short) or an enumerated type. The case values must be of the same type as the expression

If the expression matches one of the case values, the code block inside that case is executed. If the expression does not match any of the case values, the code block inside the default clause is executed.

The switch statement is a powerful tool for controlling the flow of execution of a program. It is especially useful for handling multiple conditional statements.

A nested switch statement is a switch statement that contains another switch statement inside of it. This can be useful for handling complex conditional logic. The syntax for a nested switch statement is as follows:


  switch (outerExpression) {
    case value1:
      // code to execute if the outer expression is equal to value1
      switch (innerExpression) {
        case value2:
          // code to execute if the inner expression is equal to value2
          break;
        case value3:
          // code to execute if the inner expression is equal to value3
          break;
        ...
        default:
          // code to execute if the inner expression does not match any of the case values
      }
      break;
    case value2:
      // code to execute if the outer expression is equal to value2
      switch (innerExpression) {
        case value4:
          // code to execute if the inner expression is equal to value4
          break;
        case value5:
          // code to execute if the inner expression is equal to value5
          break;
        ...
        default:
          // code to execute if the inner expression does not match any of the case values
      }
      break;
    ...
    default:
      // code to execute if the outer expression does not match any of the case values
  }
 
Here are some tips for using the switch statement effectively:
  • Use the switch statement for multi-way branch statements.
  • If you only have two possible cases, it is better to use an if-else statement.
  • Use the break statement to prevent the execution of subsequent cases.
  • Use a default clause to handle cases that do not match any of the case values.
  • Use proper indentation and spacing to make your switch statements easier to read.

Exercises

Get a Number as input from the user. If the user enter 1 then display the output as "one". If the user enters 2 then display the result as "two". For any other input display invalid input. Use switch case in your code


package javaprogrammingdemo;
import java.util.Scanner;
public class javalabclass{
  public static void main(String args[]) 
  {
    System.out.println("Enter a number");
    Scanner input = new Scanner(System.in);
    int choice = input.nextInt();
  switch(choice)
  {
  case 1:
    System.out.println("one");
    break;	
  case 2:
    System.out.println("two");
      break;
  default:
    System.out.println("default");
    break;
  }
    
  }
}

Program output
Enter a number
2
two  

Get a character as input from the user.
if the user enters M then display Mathew
if the user enteres R then diplay Ram
for any other input display the output as Invalid Character


  package javaprogrammingdemo;
  import java.util.Scanner;
  public class javalabclass{
    public static void main(String args[]) 
    {
      System.out.println("Enter a character");
      Scanner input = new Scanner(System.in);
      char choice = input.next().charAt(0);
    switch(choice)
    {
    case 'M':
      System.out.println("Mathew");
      break;
    case 'R':
      System.out.println("Ram");
        break;
    default:
      System.out.println("Invalid Character");
      break;
    }		
    }
  }
  
  Program Output
  Enter a character
  M
  Mathew 

Get the Name of a Country as input from the user.
if the user enters India then display New Delhi
if the user enters US then diplay Washington DC
for any other input display the output as Invalid Input.
Use Switch Case to solve this


  package javaprogrammingdemo;
  import java.util.Scanner;
  public class javalabclass{
    public static void main(String args[]) 
    {
      System.out.println("Enter the Country Name");
      Scanner input = new Scanner(System.in);
      String country = input.nextLine();
    switch(country)
    {
    case "India":
      System.out.println("New Delhi");
      break;
    case "US":
      System.out.println("Washington DC");
        break;
    default:
      System.out.println("Invalid Input");
      break;
    }		
    }
  }
    
  Program Output
  Enter the Country Name
  India
  New Delhi 

Get the country name as input from the user.
if the user enters India as input then using a nested switch block ask the user to enter the state.
if the user enters Tamilnadu then display Southern State of India as the message
if the user enters Rajasthan then display Northern State of India as the message
for any other input display Enter a valid state as the message.
if the user enters country name as Russia then display Country is Russia as the message
for any other country name display the message "Check your Country name"
Use Switch Case to solve this


  package testProject;
  import java.util.Scanner;
  public class testProject {
  
    public static void main(String[] args) {
  
    //Example for Nested Switch Case
         Scanner obj = new Scanner(System.in);
         System.out.println("Enter your country");
         String country = obj.nextLine();
         switch(country)
         {
           case "India":
             System.out.println("Country is India");
             System.out.println("Enter your State");
             String state = obj.nextLine();
             switch(state)
             {
             case "Tamilnadu":
               System.out.println("Southern State of India");
               break;
             case "Rajasthan":
               System.out.println("Northern State of India");
               break;
             default:
                System.out.println("Enter a valid state");
                break;
             }
             break;
           case "Russia":
             System.out.println("Country is Russia");
             break;
           default:
             System.out.println("Check your Country name");
             break;
         }
         
         System.out.println("Thanks for using our Software");
    }
  
  }