C - Switch with multiple case numbers
Categories:
C Switch Statement with Multiple Case Numbers
Learn how to effectively use the switch
statement in C to handle multiple case values for a single block of code, improving readability and efficiency.
The switch
statement in C is a powerful control flow construct that allows a program to execute different blocks of code based on the value of an expression. While typically each case
label corresponds to a unique value, there are common scenarios where you want the same code to execute for several different input values. This article explores various techniques to achieve this, focusing on readability and best practices.
The Fall-Through Mechanism
The most common and idiomatic way to handle multiple case numbers for a single block of code in C's switch
statement is by utilizing the 'fall-through' mechanism. In C, if a case
block does not end with a break
statement, execution will 'fall through' to the next case
label. By intentionally omitting break
statements between multiple case
labels, you can group them to execute the same code.
#include <stdio.h>
int main() {
int day = 6;
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5:
printf("It's a weekday.\n");
break;
case 6:
case 7:
printf("It's the weekend!\n");
break;
default:
printf("Invalid day.\n");
break;
}
return 0;
}
Using fall-through for multiple case labels
flowchart TD A[Start] --> B{Evaluate 'day'}; B --> |day = 1,2,3,4,5| C[Print "It's a weekday."]; B --> |day = 6,7| D[Print "It's the weekend!"]; B --> |default| E[Print "Invalid day."]; C --> F[End]; D --> F; E --> F;
Flowchart illustrating the switch statement with grouped cases
// fall-through
to improve code clarity and prevent future maintainers from mistakenly adding a break
.Alternative: Using Logical OR (if-else if)
Although the switch
statement is often preferred for its clarity with discrete values, an if-else if
ladder using logical OR (||
) can also achieve the same outcome. This approach might be more suitable when the conditions are not simple equality checks or when the values are not contiguous.
#include <stdio.h>
int main() {
int day = 6;
if (day == 1 || day == 2 || day == 3 || day == 4 || day == 5) {
printf("It's a weekday.\n");
} else if (day == 6 || day == 7) {
printf("It's the weekend!\n");
} else {
printf("Invalid day.\n");
}
return 0;
}
Achieving multiple case logic with if-else if and logical OR
switch
statement with fall-through is generally more readable and often more efficient than a long if-else if
chain. However, for complex conditions or ranges, if-else if
is more flexible.Best Practices and Considerations
When dealing with multiple case numbers, consider the following to write robust and maintainable code:
- Readability: The fall-through method is highly readable for grouping cases. Ensure your
case
labels are ordered logically. default
Case: Always include adefault
case to handle unexpected or invalid input values. This makes your code more robust.break
Statements: Remember thatbreak
statements are crucial to prevent unintended fall-through. Only omit them when you explicitly want execution to continue to the nextcase
.- Ranges: For checking if a value falls within a range (e.g.,
case 1 ... 5
in GCC extensions), standard C does not support this directly withinswitch
. You would typically use anif-else if
structure or a series of fall-through cases for discrete values within the range.