• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Nested Switch Statements in C Programming with Real Life Example

Michael Kagiri/42489/7
C / C++Code

Nested Switch Statements occurs when a switch statement is defined inside another switch statement. The first switch is referred to as an outer switch statement whereas the inside switch is referred to as an inner switch statement.

In this tutorial, we will learn about the syntax of a nested switch statement in C programming. In addition, we shall also write a real-life example to demonstrate the concept of nested switch statements.

What is a nested switch statement?

A nested switch statement is defined as having a switch statement within another switch statement

The syntax for Nested Switch Case:

The basic syntax used for creating a nested switch statement is as follows:

switch (a)
{
  printf("This a is part of outer switch" );
  case 1: // code to be executed if a = 1;
    break;
  case 2: 
    switch(b) 
    {
      case 1:
        // code to be executed if b = 1;
        printf("This b is part of inner switch" );
        break;
      case 2: 
        // code to be executed if b = 2;
        printf("This b is part of inner switch" );
        break;
    }  
    break;
    default: 
      // code to be executed if 
      // a doesn't match any cases
}

In the above syntax, we have declared two nested switch statements.

The first switch statement with variable “a” is termed as the outer switch statement. Whereas, the switch statement with the variable “b” is termed as the inner switch statement.

Nested Switch Statement Example:

To help you understand the nested switch statement better, let’s consider an example.

You are searching for a department in a university and you’re asked to select a school from a choice of three schools namely:

  1. School of Computer Science
  2. School of Business
  3. School of Engineering

Having selected a school you are again provided with a list of departments that fall under the department namely:

  1. School of Computer Science
    1. Department of Informatics
    2. Department of Machine Learning
  2. School of Business
    1. Department of Commerce
    2. Department of purchasing
  3. School of Engineering
    1. Department of Mechanical Engineering
    2. Department of Mechatronics Engineering

This is a good example of the nested switch statement.

The initial choices for Computer Science, Business and Engineering schools would be inside as a set of switch statements. Then various departments would then be listed within inner switch statements beneath their respective schools.

Nested Switch Statements in C Program

Let’s now write an actual program to apply the concepts learned.

The program below assumes that only the school of business has departments for the purpose of demonstrating a nested switch statement.

#include<stdio.h>
int main()
{
  int a,b;
  printf("1.School of Computer Science\n");
  printf("2.School of Business\n");
  printf("3.School of Engineering\n");
  printf("make your selection\n");
  scanf("%d",&a);
  switch (a)
  {
    case 1: 
      //code to be executed 
      //if school of computer science is chosen;
      break;
    case 2: 
      //code to be executed 
      //if school of business is chosen;
      printf("Available Departments\n"
      printf("1.Department of commerce\n");
      printf("2.Department of purchasing\n");
      printf("Make your selection.\n");
      scanf("%d",&b);

      //inner switch to display the departments 
      //under the school of commerce
      switch(b) 
      { 
        case 1:
        // code to be executed if b = 1;
        printf("You chose Department of commerce\n" );
        break;
        case 2: 
        // code to be executed if b = 2;
        printf("You chose Department of purchasing" );
        break;
      }	  
      break;
  }
}

Note: You can also write the C code for the same example using if-else statement in programming.

The output of the Program:

In the above code, if a user chooses option 2(school of business), a list of departments in the school will be displayed and a user asked to choose a department. On choosing a department the user will get an output showing the department chosen.

The output is shown below.

nested switch statements c output

Try running this program on your system and choose various options. This will help you understand each line of code.

What’s Next?

Check out complete C/C++ programming tutorial.

This is all about nested switch statements in C programming. I tried explaining with a real-life example and a code.

Any doubt? Any question related to nested switch Programming Example in C/C++? Let’s share your thought in the comment section.

Happy Programming!

Codecppswitch-c
Michael Kagiri
I'm a lecturer at Dedan Kimathi University of Technology And Ph.D. Student. I have a great passion for writing codes and preparing tutorials in Computer Science fields like Database Management Systems, Object Oriented Programming and languages like C, C++, Java, Python, HTML.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Ven Maf
    March 25, 2020 at 1:10 am

    Thanks for writing this guide. Your way of writing is very easy to understand.

  • Reply
    Vishal Rathaur
    March 26, 2021 at 8:08 am

    Thanks for these coding examples and practical. It’s easy to learn with these practical examples.

    • Reply
      Aniruddha Chaudhari
      March 26, 2021 at 9:27 pm

      You’re welcome, Vishal!

  • Reply
    Adrew
    September 8, 2021 at 3:12 am

    Please I couldn’t find any default case codes. For example, if the user chooses an invalid option what should the program execute?

    • Reply
      Aniruddha Chaudhari
      September 12, 2021 at 11:08 am

      Adrew, you can use the default keyword to execute the default code block.

      Here is the default statement you can add in the above example.

            switch(b) 
            { 
              case 1:
              // code to be executed if b = 1;
              printf("You chose Department of commerce\n" );
              break;
              case 2: 
              // code to be executed if b = 2;
              printf("You chose Department of purchasing" );
              break;
              default:
              printf("Invalid selection.");
              break;
            }  
      
  • Reply
    Jithendra
    October 11, 2021 at 10:36 pm

    As Everyone said…

    The examples with explanations are NICE…!!!

    Now I am able to understand the switch statement…

    Eagerly looking forward to even more explanations….👍👍

  • Reply
    Mohammad Aslam Ansari
    November 24, 2021 at 7:13 am

    Hi,

    Good Morning,

    Respected: SIR.

    You have given the example of Departments in that its only School of business is executed. But, I need both three of the Departments to get executed and if you don’t mind please make it 4 departments and make execute all departments one by one. Please…..

    Because I need to be executed…

Leave a Reply Cancel reply

C Programming

  1. C- Introduction
  2. C- Compile & Execute Program
  3. C- Data Types
  4. C- if-else statement
  5. C- While, do-while, for loop
  6. C- Array
  7. C- Function (Types/Call)
  8. C- strlen() vs sizeof()
  9. C- Nested Switch Statement
  10. C- Recursion
  11. C- Dynamic Programming
  12. C- Storage Classes
  13. C- Creating Header File
  14. C- Null Pointer
  15. C- Stack and Queue
  16. C- Implement Stack using Array
  17. C- Implement Linked List in C
  18. C- File Handling
  19. C- Makefile Tutorial

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

Sorting Algorithms

  • Different Types of Sorting Algo
  • Selection Sort
  • Bubble Sort
  • Quick Sort

Programming for Practice

  1. Online C/C++ Compiler

String Handling:

  1. Remove White Spaces from String
  2. Implement strstr Function in C
  3. Convert String to Int – atoi()
  4. Check if String is Palindrome
  5. Check if Two Strings are Anagram
  6. Split String in using strtok_r()
  7. Undefined reference to strrev

Array:

  1. Check if Array is Sorted

Bit Manipulation:

  1. Count Number of 1’s in Binary

Linked List:

  1. Reverse a Linked List Elements

Number System:

  1. Program to Find 2’s Complement
  2. Convert Decimal to Binary in C

Tricky Questions:

  1. Add Two Numbers without Operator
  2. Find Next Greater Number
  3. Swap values without temp variable
  4. Print 1 to 100 without Loop

Interview Coding Questions

  • 50+ Interview Coding Questions

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard