When I create a switch
statement in VS2008 C# like this (contrived):
switch (state) {
case '1':
state = '2';
case '2':
state = '1';
}
it complains that I'm not allowed to drop through:
Control cannot fall through from one case label ('case '1' (0x31):') to another
If you're not allowed to drop through, then what is the purpose of the break
statement at all? Why didn't the language designers just leave it out and automatically jump to the end of the switch
statement instead of forcing us to put in an unnecessary construct?
Basically to make it more familiar to C/C++/Java developers. Personally I think it was a mistake, but that's the reasoning.
I would have preferred a forced block:
case '1':
{
}
Aside from anything else, that would have avoided the weird variable scoping situations for switch/case. You could still have multiple case labels, of course:
case '0':
case '1':
{
}
It might also be nice to be able to list multiple cases more simply:
case '0', '1':
{
}
Oh, and a slight nit-pick about your description of the existing language: you don't have to have a break. It's just that the end of the case has to be unreachable. You can also have throw
, goto
or return
. There may be others that I've missed, too :)