Open In App

Nested switch case

Switch-case statements:
These are a substitute for long if statements that compare a variable to several integral values 
 



Points to remember while using Switch Case 
 

Syntax: 



switch (n)
{
    case 1: // code to be executed if n = 1;
  break;
    case 2: // code to be executed if n = 2;
  break;
    default: // code to be executed if 
      // n doesn't match any cases
}

Nested-Switch Statement:
Nested-Switch statements refers to Switch statements inside of another Switch Statements.
Syntax: 
 

switch(n)
{
  // code to be executed if n = 1;
  case 1: 
    
  // Nested switch
  switch(num) 
  {
    // code to be executed if num = 10
    case 10: 
      statement 1;
      break;
      
    // code to be executed if num = 20
    case 20: 
      statement 2;
      break;
      
    // code to be executed if num = 30
    case 30: 
      statement 3;
      break;
      
      // code to be executed if num 
      // doesn't match any cases
      default: 
  }
  
  
  break;
    
  // code to be executed if n = 2;
  case 2:
    statement 2;
    break;
  
  // code to be executed if n = 3;
  case 3: 
    statement 3;
    break;
  
   // code to be executed if n doesn't match any cases
   default: 
}

Example:
 




// Following is a simple program to demonstrate
// syntax of Nested Switch Statements.
#include <stdio.h>
 
int main()
{
    int x = 1, y = 2;
 
    // Outer Switch
    switch (x) {
 
    // If x == 1
    case 1:
 
        // Nested Switch
 
        switch (y) {
 
        // If y == 2
        case 2:
            printf( "Choice is 2");
            break;
 
        // If y == 3
        case 3:
            printf( "Choice is 3");
            break;
        }
        break;
 
    // If x == 4
    case 4:
        printf( "Choice is 4");
        break;
 
    // If x == 5
    case 5:
        printf( "Choice is 5");
        break;
 
    default:
        printf( "Choice is other than 1, 2 3, 4, or 5");
         
    }
    return 0;
}




// Following is a simple program to demonstrate
// syntax of Nested Switch Statements.
#include <iostream>
using namespace std;
 
int main()
{
    int x = 1, y = 2;
 
    // Outer Switch
    switch (x) {
 
    // If x == 1
    case 1:
 
        // Nested Switch
 
        switch (y) {
 
        // If y == 2
        case 2:
            cout <<  "Choice is 2";
            break;
 
        // If y == 3
        case 3:
            cout <<  "Choice is 3";
            break;
        }
        break;
 
    // If x == 4
    case 4:
        cout <<  "Choice is 4";
        break;
 
    // If x == 5
    case 5:
        cout <<  "Choice is 5";
        break;
 
    default:
        cout <<  "Choice is other than 1, 2 3, 4, or 5";
         
    }
    return 0;
}
 
// This code is contributed by Shubham Singh




// Following is a simple program to demonstrate
// syntax of Nested Switch Statements.
import java.io.*;
 
class GFG {
 
    public static void main (String[] args)
    {
        int x = 1, y = 2;
     
        // Outer Switch
        switch (x) {
     
        // If x == 1
        case 1:
     
            // Nested Switch
     
            switch (y) {
     
            // If y == 2
            case 2:
                System.out.println("Choice is 2");
                break;
     
            // If y == 3
            case 3:
                System.out.println("Choice is 3");
                break;
            }
            break;
     
        // If x == 4
        case 4:
            System.out.println("Choice is 4");
            break;
     
        // If x == 5
        case 5:
            System.out.println("Choice is 5");
            break;
     
        default:
            System.out.println("Choice is other than 1, 2 3, 4, or 5");
             
        }
    }
}
 
// This code is contributed by Shubham Singh




# Following is a simple program to demonstrate
# syntax of Nested Switch Statements.
 
x = 1
y = 2
 
# Outer Switch
def switch_x(x):
    switcher = {
        1: switch_y(y),
        4: "Choice is 4",
        5: "Choice is 5",
    }
    return switcher.get(x, "Choice is other than 1, 2 3, 4, or 5")
 
def switch_y(y):
    switcher = {
        2: "Choice is 2",
        3: "Choice is 3",
    }
    return switcher.get(y, "")
print(switch_x(x))
 
# This code is contributed by Shubham Singh




// Following is a simple program to demonstrate
// syntax of Nested Switch Statements.
using System;
 
public class GFG{
 
    static public void Main ()
    {
        int x = 1, y = 2;
     
        // Outer Switch
        switch (x) {
     
        // If x == 1
        case 1:
     
            // Nested Switch
     
            switch (y) {
     
            // If y == 2
            case 2:
                Console.WriteLine("Choice is 2");
                break;
     
            // If y == 3
            case 3:
                Console.WriteLine("Choice is 3");
                break;
            }
            break;
     
        // If x == 4
        case 4:
            Console.WriteLine("Choice is 4");
            break;
     
        // If x == 5
        case 5:
            Console.WriteLine("Choice is 5");
            break;
     
        default:
            Console.WriteLine("Choice is other than 1, 2 3, 4, or 5");
             
        }
    }
}
 
// This code is contributed by Shubham Singh




<script>
// Following is a simple program to demonstrate
// syntax of Nested Switch Statements.
var x = 1, y = 2;
 
// Outer Switch
switch (x) {
 
    // If x == 1
    case 1:
 
        // Nested Switch
 
        switch (y) {
 
        // If y == 2
        case 2:
            document.write("Choice is 2");
            break;
         
        // If y == 3
        case 3:
            document.write("Choice is 3");
            break;
        }
         
        break;
         
    // If x == 4
    case 4:
        document.write("Choice is 4");
        break;
 
    // If x == 5
    case 5:
        document.write("Choice is 5");
        break;
         
    default:
        document.write("Choice is other than 1, 2 3, 4, or 5");
         
}
 
// This code is contributed by Shubham Singh
</script>

Output: 
Choice is 2

 


Article Tags :