Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
Nested switch statement in C++
Next article icon

Nested switch statement in C++

Last Updated : 08 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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

  • The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
  • Switch is a control statement that allows a value to change control of execution.

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 n        // 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: 

CPP
// 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; } 
Output:
Choice is 2

Next Article
Nested switch statement in C++

M

MinalJain
Improve
Article Tags :
  • Technical Scripter
  • C++ Programs
  • C++
  • Computer Science Fundamentals
Practice Tags :
  • CPP

Similar Reads

    Why Breaks are used in C++ Switch Statement?
    In C++, the switch statement allows us to select a code block to be executed based on some conditions among many alternatives (listed in the form of cases). The break keyword plays an important role in the functioning of a switch statement. In this article, we will learn why breaks are used in C++ s
    2 min read
    How a statement is handled inside switch block but outside case
    Switch case statements are a substitute for long if statements that compare a variable to several integral values. The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. It is a control state
    4 min read
    Swap Two Numbers Without Third Variable in C++
    In C++, swapping two numbers means we need to exchange the value of two numbers. In this article, we will learn how to swap two numbers without using the third variable in C++. Example Input: a=10b=20Output:After swapping:a=20b=10Swap Two Numbers Without Using a Third VariableIn C++ we can swap two
    2 min read
    How to Utilize the goto Statement in C++?
    The goto statement in C++ is a control flow statement that allows the users to move the control flow from one part to another part of the program. In this article, we will learn how to utilize the goto statement in C++. Utilize the goto Statement in C++In C++, the goto statement transfers the progra
    2 min read
    C++ Exercises - C++ Practice Set with Solutions
    Do you want to improve your command on C++ language? Explore our vast library of C++ exercise questions, which are specifically designed for beginners as well as for advanced programmers. We provide a large selection of coding exercises that cover every important topic, including classes, objects, a
    15+ min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences