views:

151

answers:

3

I was recently coding a small java program (as design for an 8086 Assembler program) and I wound up in an interesting position -- I needed to exit out of a while loop from an inner switch-statement, something like this (pseudocode, obviously):

:MyLoop
While(foo)
    switch (bar)
       case '1': print '1'; break
       case '0': print '0'; break
       case ';': end while loop;

It seemed like the perfect place for a goto statement because a single "break" would only exit the switch statement,(especially considering that I was designing for assembly) but Java has no gotos!

I found instead that Java has something called a multi-level break, so by using "break MyLoop", the program would break out of both the switch case and the while loop. '

My question then -- is this an appropriate use of a multi-level break? If, for some reason, I wanted to keep the switch (instead of, say, nested else ifs) statement, is there an alternative way to mimic a multi-level break via "break" or "continue" alone?

+11  A: 

Personally I would usually regard this as a hint to refactor the loop into its own method - then you can return from the whole method instead of just breaking out of the loop.

I personally find that clearer in most cases - but otherwise, this is exactly the kind of thing that labeled breaks are there for. It's generally cleaner (IMO) than having a separate boolean variable indicating whether or not to continue the loop.

Jon Skeet
+1 for `hint to refactor the loop into its own method`
seanizer
I had that originally, but one of the parameters (this was an assignment for part of an 8086 Assembly course) was no subroutines. Thanks for giving me piece of mind though.
Raven Dreamer
I do this too, when I'm not working for a company that has a "one return statement per method" policy
finnw
@finnw: Ick. Time to explain to someone that languages have moved on, and most of the reasons for the single-return-statement idiom have gone.
Jon Skeet
+2  A: 

is this an appropriate use of a multi-level break?

Yes.

is there an alternative way to mimic a multi-level break via "break" or "continue" alone?

You could set a boolean flag and then break at each level.

Aaron Digulla
It's not necessary to use boolean flags for multilevel breaks. Both break and continue statements can be labeled to relate to outer constructs instead of the innermost 'breakable' or 'continuable' constructs.
jarnbjo
@jarnbjo: In what way is that then an *alternative*? You've just described the multi-level break again.
Jon Skeet
@jarnbjo -- Aaron's second statement is just an answer to the hypothetical question posed by the OP; it is obviously not given as a recommendation. (Aaron +1)
dsmith
A: 

Jon Skeet's answer in code:

public void doSomething() {
    while(foo) {
        switch (bar) {
            case '1': print '1'; break;
            case '0': print '0'; break;
            case ';': return; //Effectively exits the loop
        }
    }
}
Dolph
What is this supposed to change? He meant refactoring the _loop_, not the the print command.This does not do anything about the problem with multi-level-break.
f1sh
@f1sh: No, this *is* what I meant. The loop is now in its own method, so to break out of the loop you only need to return from the method, instead of using multi-level break.
Jon Skeet
@f1sh - the relevant bits are the doSomthing() function and the return statement. This replaces the labelled break.
dsmith