Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I break multiple foreach loops? [duplicate]

I have four foreach loops that iterate through the collections and based on a condition do something.

Here is the code that I am writing now:

boolean breakFlag = false; String valueFromObj2 = null; String valueFromObj4 = null; 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        breakFlag = compareTwoVariable(valueFromObj2, valueFromObj4 );        if(breakFlag){break;}       } // fourth loop ends here       if(breakFlag){break;}     } // third loop ends here     if(breakFlag){break;}   } // second loop ends here   if(breakFlag){break;} } // first loop ends here 

The main object (objects in the code) comes from a third-party provider SDK, so I cannot change anything on that portion. I want to ask the Stack Overflow community if there is a better approach to break all the four foreach loops. Or if there is any other way to refactor this code to make it more readable and maintainable.

like image 595
royalghost Avatar asked Feb 15 '09 21:02

royalghost


People also ask

How do I break out of nested foreach?

The only way to this directly is with a goto . Another (better) option is to restructure until the problem goes away. For instance by putting the inner code (while + foreach) in a method and use return to get back.

Does Break take you out of all loops?

In a nested loop, a break statement only stops the loop it is placed in. Therefore, if a break is placed in the inner loop, the outer loop still continues. However, if the break is placed in the outer loop, all of the looping stops.

Does foreach make a copy?

Summary. To summarize: foreach will copy the array structure if and only if the iterated array is not referenced and has a refcount > 1. foreach will additionally copy the array values if and only if the previous point applies and the iteration is done by reference.

What is break in foreach?

break crashes and return does nothing but continue iteration. It is worth noting that while return does indeed continue the iteration, it will skip any code that comes after it in the block. Take this code for instance: [1,2,3]. forEach(function(el) { if(el === 2) { console.


2 Answers

Use a label on the outermost loop, and include this label in the break statement when you want to jump out of all the loops. In the example below, I've modified your code to use the label OUTERMOST:

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 
like image 92
Luke Woodward Avatar answered Oct 09 '22 02:10

Luke Woodward


Extract all the loops into the function and use return.

like image 23
Alex Reitbort Avatar answered Oct 09 '22 02:10

Alex Reitbort