[pastacode lang=”java” manual=”A%20nested%20loop%2C%0Afor%20(Type%20type%20%3A%20types)%20%7B%0A%20%20%20%20for%20(Type%20t%20%3A%20types2)%20%7B%0A%20%20%20%20%20%20%20%20%20if%20(some%20condition)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Do%20something%20and%20break…%0A%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%20%2F%2F%20Breaks%20out%20of%20the%20inner%20loop%0A%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D” message=”Java Code” highlight=”” provider=”manual”/]

How break out of both loops occurs

  • There are situations we need to be nested loops in Java, one loop containing another loop e.g. to implement many O(n^2) or quadratic algorithms e.g. bubble sort, insertion sort, selection sort, and searching in a two-dimensional array.
  • Nesting looping e.g. printing pascal triangle and printing those star structures exercises from school days.
    Depending upon some condition we also like to come out of both inner and outer loop.

For example, while searching a number in a two-dimensional array, once you find the number, you want to come out of both loops. There is a feature called labeled break, which you can use to break from nested loop. Label with break you can terminate a particular loop in nested loop structure.

How to use Label in Java

  • There are two steps to break from nested loop, first part is labeling loop and second part is using labeled break.
  • We must put our label before loop and need a colon after the label.
  • When using that label after break, control will jump outside of the labeled loop. This means if there are 10 level of nested loop, it can break from all of them by just calling break and label of first loop. Similarly using labeled continue, it starts continuing from the labeled loop
  • Label in Java is little different then goto. labeled break has no similarity with goto because it don’t allow you to go on a particular line, all you go is outside the loop.
  • Labeled continue is little bit similar to goto because it goes to the loop again but not at any arbitrary point, since continue can only be used with loop, effect is limited to the loops only.
  • To exit at any point inside an inner loop better use return statement. For this we need to externalize the code into a method and then call it, now at any point if want to go out of the loop, just call return without any value. This will improve readability.
[ad type=”banner”]

How to break from nested Loop in Java

  • The sample code of breaking nested loop in Java, we just have two loops, OUTER and INNER.
  • We are printing number in both the loop but once the product of two counters exceed 5, we break out from the outer loop.
  • This makes program complete because we also come out of main method.
  • In the next example, the same logic has been developed using a method and return statement called breakFromNestedLoop()
  • To break out from nested loop consider using a method and return statement over labeled break statement.
[pastacode lang=”java” manual=”import%20java.io.IOException%3B%0A%20%2F**%20*%20How%20to%20break%20from%20nested%20loop%20in%20Java.%20You%20can%20use%20labeled%20*%20statement%20with%20break%20statement%20to%20break%20from%20nested%20loop.%20%20*%2F%20%0Apublic%20class%20BreakingFromNestedLoop%7B%20public%20static%20void%20main(String%20args%5B%5D)%20throws%20IOException%0A%20%7B%20%0A%2F%2F%20this%20is%20our%20outer%20loop%20%0Aouter%3A%20for%20(int%20i%20%3D%200%3B%20i%20%3C%204%3B%20i%2B%2B)%20%0A%7B%20%0A%2F%2F%20this%20is%20the%20inner%20loop%20%0Afor%20(int%20j%20%3D%200%3B%20j%20%3C%204%3B%20j%2B%2B)%20%0A%7B%20%0A%2F%2F%20condition%20to%20break%20from%20nested%20loop%0A%20if%20(i%20*%20j%20%3E%205)%0A%20%7B%20%0ASystem.out.println(%22Breaking%20from%20nested%20loop%22)%3B%20%0Abreak%20outer%3B%0A%20%7D%20%0ASystem.out.println(i%20%2B%20%22%20%22%20%2B%20j)%3B%20%0A%7D%20%7D%20%0ASystem.out.println(%22exited%22)%3B%0A” message=”Java Code” highlight=”” provider=”manual”/] [ad type=”banner”] [pastacode lang=”java” manual=”%2F%2F%20better%20way%20is%20to%20encapsulate%20nested%20loop%20in%20a%20method%20%0A%2F%2F%20and%20use%20return%20to%20break%20from%20outer%20loop%20%0AbreakFromNestedLoop()%3B%20%0A%7D%20%0A%2F**%20*%20You%20can%20use%20return%20statement%20to%20return%20at%20any%20point%20from%20a%20method.%20%0A*%20This%20will%20help%20you%20to%20break%20from%20nested%20loop%20as%20well%20*%2F%20%0Apublic%20static%20void%20breakFromNestedLoop()%0A%7B%20%0Afor(int%20i%3D0%3B%20i%3C6%3B%20i%2B%2B)%0A%7B%20%0Afor(int%20j%3D0%3B%20j%3C3%3B%20j%2B%2B)%0A%7B%20%0Aint%20product%20%3D%20i*j%3B%20%0Aif(product%20%3E%204)%0A%7B%20%0ASystem.out.println(%22breaking%20from%20nested%20loop%20using%20return%22)%3B%20return%3B%20%0A%7D%20%7D%20%7D%0ASystem.out.println(%22Done%22)%3B%20%7D%20%7D” message=”Java Code” highlight=”” provider=”manual”/]

Output

0 0
0 1
0 2
0 3
1 0
1 1
1 2
1 3
2 0
2 1
2 2
Breaking from nested loop
exited
breaking from nested loop using return

Categorized in: