views:

377

answers:

3

Hi there, I'm having hard time understanding, why the compiler requires using break statement. It's not possible to miss it since the fall through is now allowed. I see the reason for the break in C or C++, but is it needed here.

Why it's not a built-in behavior to break after a case is over ? Isn't it just a syntax with no semantic?

Sorry, if it's a stupid question.

EDIT: The fall through is allowed only when the case is empty. When there is a statement there you can't omit the break statement. So, it's a different matter.

+2  A: 

The break statement in c# was a design decision by the creators of the language...Essentially they wanted an "unambiguous" break statement, a break statement that would only work one way. In short, they didn't want fall-through, and if they had just prevented fall-through without including "break," it would have broken backwards compatibility with c++.

Robert Harvey
OK why then this behavior is not built-in without the need of explicit break statement in the end of the cases. They can do it implicitly, right ?
anthares
They wanted to be *explicit* about not falling through. The required `break` statement is a *declaration* that the code will not fall through.
Robert Harvey
+6  A: 

The compiler doesn't 'need' the break statements, it demands them.

This was a design decision, comparable with requiring the use of 'ref' when calling a method with a pass-by-reference parameter.

It keeps the code semantically close to C and C++ while eliminating the pitfall of what was always a debatable 'feature' of the C languages.

Henk Holterman
So, it just for clarification and has not any actual role ?
anthares
@anthares: You could say that. The break could easily be inserted by the compiler, the programmer has no choice here. But 'readability' is an important role for language constructs.
Henk Holterman
It does serve a role. Switch is translated to goto statements in the IL and that is to have greater speed. lets say you have 10-20 cases; With if/else if you would have an average of 10 comparisons to get to the right case where as with the goto generated is much faster.
ThanosPapathanasiou
@ ThanosPapathanasiou: that makes no sense. Eliminating the break does not make the compiler treat the switch any different.
Henk Holterman
That was may wondering exactly - does it have a significant role for the compiler or it is a "readability" / "clarification" feature. Thanks!
anthares
@Henk Holterman: yes it doesn't change how the compiler treats the switch but it clarifies where to place another jump command to exit the switch. maybe I didn't express myself well enough before.
ThanosPapathanasiou
Thanos, I don't believe that either. The compiler can assume a break any time it needs to, and apply other optimizations as well.
Henk Holterman
I agree with Henk. I don't think that there is need to clarify where to place another jump command to exit the switch. It is well-known without the break.
anthares
+1  A: 

Usually this kind of code is a bug:

// Contrived calculator demostration
decimal x = 5m;
decimal y = 10m;
decimal result = 0m;
string blah = "Divide";

// .. other code omitted

switch(blah) {
    case "Divide":
        result = x / y;

    case "Multiply":
        result = x * y;

    case "Add":
        result = x + y;

    case "Subtract":
        result = x - y;

    default:
        MessageBox.Show("Not a valid operation");

}

However, the compiler can't assume the missing breaks are a bug. As far as it knows, you really did want the cases to fall-through.

Simply assuming that breaks should be at the end of every case would just trade one bug for a different bug.

So, instead, the language designers disallowed fall-through from non-empty cases and throw an error if you omit them.

If you need code shared between non-empty cases, put it in a private (possibly static) method and call it from there.

One last note: Empty cases falling through is all that an empty case is expected to do, which is why it's allowed.

R. Bemrose
Actually the question was why language designers throw an error, instead of just implicitly break your case after it's done.
anthares
But "The compiler can't assume the missing breaks are a bug." only because it is written this way. Since the fall-through in non empty cases is not allowed the question is way the compiler throws an error instead assume the only one allowed behavior.
anthares
the compiler can't assume the missing breaks are a bug. As far as it knows, you really did want the cases to fall-through. Simply assuming that breaks should be at the end of every case would just trade one bug for a different bug.
R. Bemrose
You can explicitly "fall-through" with a `goto case` statement: http://msdn.microsoft.com/en-us/library/13940fs2.aspx
280Z28
But this is different case. It is an instruction that will be executed before the end of the case. So, it will not mess with the proposed by me logic - "if reach the end of the case, put a break".
anthares