tags:

views:

671

answers:

4

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?

+8  A: 

If you don't have any code in case 1, you are allowed to fall through, so you can say that "all of these cases share this piece of code"

simendsjo
But in that case you will not get a warning. The compiler is warning you about something it can infer.
Kobi
+32  A: 

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 :)

Jon Skeet
@Jon +1 - Never knew you could do that!
GenericTypeTea
@GenericTypeTea: Which bit?
Jon Skeet
I think it's a good decision not to make identically looking constructs behaving differently. That would be a hell for programmers who have to switch languages frequently.
ammoQ
I also like the '0', '1' syntax. The D programming language has a much nicer switch IMO: http://www.digitalmars.com/d/2.0/statement.html#SwitchStatementIt allows case 1,2,3: and case 1: .. case 10 for from to
simendsjo
@Jon - never mind, I completely misread your answer. "I would have preferred a forced block"... I thought you said you could use a code block. I got a bit over excited there.
GenericTypeTea
@GenericTypeTea: You certainly *can* put a block there even now - but you'll still need a break, and you aren't *forced* to use a block.
Jon Skeet
@Jon - the braces are pretty redundant in C# then? So, what you're saying is that it would of made more sense if they would auto-break for you, thereby negating the need to write `break;return;throw;etc;` every time?
GenericTypeTea
@GenericTypeTea: No, they're not redundant: they define a new scope. As it is, variables declared "directly" in the case are in the scope of the whole switch statement, which is weird. I'd like the braces to be enforced, but for that to effectively have an automatic break. You would still be able to return or throw if necessary.
Jon Skeet
@Jon - Thanks for the explanation! Nice to learn something new about such a fundamental aspect of the language.
GenericTypeTea
I agree, the scoping for switch statements is confusing. It leads me to have to declare a bunch of variables before I begin the switch block. Although it does point out something not immediately obvious: that the runtime can use a variable as soon as it's in scope, not only below that line of code - this is something that can seem odd when debugging and moving the line of execution around.
LoveMeSomeCode
Perhaps the best solution would have been for a /switch-section/ to take an /embedded-statement/ rather than a /statement-list/. It would then match `if`, `while`, `for`, etc. No break would be required. You get one statement. More than that, and you put braces. That would have been awesome.
Jeffrey L Whitledge
@Jeffrey - in other words, a Pascal case statement.
Muhammad Alkarouri
+9  A: 

You are allowed to drop through, but you have to do so explicitly with the goto keyword:

switch (state) {
    case '1':
        state = '2';
        goto case '2';
    case '2':
        state = '1';
        break;
}

You can break or gotoin C#, but what you cannot do is not state which you want, because that's a potential source of hard-to-spot bugs.

It's a lot easier to spot that your code says goto when you wanted break (or vice versa) than it is to spot that you forgot to add either.

It might sound stupid, but many a tired two-hour search for the cause of a C++ bug ends in a sudden realisation that you forgot to add a break and your code is falling through all the time. C# avoids that by forcing you to state what you want.

Chris
I'm embarrassed I didn't know you could 'continue'. Just thought it was illegal to avoid all fall through bugs... Thanks :)
simendsjo
Did you try this code? It doesn't compile. You can do "goto case '2'" though.
Jon Skeet
@Jon, I just plugged that into VS2008 and it worked okay. That's running within the IDE though I woudn't think it would be different outside??
paxdiablo
Gives me "No enclosing loop out of which to break or continue"
Alex K.
Actually, you may be right, my test _is_ within a while loop (I plugged it into my current project). That means this won't drop through so much as restart the loop - that would be a very annoying bug :-)
paxdiablo
Indeed. This answer is wrong. To go on to the next case, use "goto case blah". Continue is only valid in a loop.
Eric Lippert
Okay, if people are going to keep upvoting this, let's at least make it right :-) Edited to use `goto` instead of `continue`.
paxdiablo
+15  A: 

From the horses mouth (MSDN) Why is the C# switch statement designed to not allow fall-through, but still require a break?.

Quoting the salient bits, this is why they don't allow fall-through:

This implicit fall-through behavior is often used to reduce the amount of code needed and often isn't an issue the first time that code is written. However, as code moves from the initial development phase into a maintenance phase, the code above can lead to subtle errors that are very hard to debug. These errors result from the very common mistake of the developer adding a case, yet forgetting to put a break at the end of the block.

In C#, the switch statement requires that explicit flow control occur at the end of a case, either a break, goto, return, or throw. If the developer desires fall-through semantics, it can be achieved by an explicit goto at the end of a case statement.

And this is why it's not automatic:

As a result of the C# rules requiring explicit flow-control to occur at the end of a case block (most usually a break), many people question why the behavior simply wasn't changed such that fall-through didn't occur. That is, don't make break required, simply change the semantics of switch to not have fall-through for cases. The reason this wasn't done was so that developers who were very used to C++ wouldn't have a hard time understanding what a switch statement was doing.

Alex K.