🟢 Beginner · Lesson 12
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.
Syntax
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.
C Language – Without break (Fall-through)
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! }
Two
Three
Default
⚠️ Always Use break!
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
C Language
#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; }
Enter day number (1-7): 5
Friday
Example 2: Calculator using switch
C Language – Switch Calculator
#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; }
Enter: num op num (e.g. 5 + 3): 10 * 4
= 40.00
Example 3: Vowel or Consonant (char switch)
C Language – Multiple case labels
#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; }
Enter a letter: A
A is a Vowel
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
🏋️ Practice
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.
Frequently Asked Questions
What is a switch statement in C?
A
switch statement chooses one block of code to run based on the value of a variable. It compares the variable against several case labels and runs the matching one, offering a cleaner alternative to a long chain of if-else statements.Why is break used in a switch statement?
break ends the switch after a matching case runs. Without it, execution "falls through" and keeps running the following cases too. So you normally put a break at the end of each case unless you deliberately want fall-through.What is the default case in a switch?
The
default case runs when none of the other case labels match the value. It works like the else of a switch, letting you handle unexpected or leftover values. It is optional but usually good practice to include.What is the difference between switch and if-else in C?
A
switch tests one variable against several constant values and is often clearer for many fixed options. if-else can test any condition, including ranges and complex expressions, so it is more flexible. Use switch for many equality checks on one value, if-else for everything else.What data types can a switch use in C?
A C
switch works with integer types and characters, since a char is an integer underneath. It cannot switch on floating-point numbers or strings, and every case label must be a constant value, not a variable or range.💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.