---
title: "Breaking out of nested loops in Java"  
description: "Breaking out of nested loops in Java"  
author: "Royce Roy"  
published: 2015-05-08  
updated: 2015-05-08  
canonical: https://www.mindstick.com/forum/23205/breaking-out-of-nested-loops-in-java  
category: "java"  
tags: ["java", "loop"]  
reading_time: 2 minutes  

---

# Breaking out of nested loops in Java

I've got a [nested loop](https://www.mindstick.com/blog/390/using-javascript-nested-loops) 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](https://yourviews.mindstick.com/view/81402/it-s-liberals-vs-progressives-in-us-politics-now) how can I break out of both [loops](https://www.mindstick.com/forum/161927/explain-the-python-while-loops-with-example). I've looked at similar [questions](https://www.mindstick.com/blog/124895/hp-hpe0-s54-cheat-sheet-exam-questions-bank-for-guaranteed-success), but none concerns [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) specifically. I couldn't [apply](https://www.mindstick.com/articles/13148/discover-to-apply-make-up-like-a-professional-supermodel) these [solutions](https://www.mindstick.com/articles/12807/product-searching-issues-and-solutions-for-ecommerce-store-advanced-search-autocomplete-suggest) because most used gotos.\
I don't want to put the inner loop in a different method.\
[Update](https://www.mindstick.com/forum/156353/what-is-hummingbird-update): I don't want to rerun the loops, when breaking I'm finished with the [execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) of the loop block.

## Replies

### Reply by Anonymous User

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 00 10 20 30 41 01 11 21 31 42 02 12 22 3BreakingDone \
2)you can use labels;

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

\
3)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;            }        }    }}                                                            
```


---

Original Source: https://www.mindstick.com/forum/23205/breaking-out-of-nested-loops-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
