tags:

views:

2854

answers:

6

I've got a nested loop construct like this:

for (Type type : types) {
    for (Type t : types2) {
         if (some condition) {
             // Do something and break...
             break; // Breaks out of the inner loop
         }
    }
}

Now how can I break out of booth loops. I've looked at similar questions, but none concerns Java specifically. I couldn't apply these solutions because most used gotos.

I don't want to put the inner loop in a different method.

Update: I don't want to rerun the loops, when breaking I'm finished with the execution of the loop block.

+27  A: 

You can use break with a label for the outer loop. For example:

public class Test {
  public static void main(String[] args) {
    outerloop:
    for (int i=0; i < 5; i++) {
      for (int j=0; j < 5; j++) {
        if (i * j > 6) {
          System.out.println("Breaking");
          break outerloop;
        }
        System.out.println(i + " " + j);
      }
    }
    System.out.println("Done");
  }
}

This prints:

0 0
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
1 4
2 0
2 1
2 2
2 3
Breaking
Done
Jon Skeet
What I dislike with this solution is that if don't want to rerun the loops I have to introduce a condition. And the label has to be defined in front of the loop. Basically I would prefer to jump right after the loops.
boutta
This *does* jump to directly after the loop. Try it! Yes, the label comes before the loop, but that's because it's labeling the loop, rather than the place you want to exit to. (You can continue with a label too.)
Jon Skeet
After commenting I tried it. It works, thx for the great explanation.
boutta
Don't question the Skeets infinite wisdom!!! :-)
Kamikaze Mercenary
I'll never do that again, I promise ;)
boutta
A: 

You can throw an exception, or repeat the inner loop condition in the outer loop.

1800 INFORMATION
"throwing an Exception" would work, but mis-using exceptions for flow control is A Bad Thing (tm).
Joachim Sauer
You too can make a suggestion without prejudice as to the worth of the idea
1800 INFORMATION
Throwing an exception will only get you on thedailywtf.com. :)
Bombe
+2  A: 

you can use labels;



label1 : for(int i =0;;)
{
     for(int g =0;;)
     {
         break lable1;
     }
}

simon622
A: 

You can use a temporary variable:

boolean outerBreak = false;
for (Type type : types) {
   if(outerBreak) break;
    for (Type t : types2) {
         if (some condition) {
             // Do something and break...
             outerBreak = true;
             break; // Breaks out of the inner loop
         }
    }
}

Depending on your function, you can also exit/return from the inner loop:

for (Type type : types) {
    for (Type t : types2) {
         if (some condition) {
             // Do something and break...
             return;
         }
    }
}
Miguel Ping
I find this way kind of cluttered.
boutta
+7  A: 

You can use a named block around the loops:

search: {
    for (Type type : types) {
        for (Type t : types2) {
            if (some condition) {
                // Do something and break...
                break search;
            }
        }
    }
}
Joey
You don't need to create a new block to use a label.
Jon Skeet
No, but it makes the intent a lot clearer. See the first comment on the accepted answer.
Bombe
Jon: Ok, didn't know that as I didn't ever use them :) I just knew they existed and looked up in the spec.
Joey
+1 because named blocks is new to me.
Johannes Schaub - litb
A: 

maybe with a function?

public void doSomething(List<Type> types, List<Type> types2){
  for(Type t1 : types){
    for (Type t : types2) {
      if (some condition) {
         //do something and return...
         return;
      }
    }
  }
}
Fortega
That only breaks the first loop...
boutta
Ah yes, the question was to break out of both loops... I adjusted my answer...
Fortega