There's a bit more nuance to Switch statements in different languages, C is particularly strange.
If you want a bit of history; the Switch statement in C is the primary replacement of the old Goto statement, it actually inherits the quirks of the goto statement. Despite using braces {} the C switch statement doesn't create a new stack-frame, which still catches me out sometimes. If you want to declare new variables for a switch in C/C++ you need to manually make a stack frame.
<div class="c" id="{CB}" style="font-family: monospace;"><ol><span style="color: #b1b100;">switch ( willy ) {
<span style="color: #b1b100;">case RATHER_SMALL:
<span style="color: #993333;">const <span style="color: #993333;">char * <span style="color: #993333;">const message = "Wow so small!"; // COMPILE ERROR HERE!!!
[url=http://www.opengroup.org/onlinepubs/009695399/functions/printf.html]printf[/url]( "%s<span style="color: #000099; font-weight: bold;">\n", message );
<span style="color: #000000; font-weight: bold;">break;
}
<span style="color: #b1b100;">switch ( willy ) {
<span style="color: #b1b100;">case RATHER_SMALL:
{
// This compiles fine as we added a new stack-frame with {}
<span style="color: #993333;">const <span style="color: #993333;">char * <span style="color: #993333;">const message = "Wow so small!";
[url=http://www.opengroup.org/onlinepubs/009695399/functions/printf.html]printf[/url]( "%s<span style="color: #000099; font-weight: bold;">\n", message );
}
<span style="color: #000000; font-weight: bold;">break;
}
This behaviour is literally here because the Goto statement jumps within a stack-frame, so the Switch statement operates within the current stack-frame; you cannot declare variables within a stack-frame (when you declare a variable in C/C++ the compiler secretly allocates them at the start of the stack-frame, C99 used to force programmers to declare their variables at the top because of this).
The Switch in C is probably the messiest part of the language. The history of the language collided with modern practices (being able to write variable declarations anywhere within the frame). I know some studios actually have the policy of never use the break statement, which covers most uses for switch statements (use if-else instead).
C/C++ only allows you to use integer types in switch statements - this is something other languages relaxes on (and treats the switch statement as a glorified if-else). Ruby lets you switch on anything, pretty much (including conditional statements; can be something like "when a != b").
I think another language to look at is Swift. C switches have the "break" keyword, which prevents the program from stepping into the next conditional in the switch statement (yes there are times when this is useful). In majority of cases, the programmer wants to break on every conditional, so you end up with breaks everywhere.
In C you can do this:
<div class="c" id="{CB}" style="font-family: monospace;"><ol><span style="color: #b1b100;">switch ( letter ) {
<span style="color: #b1b100;">case 'A':
<span style="color: #b1b100;">case 'a':
[url=http://www.opengroup.org/onlinepubs/009695399/functions/printf.html]printf[/url]( "<span style="color: #000099; font-weight: bold;">\"A<span style="color: #000099; font-weight: bold;">\"<span style="color: #000099; font-weight: bold;">\n" );
<span style="color: #000000; font-weight: bold;">break;
}
// Prints A when letter == 'A' or letter == 'a'
In Swift this fails due to a missing statement for case 'A'.
In Swift you can combine with a comma or use the Fallthrough keyword:
<div id="{CB}" style="font-family: monospace;"><ol>switch ( letter ) {
case 'A', case 'a':
print( "\"A\"" )
}
// Prints A when letter == 'A' or letter == 'a'
switch ( letter ) {
case 'A':
fallthrough
case 'a':
print( "\"A\"" )
}
// Does the same as above
So Swift breaks by default, requires fallthrough otherwise, uses the comma as an easy way to capture multiple cases.