2

Possible Duplicate:
Breaking out of nested loops in Java

How can I use break and/or continue statements to return to the first line of the while loop at points 1, 2 and 3, for example, as indicated in the pseudocode?

Suppose I have a scenario reminiscent of the following:

while(condition) {
    // want to return to this point
    for (Integer x : xs) {
        // point 1
        for (Integer y : ys) {
            // point 2
            ...
        }
        ...
    }
    for (Integer a : as) {
        for (Integer b : bs) {
            // point 3
            ...
        }
        ...
    }
}
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Froskoy
  • 2,967
  • 5
  • 20
  • 21
  • "Is there some kind of break statement" i believe, there is only one kind of break exist – Ravi Dec 12 '12 at 16:42
  • @coders: thanks - have edited for clarity. – Froskoy Dec 12 '12 at 16:47
  • @OldCurmudgeon: I very much disagree that this post is a duplicate. The fact that two different types of loops are involved and that there are multiple sequential inner loops makes this question *very* different from the others on the site. – Froskoy Dec 12 '12 at 16:48

6 Answers6

5

Use a label such as :

outer:
while(condition) {
// want to return to this point
for (Integer x : xs) {
    // point 1
    for (Integer y : ys) {
        // point 2
        ...
    }
    ...
}
for (Integer a : as) {
    for (Integer b : bs) {
        // point 3
        ...
    }
    ...
}

}

and you can then use break outer; to escape the while loop. This works with nested for loops as well, but I try not to overuse labels

As pointed out by @Peter, use continue outer; if you wish to finish the current outer iteration early and continue on to the next, as opposed to escaping the while loop.

Darius
  • 5,180
  • 5
  • 47
  • 62
2

label your Loops with syntax

LABEL: LoopType and later you can use Break Statement to get out of a certain loop with break LABEL;.

OUT: while(somecond){
IN:for(...) {
   INNER: for(...){
break IN;// will get you outta IN and INNER for loop
}
 }
}
PermGenError
  • 45,977
  • 8
  • 87
  • 106
2
loop:
while(condition) {
// want to return to this point
for (Integer x : xs) {
    continue loop:
    for (Integer y : ys) {
        continue loop:
        ...
    }
    ...
}
for (Integer a : as) {
    for (Integer b : bs) {
        continue loop:
        ...
    }
    ...
}

}

Nemanja
  • 1,161
  • 11
  • 13
1

You can do that :

here:
while(condition) {
   for (Integer x : xs) {
        // point 1
        for (Integer y : ys) {
            break here;
        }
    }

The break statement, with or without label, is described in the Java specification :

A break statement with no label attempts to transfer control to the innermost enclosing switch, while, do, or for statement of the immediately enclosing method or initializer; this statement, which is called the break target, then immediately completes normally.

A break statement with label Identifier attempts to transfer control to the enclosing labeled statement (§14.7) that has the same Identifier as its label; this statement, which is called the break target, then immediately completes normally. In this case, the break target need not be a switch, while, do, or for statement.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

yes, you can use the break statement with a label:

SOME_LABEL:
   // ... some code here
   break SOME_LABEL;

used sparingly, this is a clean way to break out of multiple nested loops.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
0

You could do like this posted here stackoverflow.com/questions/886955/breaking-out-of-nested-loops-in-java handles the same question.

8bitcat
  • 2,206
  • 5
  • 30
  • 59
  • That actually doesn't answer my question, or at least I thought it didn't when I first viewed it before reading the other answers here, because of the mixed for and while loops, and breaking out at the subtly different points. – Froskoy Dec 12 '12 at 16:43