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:// Statementsswitch (expression2){case valueA:// Statementsbreak;case valueB:// Statementsbreak;default:// Statements}break;case value2:// Statementsbreak;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. BCA2. BScEnter course number: 1Select BCA Subject:1. C++2. JavaEnter subject number: 2You selected BCA - Java
Example 2:
Select Course:1. BCA2. BScEnter course number: 2Select BSc Subject:1. Physics2. MathematicsEnter subject number: 1You selected BSc - Physics
How Nested switch Works (Nested switch कैसे कार्य करता है?)
- First, the outer
switchchecks the value of thecoursevariable.सबसे पहले outerswitch,coursevariable की value को check करता है। - If the user selects
1, the BCA case is executed and the innerswitchchecks the subject.यदि user1चुनता है, तो BCA वाला case execute होता है और innerswitchविषय को check करता है। - If the user selects
2, the BSc case is executed and its innerswitchchecks the selected subject.यदि user2चुनता है, तो BSc वाला case execute होता है और उसका innerswitchचुने गए विषय को check करता है। - The
breakstatement exits the current switch statement.breakstatement वर्तमानswitchstatement से बाहर निकलता है।
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
Post a Comment