
Switch Statement in C
The switch
statement in C is used to execute different blocks of code based on the value of an expression.
"The switch statement provides a cleaner way to handle multiple conditions than multiple if-else statements."
What is a Switch Statement?
The switch
statement allows a variable to be tested for equality against multiple cases. Each case has a block of code that executes if the case matches the variable.
Syntax of Switch Statement
switch(expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
...
default:
// Code to execute if no case matches
}
Flowchart of Switch Statement
The flowchart below explains how the switch statement works:
- The expression inside
switch
is evaluated. - If it matches a case, that block of code runs.
- If no case matches, the
default
block (if provided) runs. - The
break
statement exits the switch after executing a case.
Example: Simple Switch Statement
#include <stdio.h>
int main() {
int choice;
printf("Enter a number (1-3): ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("You selected One.\n");
break;
case 2:
printf("You selected Two.\n");
break;
case 3:
printf("You selected Three.\n");
break;
default:
printf("Invalid choice!\n");
}
return 0;
}
Example: Switch with Character Input
#include <stdio.h>
int main() {
char grade;
printf("Enter your grade (A, B, C, D, F): ");
scanf(" %c", &grade);
switch(grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Good job!\n");
break;
case 'C':
printf("You passed.\n");
break;
case 'D':
printf("Try harder.\n");
break;
case 'F':
printf("Failed. Study more!\n");
break;
default:
printf("Invalid grade.\n");
}
return 0;
}
Example: Switch Without Break (Fall Through)
#include <stdio.h>
int main() {
int num = 2;
switch(num) {
case 1:
printf("Case 1\n");
case 2:
printf("Case 2\n");
case 3:
printf("Case 3\n");
default:
printf("Default case\n");
}
return 0;
}
Key Concepts in Switch Statement
Concept | Description |
---|---|
break |
Exits the switch after executing a case. |
default |
Runs if no case matches. |
Fall Through | If break is not used, execution continues to the next case. |
Best Practices for Using Switch Statement
- Always use
break
to prevent fall-through unless explicitly needed. - Use
default
to handle unexpected values. - Switch works only with integers and characters (not floating points).
Example: Using Enum with Switch
#include <stdio.h>
enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };
int main() {
enum Days today = WEDNESDAY;
switch(today) {
case SUNDAY:
printf("It's a holiday!\n");
break;
case MONDAY:
printf("Start of the workweek.\n");
break;
case WEDNESDAY:
printf("Midweek!\n");
break;
case FRIDAY:
printf("Weekend is near!\n");
break;
default:
printf("Just another day.\n");
}
return 0;
}
Conclusion
The switch
statement is useful for handling multiple conditions in a structured way. It improves readability and reduces the complexity of multiple if-else
statements. Mastering it will help you write efficient C programs! 🚀