How can we get out of 4 inner loops?

Hi I am starting in java and my program has 4 for loops: my program works so that if b is true , the element will be deleted from pointList and n will be n-- and I want to exit all for loops and return from the first for loop so l will be l++ , how can I do this? with an expression of break?

 for (int l = 0; l < n; l++) { for (int i = 1; i < (n - 2); i++) { for (int j = i + 1; j < (n - 1); j++) { for (int k = j + 1; k < n; k++) { if (l != i && l != j && l != k) { boolean b = isOK(pointList.get(l), pointList.get(i), pointList.get(j), pointList.get(k)); System.out.println(b); if (b == true) { pointList.remove(pointList.get(l); n--; break; } else System.out.println(b); } } } } } 
+7
java for-loop break label
source share
9 answers

You can use jammed as:

  for (int l = 0; l < n; l++) { foo: for (int i = 1; i < (n - 2); i++) { for (int j = i + 1; j < (n - 1); j++) { for (int k = j + 1; k < n; k++) { if (l != i && l != j && l != k) { boolean b = isOK(pointList.get(l), pointList.get(i), pointList.get(j), pointList.get(k)); System.out.println(b); if (b == true) { pointList.remove(pointList.get(l); n--; break foo; } else System.out.println(b); } } } } } 
+17
source share

In a loop, the break statement completes the inner loop, and continue to the next iteration. For these two statements to work in a different loop in the inner, you need to use labels . Something like this should work:

 outerloop: for (int l = 0; l < n; l++) { for (int i = 1; i < (n - 2); i++) { for (int j = i + 1; j < (n - 1); j++) { for (int k = j + 1; k < n; k++) { if (l != i && l != j && l != k) { boolean b = isOK(pointList.get(l), pointList.get(i), pointList.get(j), pointList.get(k)); System.out.println(b); if (b == true) { pointList.remove(pointList.get(l); n--; continue outerloop; } else System.out.println(b); } } } } } 
+5
source share

Look at the instruction with the inscription break

for example here: Branching expressions

+3
source share
 String valueFromObj2 = null; String valueFromObj4 = null; OUTERMOST: for(Object1 object1: objects){ for(Object2 object2: object1){ //I get some value from object2 valueFromObj2 = object2.getSomeValue(); for(Object3 object3 : object2){ for(Object4 object4: object3){ //Finally I get some value from Object4. valueFromObj4 = object4.getSomeValue(); //Compare with valueFromObj2 to decide either to break all the foreach loop if( compareTwoVariable(valueFromObj2, valueFromObj4 )) { break OUTERMOST; } }//fourth loop ends here }//third loop ends here }//second loop ends here }//first loop ends here 
+3
source share

Use tagged loop

 for (int l = 0; l < n; l++) { loopa: for (int i = 1; i < (n - 2); i++) { for (int j = i + 1; j < (n - 1); j++) { for (int k = j + 1; k < n; k++) { if (l != i && l != j && l != k) { boolean b = isOK(pointList.get(l), pointList.get(i), pointList.get(j), pointList.get(k)); System.out.println(b); if (b == true) { pointList.remove(pointList.get(l); n--; break loopa; } else System.out.println(b); } } } } } 

and then break from the marked cycle

+2
source share
 again: for (int l = 0; l < n; l++) { for (int i = 1; i < (n - 2); i++) { for (int j = i + 1; j < (n - 1); j++) { for (int k = j + 1; k < n; k++) { if (l != i && l != j && l != k) { boolean b = isOK(pointList.get(l), pointList.get(i), pointList.get(j), pointList.get(k)); System.out.println(b); if (b == true) { pointList.remove(pointList.get(l); n--; break again; } else System.out.println(b); } } } } } 
+1
source share

I agree with all the other answers. However, I would like to point out that an alternative to exit would be to simply put this code in its own procedure and use the return to break out of it all. Your quad-core cycle is so complex in itself that it probably should still be in its own program.

I worked on DoD jobs that required a cyclomatic complexity of at most 6 for any one procedure (with some exceptions). This series of loops alone is 4. If you cannot find an easier way to do this, you really need to pop them in your routine in order to maintain the sanity of the poor shmaks who should support this code.

+1
source share

The first โ€œquick and dirtyโ€ solution would be to use the stay_into_loops variable and change the for loops, such as:

  boolean stay_into_loops = true // here goes the first for loop for (int i = 1; i < (n - 2) && stay_into_loops ; i++) { for (int j = i + 1; j < (n - 1) && stay_into_loops ; j++) { for (int k = j + 1; k < n && stay_into_loops ; k++) { if (l != i && l != j && l != k) { boolean b = isOK(pointList.get(l), `pointList.get(i), pointList.get(j), pointList.get(k));` System.out.println(b); if (b == true) { pointList.remove(pointList.get(l); n--; stay_into_loops = false; break; 

However, this is usually the smell of code when you come across such things. Consider code refactoring, because at some point it will turn into a mess.

0
source share

Create an outlet for yourself in each inner cycle.
Here's a quick and painless solution.

  bool breakout; for (int l = 0; l < n; l++) { breakout = false; for (int i = 1; i < (n - 2) && !breakout; i++) for (int j = i + 1; j < (n - 1) && !breakout; j++) for (int k = j + 1; k < n && !breakout; k++) { if(b == true) breakout = true; } } 

So you see that breakout boolean is your ticket from each inner loop, because it is checked in every for declaration. And it gets reset every time the iteration of the first for .

0
source share

All Articles