Why does Java allow for labeled breaks on arbitrary statements?

Why make it so that you can only break out of certain enclosing statements using labeled breaks but not regular breaks

Consider:

while (true) {
    if (condition) {
        break;
    }
}

If the break did as you suggest, this code would perform unexpectedly. Breaks would become a lot more difficult to use.

And why allow for this behavior at all?

I don't use it, but it is a feature and allows for certain unique control-flow constructs. I'd ask you, why not allow it?


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.


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?


The issue with goto is that it can jump forward, past code. A labeled break cannot do that (it can only go backwards). IIRC C++ has to deal with goto jumping past code (it is been over 17 years since I cared about that though so I am not sure I am remembering that right).

Java was designed to be used by C/C++ programmers, so many things were done to make it familiar to those developers. It is possible to do a reasonable translation from C/C++ to Java (though some things are not trivial).

It is reasonable to think that they put that into the language to give C/C++ developers a safe goto (where you can only go backwards in the code) to make it more comfortable to some programmers converting over.

I have never seen that in use, and I have rarely seen a labeled break at all in 16+ years of Java programming.

You cannot break forward:

public class Test 
{
    public static void main(final String[] argv) 
    {
        int val = 1;

        X:
        {
            if(argv.length == 0)
            {
                break X;
            }

            if(argv.length == 1)
            {
                break Y;   <--- forward break will not compile
            }
        }

        val = 0;

        Y:
        {
            Sysytem.out.println(val); <-- if forward breaks were allowed this would 
                                          print out 1 not 0.
        }
    }
}