Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java allow for labeled breaks on arbitrary statements?

I just learned today that the following Java code is perfectly legal:

myBlock: {
    /* ... code ... */

    if (doneExecutingThisBlock())
        break myBlock;

    /* ... more code ... */
}

Note that myBlock isn't a loop - it's just a block of code I've delimited with curly braces.

This seems like a rather strange feature to have. It means that you can use a named break to break out of an if statement or anonymous block, though you can't normally use a break statement in these contexts.

My question is this: is there a good reason for this design decision? That is, why make it so that you can only break out of certain enclosing statements using labeled breaks but not regular breaks? And why allow for this behavior at all? Given how (comparatively) well-designed Java is as a language I would assume there's a reason for this, but I honestly can't think of one.

like image 226
templatetypedef Avatar asked Feb 24 '11 02:02

templatetypedef


People also ask

What is the purpose of break statement with label?

The break Statement This is the output of the program. The break statement terminates the labeled statement; it does not transfer the flow of control to the label. Control flow is transferred to the statement immediately following the labeled (terminated) statement.

What is meant by Labelled break statement?

In Labelled Break Statement, we give a label/name to a loop. When this break statement is encountered with the label/name of the loop, it skips the execution any statement after it and takes the control right out of this labelled loop. And, the control goes to the first statement right after the loop.

What is the difference between break and Labelled break in Java?

Without a label, break will break out of the inner loop. With a label you can stop execution of nested loops. Save this answer.

What is the difference between an unlabeled and a labeled continue statement?

The labeled continue statement is similar to the unlabelled continue statement in the sense that both resume the iteration. The difference with the labeled continue statement is that it resumes operation from the target label defined in the code.


2 Answers

It is plausible that this was done for simplicity. If originally the labeled break can only break loop statements, then it should be immediately clear to language designer that the restriction isn't necessary, the semantics work the same for all statements. For the economics of the language spec, and simpler implementation of compilers, or just out of the habit towards generality, labeled break is defined for any statement, not just loop statements.

Now we can look back and judge this choice. Does it benefit programmers, by giving them extra expression power? Seems very little, the feature is rarely used. Does it cost programmers in learning and understanding? Seems so, as evidenced by this discussion.

If you could go back time and change it, would you? I can't say I would. We have a fetish for generality.

If in a parallel universe it was limited to loop statements only, there is still a chance, probably much smaller, that someone posts the question on stackoverflow: why couldn't it work on arbitrary statements?

like image 189
irreputable Avatar answered Oct 04 '22 02:10

irreputable


Think of it as a return statement that returns from the block instead of from the entire function. The same reasoning you apply to object to break being scattered anywhere can also be applied to return being allowed anywhere except at the end of a function.

like image 41
Ted Hopp Avatar answered Oct 04 '22 02:10

Ted Hopp