How can I break out of two nested loops in Objective-C?

I have two loops nested as follows:

for(...) { for(...) { } } 

I know there is a break statement. But am I embarrassed if he breaks both loops or just the one in which she was called? I need to break both as soon as I see that it makes no sense to repeat more than once.

+71
c loops objective-c
May 14 '09 at 12:55
source share
13 answers

break breaks out of one loop, but you can add a check for the outer loop, which breaks when the inner break.

 bool dobreak = false; for ( ..; !dobreak && ..; .. ) { for ( ... ) { if (...) { dobreak = true; break; } } } 
+89
May 14 '09 at 12:59
source share

If using goto simplifies the code, then that would be appropriate.

 for (;;) { for (;;) { break; /* breaks inner loop */ } for (;;) { goto outer; /* breaks outer loop */ } } outer:; 
+101
May 14, '09 at 13:40
source share

The break statement takes you out of the innermost loop. If you do not need additional overhead in the code, memory, and performance of the allocated state variable, I recommend reorganizing the code into a function or your own method and using return to exit all cycles:

 void do_lots_of_work(void) { int i, j; for(i=0; i<10 ; i++) { for(j=0;j< 10; j++) { .. .. if(disaster_struck()) return; /* Gets us out of the loops, and the function too. */ } } } 
+12
May 14 '09 at 13:13
source share

Besides the already mentioned flag variable or goto, you can throw an Objective-C exception:

 @try { for() { for() { @throw ... } } } @catch{ ... } 
+9
May 15 '09 at 1:16
source share

Others mentioned how you can set a flag or use goto , but I would recommend refactoring your code so that the inner loop is turned into a separate method. This method can then return some flag to indicate that the outer loop should break . If you name your methods accordingly, it will be more readable.

 for (int i = 0; i < 10; i++) { if (timeToStop(i)) break; } -(bool) timeToStop: (int) i { for (int j = 0; j < 10; j++) { if (somethingBadHappens) return true; } return false; } 

Pseudocode, not tested, but you understand the idea.

+6
May 14, '09 at 1:59 pm
source share

The break statement will exit the loop in the region that is the parent loop. If you want to exit the second loop, you can also use a boolean variable that is in the scope of both loops

 bool isTerminated = false; for (...) { if (!isTerminated) { for(...) { ... isTerminated = true; break; } } else { break; } } 
+2
May 14 '09 at 13:00
source share

Probably the easiest way is to use the flag variable

 for(i=0; i<10 && (done==false); i++) for(j=0;j< 10; j++){ .. .. if(...){done=true; break;} } 
+2
May 14, '09 at 13:03
source share

Change the counter of the upper contour before breaking

 for(i=0; i<10 ; i++) for(j=0;j< 10; j++){ .. .. i = 10; break; } 
+2
May 14 '09 at 1:10 p.m.
source share

Another solution is the factor of the second cycle in the function:

 int i; for(i=0; i<10 ; i++){ if !innerLoop(i) { break; } } bool innerLoop(int i) int j; for(j=0;j< 10; j++){ doSomthing(i,j); if(endcondtion){ return false; } } } 
+2
Sep 01 2018-10-10 at
source share

If a break is performed from a set of nested loops, only the innermost loop in which the break is executed ends. (Same as standard C)

+1
May 14 '09 at 12:59 a.m.
source share

The break statement breaks out of the innermost loop. An additional test and a break statement are required to exit the outer loop.

+1
May 14, '09 at 13:00
source share

In the same way as the latter, usually these are:

 for(i=0;i<a;i++){ for(j=0;j<a;j++){ if(Something_goes_wrong){ i=a; break; } } } 
0
Nov 14 '13 at 13:43
source share

Just for a laugh, how about changing this true / false check in the method and using the return :

 - (bool) checkTrueFalse: parameters{ for ( ...) { for ( ... ) { if (...) { return true; } } } return false; } 
0
Oct 07 '14 at 15:56
source share



All Articles