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 ... } ... } } 
3
  • "Is there some kind of break statement" i believe, there is only one kind of break exist Commented Dec 12, 2012 at 16:42
  • @coders: thanks - have edited for clarity. Commented Dec 12, 2012 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. Commented Dec 12, 2012 at 16:48

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I wasn't aware you could use labels with different types of loops. That's really helpful.
+1 labels are usually in UPPER_CASE. I assume you want continue outer; Break will exit that loop as well.
@Froskoy You can break out of any block of code, it doesn't have to be a loop.
@PeterLawrey makes a good point, follow the kind of naming convention used for CONSTANTS
2

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.

Comments

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: ... } ... } 

}

2 Comments

What's the difference between using break and continue in this situation?
Continue returns to beginig of loop, break exits loop
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 } } } 

Comments

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.

Comments

0

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

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.