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 break
s but not regular break
s? 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.
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.
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.
Without a label, break will break out of the inner loop. With a label you can stop execution of nested loops. Save this answer.
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.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With