Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to use break?

I was wondering why C# requires me to use break in a switch statement although a fall-through semantics is by definition not allowed. hence, the compiler could generate the break at the end of each case-block and save me the hassle.

However, there is one scenario (which has already been discussed on this site) which I could come up with that might be the reason for the explicit usage of break:

switch (foo) {
    case 0:
    case 1:
        bar();
        break;
    default:
        break;
}

Here, the method bar() is called if foo has either the value 0 or 1.

This option would break in the truest sense of the word if the compiler would generate break statements by itself. Is this it, is this the reason why the break is compulsory or are there any other good reasons?

like image 859
Uwe Honekamp Avatar asked Oct 14 '09 19:10

Uwe Honekamp


People also ask

What is the purpose of using break?

The break statement terminates the execution of the nearest enclosing do , for , switch , or while statement in which it appears.

Is it good practice to use break?

break is a completely acceptable statement to use (so is continue, btw). It's all about code readability -- as long as you don't have overcomplicated loops and such, it's fine.

Is Break necessary?

No, syntactically and semantically a break is not required.

Why is break needed in switch?

You can use the break statement to end processing of a particular labeled statement within the switch statement. It branches to the end of the switch statement. Without break , the program continues to the next labeled statement, executing the statements until a break or the end of the statement is reached.


2 Answers

The question presupposes a falsehood and therefore cannot be answered. The language does NOT require a break at the end of a switch section. The language requires that the statement list of a switch section must have an unreachable end point. "break" is just the most commonly used statement that has this property. It is perfectly legal to end a switch section with "return;", "goto case 2;", "throw new Exception();" or "while (true) {}" (or in some cases, continue, though that's usually a bad idea.)

The reason we require a statement list with an unreachable endpoint is to enforce the "no fall through" rule.

If your question is better stated as "why doesn't the compiler automatically fix my error when I fail to produce a switch section with a statement list with an unreachable end point, by inserting a break statement on my behalf?", then the answer is that C# is not a "make a guess about what the developer probably meant and muddle on through" sort of language. Some languages are -- JScript, for example -- but C# is not.

We believe that C# programmers who make mistakes want to be told about those mistakes so that they can intentionally and deliberately correct them, and that this is a good thing. Our customers tell us that they do not want the compiler to make a guess about what they meant and hope for the best. That's why C# also does not make a guess about where you meant to put that missing semicolon, as JScript does, or silently fail when you attempt to modify a read-only variable, as JScript does, and so on.

like image 125
Eric Lippert Avatar answered Sep 28 '22 03:09

Eric Lippert


I suspect that the reason C# requires the developer to place a break or terminal statement at the end of each case is for clarity.

It avoids newcomers to the language from assuming that switch( ) in C# behaves like switch in C or C++ where fall through behavior occurs. Only in the cases of adjacent empty cases does fall through occur in C# - which is relatively obvious.

EDIT: Actually, in C# fallthrough is always illegal. What is legal, however, is having a single case associated with two or more labels. Eric Lippert writes at length about this behavior and how it differs from C/C++ switch statements.

You may be interested in reading this article on Eric Lipperts blog.

like image 31
LBushkin Avatar answered Sep 28 '22 04:09

LBushkin