I\'ve always wondered this - why can\'t you declare variables after a case label in a switch statement? In C++ you can declare variables pretty much anywhere (and declaring
Consider:
switch(val)
{
case VAL:
int newVal = 42;
default:
int newVal = 23;
}
In the absence of break statements, sometimes newVal gets declared twice, and you don't know whether it does until runtime. My guess is that the limitation is because of this kind of confusion. What would the scope of newVal be? Convention would dictate that it would be the whole of the switch block (between the braces).
I'm no C++ programmer, but in C:
switch(val) {
int x;
case VAL:
x=1;
}
Works fine. Declaring a variable inside a switch block is fine. Declaring after a case guard is not.