switch Statements
suggest changeswitch statements are useful when you want to have your program do many different things according to the value of a particular test variable.
An example usage of switch statement is like this:
int a = 1;
switch (a) {
case 1:
puts("a is 1");
break;
case 2:
puts("a is 2");
break;
default:
puts("a is neither 1 nor 2");
break;
}
With those break statements, this example is equivalent to
int a = 1;
if (a == 1) {
puts("a is 1");
} else if (a == 2) {
puts("a is 2");
} else {
puts("a is neither 1 nor 2");
}
If the value of a is 1 when the switch statement is used, a is 1 will be printed. If the value of a is 2 then, a is 2 will be printed. Otherwise, a is neither 1 nor 2 will be printed.
The controlling expression must have integer type after the integer promotions. A case label is an integer constant expression. The same value can appear at most once in one switch.
default: is taken when the value matches no case label. A default arm is optional. It is useful when unexpected values should be reported.
break leaves the switch. It is not required. Without it, execution continues into the next case. That is fall-through: later statements run whether or not their labels match, until a break or the end of the switch. Fall-through is how Duff's device shares one loop body among several entry points. Many compilers warn about an unlabeled fall-through because it is a common accident.
Below is an example that shows effects of the absence of break;:
int a = 1;
switch (a) {
case 1:
case 2:
puts("a is 1 or 2");
case 3:
puts("a is 1, 2 or 3");
break;
default:
puts("a is neither 1, 2 nor 3");
break;
}
When the value of a is 1 or 2, a is 1 or 2 and a is 1, 2 or 3 will both be printed. When a is 3, only a is 1, 2 or 3 will be printed. Otherwise, a is neither 1, 2 nor 3 will be printed.
Note that the default case is not necessary, especially when the set of values you get in the switch is finished and known at compile time.
The best example is using a switch on an enum.
enum msg_type { ACK, PING, ERROR };
void f(enum msg_type t)
{
switch (t) {
case ACK:
// do nothing
break;
case PING:
// do something
break;
case ERROR:
// do something else
break;
}
}
There are multiple advantages of doing this:
- most compilers will report a warning if you don’t handle a value (this would not be reported if a
defaultcase were present) - for the same reason, if you add a new value to the
enum, you will be notified of all the places where you forgot to handle the new value (with adefaultcase, you would need to manually explore your code searching for such cases) - The reader does not have to guess what a
defaultarm is hiding: other enumerators, or a guard added just in case. If other enumerators exist, it is also unclear whetherdefaulthandles them on purpose. - handling each
enumvalue makes the code self explanatory as you can’t hide behind a wild card, you must explicitly handle each of them.
An enumerator list does not limit the values an enum object can hold. This conversion is to the enum’s integer type. If 666 fits in that type, the result is simply a value that matches no case, and the switch does nothing. It is not undefined behavior by itself:
enum msg_type t = (enum msg_type)666;
A check before the switch can reject that value when the program needs to:
void f(enum msg_type t)
{
if (!is_msg_type_valid(t)) {
// Handle this unlikely error
}
switch(t) {
/* same cases as above */
}
}