Nested switch Statement in C++ – Explained with Examples नेस्टेड स्विच स्टेटमेंट – उदाहरणों सहित

A nested switch statement in C++ means using one switch statement inside another switch statement. It is useful when a program needs to make a second-level decision based on the result of the first decision.

C++ में Nested switch statement का अर्थ है एक switch statement के अंदर दूसरे switch statement का उपयोग करना। इसका उपयोग तब किया जाता है जब प्रोग्राम में पहले निर्णय के आधार पर दूसरा निर्णय लेना हो।

Syntax (सिंटैक्स)

switch (expression1)
{
case value1:
// Statements
switch (expression2)
{
case valueA:
// Statements
break;
case valueB:
// Statements
break;
default:
// Statements
}
break;
case value2:
// Statements
break;
default:
// Statements
}

The inner switch statement is placed inside a case block of the outer switch statement.

Inner switch statement को outer switch statement के किसी case block के अंदर लिखा जाता है।

Example: Nested switch Statement in C++

The following program uses a nested switch statement to select a course and then select a subject within that course.

निम्न प्रोग्राम में Nested switch statement का उपयोग करके पहले एक कोर्स चुना जाता है और फिर उस कोर्स के अंतर्गत एक विषय चुना जाता है।

#include <iostream>
using namespace std;
int main()
{
int course, subject;
cout << "Select Course:" << endl;
cout << "1. BCA" << endl;
cout << "2. BSc" << endl;
cout << "Enter course number: ";
cin >> course;
switch (course)
{
case 1:
cout << "\nSelect BCA Subject:" << endl;
cout << "1. C++" << endl;
cout << "2. Java" << endl;
cout << "Enter subject number: ";
cin >> subject;
switch (subject)
{
case 1:
cout << "You selected BCA - C++";
break;
case 2:
cout << "You selected BCA - Java";
break;
default:
cout << "Invalid subject selection";
}
break;
case 2:
cout << "\nSelect BSc Subject:" << endl;
cout << "1. Physics" << endl;
cout << "2. Mathematics" << endl;
cout << "Enter subject number: ";
cin >> subject;
switch (subject)
{
case 1:
cout << "You selected BSc - Physics";
break;
case 2:
cout << "You selected BSc - Mathematics";
break;
default:
cout << "Invalid subject selection";
}
break;
default:
cout << "Invalid course selection";
}
return 0;
}

Output (आउटपुट)

Example 1:

Select Course:
1. BCA
2. BSc
Enter course number: 1
Select BCA Subject:
1. C++
2. Java
Enter subject number: 2
You selected BCA - Java

Example 2:

Select Course:
1. BCA
2. BSc
Enter course number: 2
Select BSc Subject:
1. Physics
2. Mathematics
Enter subject number: 1
You selected BSc - Physics

How Nested switch Works (Nested switch कैसे कार्य करता है?)

  1. First, the outer switch checks the value of the course variable.
    सबसे पहले outer switch, course variable की value को check करता है।

  2. If the user selects 1, the BCA case is executed and the inner switch checks the subject.
    यदि user 1 चुनता है, तो BCA वाला case execute होता है और inner switch विषय को check करता है।

  3. If the user selects 2, the BSc case is executed and its inner switch checks the selected subject.
    यदि user 2 चुनता है, तो BSc वाला case execute होता है और उसका inner switch चुने गए विषय को check करता है।

  4. The break statement exits the current switch statement.
    break statement वर्तमान switch statement से बाहर निकलता है।

A nested switch statement is useful for handling multiple levels of choices in a program. It makes menu-driven programs easier to organize and understand.

Nested switch statement प्रोग्राम में एक से अधिक स्तर के विकल्पों को संभालने में उपयोगी होता है। इससे menu-driven programs को व्यवस्थित और समझने में आसान बनाया जा सकता है.

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

C++ Programming Syllabus Complete - From Basics to Advanced C++, including OOP and Data Structures and Algorithms (DSA)

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)