Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have two for loops nested like this:

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

I know that there is a break statement. But I am confused about if it breaks both loops or just the one in which it was called? I need to break both ones as soon as I see that it doesn't make sense to iterate more times over.

like image 289
Thanks Avatar asked May 14 '09 12:05

Thanks


People also ask

How do you break out of one nested loop?

Using break in a nested loop 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.

How break works in nested for loop in C?

The break is a keyword in C which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops.


1 Answers

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

for (;;)  {     for (;;)      {         break; /* breaks inner loop */     }      for (;;)      {         goto outer; /* breaks outer loop */     } }  outer:; 
like image 102
Ori Pessach Avatar answered Sep 30 '22 11:09

Ori Pessach