switch-case Statement in C++ (C++ में switch-case स्टेटमेंट)
A switch-case statement allows the value of a variable or expression to be compared with multiple constant values. When a matching case is found, the statements associated with that case are executed.
The expression used in a switch statement must have an integral or enumeration type. In common introductory C++ examples, int and char values are used.
C++ में switch-case स्टेटमेंट की सहायता से किसी variable या expression की value को कई constant values के साथ compare किया जा सकता है। जब कोई matching case मिल जाता है, तो उस case के statements execute किए जाते हैं।
switch expression का type integral या enumeration type होना चाहिए। सामान्य उदाहरणों में int तथा char का प्रयोग किया जाता है।
Syntax (सिंटैक्स)
switch (expression){case constant1:statements;break;case constant2:statements;break;case constant3:statements;break;// Other casesdefault:statements;}
Explanation of switch-case (switch-case की व्याख्या)
switch: It is a selection control statement used to select one of multiple alternatives.case: Each case specifies a constant value to be compared with the switch expression.break: It terminates the switch statement and transfers control to the statement after the switch.default: It is an optional block that executes when no case matches the expression.switch: यह एक selection control statement है, जिसका प्रयोग कई विकल्पों में से एक को select करने के लिए किया जाता है।case: प्रत्येक case में एक constant value दी जाती है, जिससे switch expression की तुलना की जाती है।break: यह switch statement को terminate करता है तथा program control को switch के बाद वाले statement पर भेजता है।default: यह optional block है। जब कोई case match नहीं होता, तब default block execute होता है।
Important Note (महत्वपूर्ण नोट)
The break statement is commonly used at the end of each case to prevent execution from continuing into the next case. If break is omitted, execution may continue through subsequent case labels. This is called fall-through.
break का प्रयोग सामान्यतः प्रत्येक case के अंत में किया जाता है, जिससे अगले case के statements execute न हों। यदि break नहीं दिया जाता है, तो program अगले case के statements में भी जा सकता है। इसे Fall-through कहा जाता है।
The default case is optional. A break is not required at the end of default if it is the last block in the switch.
default case optional होता है। यदि default सबसे अंतिम block है, तो उसके अंत में break देना आवश्यक नहीं है।
Example: Check Whether an Alphabet is Vowel or Consonant
Write a C++ program to check whether a given alphabet is a vowel or consonant using the switch-case statement.
एक C++ प्रोग्राम लिखिए जो switch-case स्टेटमेंट की सहायता से दिए गए alphabet के vowel या consonant होने का पता करे।
Program:
#include <iostream>using namespace std;int main(){char ch;cout << "Enter an alphabet: ";cin >> ch;// Check whether the input is an alphabetif ((ch >= 'A' && ch <= 'Z') ||(ch >= 'a' && ch <= 'z')){switch (ch){case 'A':case 'a':case 'E':case 'e':case 'I':case 'i':case 'O':case 'o':case 'U':case 'u':cout << ch << " is Vowel" << endl;break;default:cout << ch << " is Consonant" << endl;}}else{cout << "Not an Alphabet" << endl;}return 0;}
Output 1:
Enter an alphabet: AA is Vowel
Output 2:
Enter an alphabet: bb is Consonant
Output 3:
Enter an alphabet: 5Not an Alphabet
Explanation (व्याख्या)
First, the program checks whether the entered character is an alphabet using the if condition.
If it is an alphabet, the switch statement checks whether the character matches any of the vowel cases: A, E, I, O, U or their lowercase forms.
If a vowel case matches, the character is displayed as a vowel.
If none of the vowel cases match, the
defaultblock displays the character as a consonant.If the entered character is not an alphabet, the program displays
Not an Alphabet.
सबसे पहले if condition की सहायता से check किया जाता है कि input character alphabet है या नहीं।
यदि character alphabet है, तो switch statement यह check करता है कि character vowel cases A, E, I, O, U या उनके lowercase forms में से किसी से match करता है या नहीं।
यदि vowel case match होता है, तो character को vowel प्रदर्शित किया जाता है।
यदि कोई vowel case match नहीं होता, तो
defaultblock character को consonant प्रदर्शित करता है।यदि input character alphabet नहीं है, तो प्रोग्राम
Not an Alphabetप्रदर्शित करता है।
The switch-case statement is a selection control structure in C++. It is useful when a variable or expression needs to be compared with multiple constant values.
C++ में switch-case एक selection control structure है। इसका प्रयोग तब उपयोगी होता है जब किसी variable या expression की value को कई constant values के साथ compare करना हो।
Comments
Post a Comment