C Switch Case Statement
Learn what switch-case is, why it is used, and when to use it with examples.
जानें कि स्विच-केस क्या है, क्यों उपयोग करते हैं और कब उपयोग करना चाहिए, उदाहरणों के साथ।
The switch
statement in C is a control structure that allows multi-way branching based on the value of an expression. It is often cleaner and more efficient than writing multiple if-else
statements.
- Why use switch? It improves readability when checking a single variable against many constant values.
- When to use switch? Use when you have discrete constant options (e.g. menu selection, day of week, grade calculation).
C में switch
स्टेटमेंट एक नियंत्रण संरचना है जो किसी एक्सप्रेशन के मान के आधार पर अलग-अलग केस चुनने की अनुमति देता है। यह कई if-else
स्टेटमेंट लिखने की तुलना में साफ और प्रभावी होता है।
- क्यों उपयोग करें? जब किसी एक वेरिएबल की तुलना कई स्थिर मानों से करनी हो, तो यह कोड को पढ़ने योग्य बनाता है।
- कब उपयोग करें? जब आपके पास सीमित विकल्प हों जैसे मेन्यू चयन, सप्ताह का दिन, ग्रेड गणना।
Example 1: Simple switch case
#include <stdio.h>
int main() {
int choice = 2;
switch(choice) {
case 1: printf("Option 1\n"); break;
case 2: printf("Option 2\n"); break;
case 3: printf("Option 3\n"); break;
default: printf("Invalid option\n");
}
return 0;
}
Outputs based on single variable value
एक वेरिएबल के मान पर आउटपुट बदलता है
Output: Option 2
Example 2: Day of Week
#include <stdio.h>
int main() {
int day = 5;
switch(day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
case 6: printf("Saturday\n"); break;
case 7: printf("Sunday\n"); break;
default: printf("Invalid day\n");
}
return 0;
}
Prints weekday name based on number
संख्या के आधार पर सप्ताह का दिन प्रिंट करता है
Output: Friday
Example 3: Grade Evaluation
#include <stdio.h>
int main() {
char grade = 'B';
switch(grade) {
case 'A': printf("Excellent\n"); break;
case 'B': printf("Good\n"); break;
case 'C': printf("Average\n"); break;
case 'D': printf("Poor\n"); break;
default: printf("Fail\n");
}
return 0;
}
Evaluates student grade
ग्रेड के आधार पर मूल्यांकन करता है
Output: Good
Example 4: Calculator
#include <stdio.h>
int main() {
char op = '+';
int a = 10, b = 20;
switch(op) {
case '+': printf("%d\n", a+b); break;
case '-': printf("%d\n", a-b); break;
case '*': printf("%d\n", a*b); break;
case '/': printf("%d\n", a/b); break;
default: printf("Invalid operator\n");
}
return 0;
}
Performs operation based on char operator
ऑपरेटर के आधार पर गणना करता है
Output: 30
Example 5: Nested switch
#include <stdio.h>
int main() {
int x = 1, y = 2;
switch(x) {
case 1:
switch(y) {
case 2: printf("x=1, y=2\n"); break;
default: printf("Inner default\n");
}
break;
default: printf("Outer default\n");
}
return 0;
}
Switch inside another switch
स्विच के अंदर स्विच
Output: x=1, y=2
Example 6: Fallthrough demo
#include <stdio.h>
int main() {
int n = 2;
switch(n) {
case 1: printf("One\n");
case 2: printf("Two\n");
case 3: printf("Three\n");
default: printf("Done\n");
}
return 0;
}
Shows effect without break statements
बिना ब्रेक के प्रभाव
Output: Two
Three
Done
Example 7: Switch with enum
#include <stdio.h>
enum Colors {RED, GREEN, BLUE};
int main() {
enum Colors c = BLUE;
switch(c) {
case RED: printf("Red\n"); break;
case GREEN: printf("Green\n"); break;
case BLUE: printf("Blue\n"); break;
}
return 0;
}
Switch with enum constants
एनम कॉन्स्टेंट्स के साथ स्विच
Output: Blue
Example 8: Multiple cases same output
#include <stdio.h>
int main() {
int n = 3;
switch(n) {
case 1:
case 2:
case 3: printf("Small number\n"); break;
default: printf("Large number\n");
}
return 0;
}
Groups multiple cases together
कई केस का एक आउटपुट
Output: Small number
Example 9: Char vs int switch
#include <stdio.h>
int main() {
char c = 'A';
switch(c) {
case 'A': printf("Letter A\n"); break;
case 'B': printf("Letter B\n"); break;
default: printf("Other letter\n");
}
return 0;
}
Shows switch on characters
कैरेक्टर पर स्विच
Output: Letter A
Example 10: Default only
#include <stdio.h>
int main() {
int n = 100;
switch(n) {
default: printf("No match found\n");
}
return 0;
}
Only default executes if no match
मैच न होने पर केवल डिफ़ॉल्ट चलता है
Output: No match found