Switch Case Statement in C Programming
Switch Case Statement in C Programming
Introduction to Switch Case Statement
The switch case statement in C programming is used to select one block of code from multiple options. It is commonly used when a single variable needs to be compared with different values.
The switch case statement in C programming improves readability and is a better alternative to long else if ladders.
Syntax of Switch Case Statement in C
case value1:
// code block
break;
case value2:
// code block
break;
default:
// default code
}
How Switch Case Statement Works
- The expression is evaluated once
- It is compared with each case value
- When a match is found, the corresponding block executes
- The
breakstatement stops further execution - If no match is found, the default block runs
Example of Switch Case Statement in C
int main() {
int day = 2;
switch(day) {
case 1:
printf(“Monday”);
break;
case 2:
printf(“Tuesday”);
break;
case 3:
printf(“Wednesday”);
break;
default:
printf(“Invalid day”);
}
return 0;
}
Example with User Input
int main() {
int choice;
printf(“Enter a number (1-3): “);
scanf(“%d”, &choice);
switch(choice) {
case 1:
printf(“Option 1 selected”);
break;
case 2:
printf(“Option 2 selected”);
break;
case 3:
printf(“Option 3 selected”);
break;
default:
printf(“Invalid input”);
}
return 0;
}
Important Rules of Switch Case Statement
Key Rules
- Use
breakafter each case defaultcase is optional but recommended- Case values must be constant
- Expression should return int or char
Advantages of Switch Case Statement
Benefits
- Makes code clean and readable
- Faster than multiple if-else in some cases
- Easy to manage multiple conditions
- Useful in menu-driven programs
When to Use Switch Case in C Programming
Use switch case statement in C programming when:
- You are comparing one variable with multiple values
- You want cleaner and more structured code
- You are building menu-based programs
Start Learning C Programming
Practice switch case programs to improve your understanding of control statements in C programming.
Summary
The switch case statement in C programming is used for multi-way decision making. It helps in writing structured, readable, and efficient code.
FAQs
What is switch case statement in C programming?
It is a control statement used to execute one block of code from multiple options.
Why use switch case instead of if-else?
Switch case is cleaner and easier to read when comparing one variable with many values.
What happens if break is not used?
The program executes all cases after the matched case (fall-through).
What is default in switch case?
It executes when no case matches the condition.



