switch-case Statement
switch-case Statement
switch Statement Syntax
The switch statement is a multi-way decision statement. It tests a variable against a list of constant values (cases) and executes the matching block.
switch (expression) { case value1: // code for case 1 break; case value2: // code for case 2 break; default: // code if no case matches }
expressionmust evaluate to an integer or char (not float/double)- Case values must be constants (literals or #define values)
- Each case ends with
break(usually) defaultis optional — runs when no case matches
How switch Works
- Evaluate the
switch(expression) - Compare result with each
casevalue from top to bottom - On match → jump to that case and start executing
- Execute until a
breakis encountered (or switch ends) - If no match → execute
default(if present)
The Role of break
Without break, execution "falls through" into the next case! This is a feature (fall-through) but usually a bug for beginners.
int x = 2; switch(x) { case 1: printf("One\\n"); case 2: printf("Two\\n"); // Matches here case 3: printf("Three\\n"); // Falls through! No break in case 2 default: printf("Default\\n"); // Also falls through! }
Unless you intentionally want fall-through, always put break; at the end of each case. Forgetting break is a very common C bug.
The default Case
default runs when no case matches. It's optional but recommended as a safety net.
Complete Programs
Example 1: Day of Week
#include <stdio.h> int main() { int day; printf("Enter day number (1-7): "); scanf("%d", &day); 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! Enter 1-7\\n"); } return 0; }
Example 2: Calculator using switch
#include <stdio.h> int main() { float a, b; char op; printf("Enter: num op num (e.g. 5 + 3): "); scanf("%f %c %f", &a, &op, &b); switch(op) { case '+': printf("= %.2f\\n", a + b); break; case '-': printf("= %.2f\\n", a - b); break; case '*': printf("= %.2f\\n", a * b); break; case '/': if(b != 0) printf("= %.2f\\n", a / b); else printf("Division by zero!\\n"); break; default: printf("Unknown operator\\n"); } return 0; }
Example 3: Vowel or Consonant (char switch)
#include <stdio.h> int main() { char ch; printf("Enter a letter: "); scanf("%c", &ch); switch(ch) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': printf("%c is a Vowel\\n", ch); break; default: printf("%c is a Consonant\\n", ch); } return 0; }
switch vs if-else
| Feature | switch | if-else |
|---|---|---|
| Condition type | Only equality (==) with integers/chars | Any condition (range, logical, etc.) |
| Readability | Cleaner for many discrete values | Better for complex conditions |
| Performance | Faster (uses jump table) | Slower for many conditions |
| Float/double | ❌ Not allowed | ✅ Allowed |
| Multiple values | ✅ Multiple case labels | Needs OR (||) operator |
Summary
switchmatches an expression against constantcasevalues- Without
break, execution falls through to next case defaultruns when no case matches- Only works with
intandchar— not float, double, or strings - Multiple case labels can share one code block
- Use switch when checking one variable against many discrete values
Write: (1) Month name from number (1=January...) (2) Season from month number (3) Simple menu with 4 operations (4) Direction from WASD key input.
switch Statement का Syntax
switch statement एक multi-way decision statement है। यह एक variable को constant values (cases) की list के साथ match करता है और matching block execute करता है।
switch (expression) { case value1: // case 1 का code break; case value2: // case 2 का code break; default: // कोई case match न हो तो }
switch कैसे काम करता है
switch(expression)evaluate करो- Result को हर
casevalue से ऊपर से नीचे compare करो - Match मिले → उस case पर jump करो और execute शुरू
breakमिले तक execute करते रहो- कोई match नहीं →
defaultexecute करो
break का Role
break के बिना execution "fall-through" होती है — यानी अगले case में भी चली जाती है! यह beginners के लिए आम bug है।
जब तक intentionally fall-through नहीं चाहिए, हर case के अंत में break; ज़रूर लिखें।
Programs
Day of Week
int day = 5; switch(day) { case 1: printf("Somvar\\n"); break; case 2: printf("Mangalvar\\n"); break; case 5: printf("Shukravar\\n"); break; case 7: printf("Ravivar\\n"); break; default: printf("Invalid\\n"); }
Calculator Program
float a = 10, b = 4; char op = '*'; switch(op) { case '+': printf("= %.2f\\n", a+b); break; case '-': printf("= %.2f\\n", a-b); break; case '*': printf("= %.2f\\n", a*b); break; case '/': printf("= %.2f\\n", a/b); break; default: printf("Invalid operator\\n"); }
सारांश
switchexpression को constantcasevalues से match करता हैbreakके बिना fall-through होती है — हमेशा break लिखेंdefaultतब चलता है जब कोई case match नहीं करता- केवल
intऔरcharwork करते हैं — float/double/string नहीं - जब एक variable को many discrete values से check करना हो तो switch use करें