Switch Case Statement in C Language with Examples
The switch statement is a decision-making statement in C language. It allows 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 integer type, such as int, char, or an enumeration type (enum). Floating-point values (float and double) and strings cannot be used directly as switch expressions.
C भाषा में switch एक निर्णय लेने वाला (Decision-Making) स्टेटमेंट है। इसके द्वारा किसी वेरिएबल या एक्सप्रेशन की वैल्यू की तुलना कई कॉन्स्टेंट वैल्यूज से की जा सकती है। जब कोई case मैच होता है, तो उस केस से संबंधित स्टेटमेंट्स निष्पादित होते हैं। switch स्टेटमेंट में उपयोग किए जाने वाले एक्सप्रेशन का डेटा टाइप इंटीजर होना चाहिए, जैसे int, char या enumeration (enum)। इसमें float, double और string का सीधे उपयोग नहीं किया जा सकता।
Each case is followed by a constant value and a colon (:). The value of the switch expression is compared with the case values. If a match is found, the statements under that case are executed. The break statement is generally used at the end of a case to terminate the switch statement. It prevents the execution from continuing into the statements of the following cases. If break is omitted, the program continues executing the subsequent case statements. This behavior is called fall-through.
प्रत्येक case के बाद एक कॉन्स्टेंट वैल्यू और कोलन (:) लगाया जाता है। Switch एक्सप्रेशन की वैल्यू की तुलना सभी case वैल्यूज से की जाती है। यदि कोई वैल्यू मैच हो जाती है, तो उस case के अंतर्गत दिए गए स्टेटमेंट्स निष्पादित होते हैं। प्रत्येक case के अंत में सामान्यतः break स्टेटमेंट का प्रयोग किया जाता है। यह switch स्टेटमेंट से बाहर निकलने में मदद करता है और अगले case के स्टेटमेंट्स को अनावश्यक रूप से निष्पादित होने से रोकता है। यदि break नहीं लगाया जाता है, तो प्रोग्राम अगले case के स्टेटमेंट्स भी निष्पादित करता है। इस प्रक्रिया को Fall-through कहते हैं।
A switch statement can also contain an optional default case. The default case is executed when none of the case values matches the switch expression. It is commonly written at the end of the switch statement. A break is not required at the end of the default case if it is the last case, but using one is also valid.
switch स्टेटमेंट में वैकल्पिक default case का भी प्रयोग किया जा सकता है। जब switch एक्सप्रेशन की वैल्यू किसी भी case से मैच नहीं होती है, तब default case निष्पादित होता है। इसे सामान्यतः switch स्टेटमेंट के अंत में लिखा जाता है। यदि default अंतिम case है, तो उसके अंत में break लगाना आवश्यक नहीं है, लेकिन इसका प्रयोग करना भी सही है।
Syntax of Switch Case Statement
switch (expression){case constant1:statements;break;case constant2:statements;break;case constant3:statements;break;default:statements;}
Explanation of Syntax:
switch: It is a keyword used to start the switch statement.expression: An integer-type expression whose value is compared with the case values.case: It specifies a constant value to be compared with the switch expression.break: It terminates the switch statement.default: It is an optional case executed when no case matches.
सिंटैक्स का विवरण:
switch: यह switch स्टेटमेंट को शुरू करने वाला कीवर्ड है।expression: यह इंटीजर टाइप एक्सप्रेशन है जिसकी वैल्यू की तुलना case वैल्यूज से की जाती है।case: इसके द्वारा तुलना के लिए एक कॉन्स्टेंट वैल्यू दी जाती है।break: यह switch स्टेटमेंट को समाप्त करता है।default: यह वैकल्पिक case है, जो तब निष्पादित होता है जब कोई भी case मैच नहीं होता है।
Example 1: C Program to Display Day Name Using Switch Case
In this program, the user enters a number from 1 to 7. The switch statement displays the corresponding day of the week.
इस प्रोग्राम में यूजर 1 से 7 के बीच एक नंबर इनपुट करता है। Switch स्टेटमेंट उस नंबर के अनुसार सप्ताह के दिन का नाम प्रदर्शित करता है।
C Program:
#include <stdio.h>int main(void){int day;printf("Enter day number (1-7): ");scanf("%d", &day);switch (day){case 1:printf("Monday");break;case 2:printf("Tuesday");break;case 3:printf("Wednesday");break;case 4:printf("Thursday");break;case 5:printf("Friday");break;case 6:printf("Saturday");break;case 7:printf("Sunday");break;default:printf("Invalid day number");}return 0;}
Output 1:
Enter day number (1-7): 3Wednesday
Output 2:
Enter day number (1-7): 9Invalid day number
Explanation:
If the user enters 3, the value of day matches case 3, so the program displays Wednesday. If the user enters a number outside the range 1 to 7, none of the cases matches and the default statement is executed.
यदि यूजर 3 इनपुट करता है, तो day की वैल्यू case 3 से मैच होती है और प्रोग्राम Wednesday प्रदर्शित करता है। यदि यूजर 1 से 7 के बाहर कोई नंबर इनपुट करता है, तो कोई भी case मैच नहीं होता और default स्टेटमेंट निष्पादित होता है।
Example 2: C Program to Perform Arithmetic Operations Using Switch Case
This program performs addition, subtraction, multiplication, and division according to the operator entered by the user.
यह प्रोग्राम यूजर द्वारा दिए गए ऑपरेटर के अनुसार Addition, Subtraction, Multiplication और Division की गणना करता है।
C Program:
#include <stdio.h>int main(void){float a, b;char op;printf("Enter two numbers: ");scanf("%f %f", &a, &b);printf("Enter operator (+, -, *, /): ");scanf(" %c", &op);switch (op){case '+':printf("Result = %.2f", a + b);break;case '-':printf("Result = %.2f", a - b);break;case '*':printf("Result = %.2f", a * b);break;case '/':if (b != 0)printf("Result = %.2f", a / b);elseprintf("Division by zero is not allowed");break;default:printf("Invalid operator");}return 0;}
Output:
Enter two numbers: 20 10Enter operator (+, -, *, /): +Result = 30.00
Explanation:
The program compares the operator entered by the user with the available cases. If the user enters +, addition is performed; if the user enters -, subtraction is performed, and so on. The division case also checks that the second number is not zero.
इस प्रोग्राम में यूजर द्वारा दिए गए ऑपरेटर की तुलना सभी उपलब्ध case से की जाती है। यदि यूजर + देता है, तो Addition होता है; यदि - देता है, तो Subtraction होता है। इसी प्रकार अन्य ऑपरेशन्स भी किए जाते हैं। Division के case में यह भी जाँचा जाता है कि दूसरी संख्या शून्य न हो।
Important Points About Switch Case Statement
- The switch expression must have an integer type, including
charandenumtypes.Switch एक्सप्रेशन का टाइप इंटीजर होना चाहिए, जिसमेंcharऔरenumभी शामिल हैं। - Case labels must be constant integral expressions. Duplicate case values are not allowed.Case labels कॉन्स्टेंट इंटीजर एक्सप्रेशन होने चाहिए। एक ही switch में दो समान case वैल्यूज की अनुमति नहीं होती है।
- The
breakstatement is used to exit the switch statement.breakस्टेटमेंट का प्रयोग switch स्टेटमेंट से बाहर निकलने के लिए किया जाता है। - If
breakis omitted, execution may continue into the following cases. This is called fall-through.यदिbreakनहीं लगाया जाता है, तो प्रोग्राम अगले case के स्टेटमेंट्स भी निष्पादित कर सकता है। इसे Fall-through कहते हैं। - The
defaultcase is optional and executes when no case matches.defaultcase वैकल्पिक होता है और तब निष्पादित होता है जब कोई भी case मैच नहीं होता है। - The
defaultcase can be placed anywhere inside the switch body, although it is generally placed at the end.defaultcase को switch बॉडी में कहीं भी लिखा जा सकता है, लेकिन सामान्यतः इसे अंत में लिखा जाता है। - Multiple case labels can share the same statements.एक से अधिक case labels के लिए समान statements का उपयोग किया जा सकता है।
Difference Between if-else and Switch Case
| if-else Statement | switch Statement |
|---|---|
| It can test relational and logical conditions. | It compares an expression with constant case values. |
| It can work with ranges and complex conditions. | It is suitable for selecting among discrete constant values. |
| It supports different data types through conditions. | Its expression must have an integer type. |
| It does not use case labels. | It uses case and optionally default labels. |
if-else स्टेटमेंट | switch स्टेटमेंट |
|---|---|
| इसमें Relational और Logical Conditions की जाँच की जा सकती है। | इसमें एक्सप्रेशन की तुलना कॉन्स्टेंट case वैल्यूज से की जाती है। |
| इसका उपयोग Range और Complex Conditions के लिए किया जा सकता है। | इसका उपयोग अलग-अलग कॉन्स्टेंट वैल्यूज में से चयन करने के लिए उपयुक्त है। |
| Conditions के माध्यम से विभिन्न डेटा टाइप पर कार्य किया जा सकता है। | इसका एक्सप्रेशन इंटीजर टाइप का होना चाहिए। |
| इसमें case labels का प्रयोग नहीं होता है। | इसमें case और वैकल्पिक default labels का प्रयोग होता है। |
The switch case statement is useful when a program needs to select one option from multiple fixed choices. It makes such programs easier to read and organize. It is commonly used in menu-driven programs, calculators, and day or month selection programs.
जब किसी प्रोग्राम में कई निश्चित विकल्पों में से किसी एक विकल्प का चयन करना हो, तब switch case स्टेटमेंट उपयोगी होता है। इससे प्रोग्राम को पढ़ना और व्यवस्थित करना आसान हो जाता है। इसका उपयोग Menu-Driven Programs, Calculators तथा Day या Month Selection Programs में किया जाता है.
Comments
Post a Comment