Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective C for-loop break and continue

How can i use a 'break' statement within a for-loop which continues form a specified label?

ex;

outer: for(int i = 0;i<[arABFBmatches count];i++){     for(int i = 0;i<[arABFBmatches count];i++){         //         break _____;     } } 

How to break to outer?

like image 583
Sukitha Udugamasooriya Avatar asked Jul 14 '10 05:07

Sukitha Udugamasooriya


People also ask

Which of the following is the objective of continue statement in C programming language?

The continue statement in Objective-C programming language works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between.


2 Answers

Hard to say from your question. I'd interpret it that you want to skip the rest of the iterations of the inner loop and continue the outer loop?

for (int i = 0; i < [arABFBmatches count]; i++) {     for (int j = 0; j < [arABFBmatches count]; j++) {         if (should_skip_rest)             break; // let outer loop continue iterating     } } 

Note that I changed the name of your inner loop invariant; using i in both is inviting insanity.

If you want to break from both loops, I wouldn't use a goto. I'd do:

BOOL allDoneNow = NO; for (int i = 0; i < [arABFBmatches count]; i++) {     for (int j = 0; j < [arABFBmatches count]; j++) {         if (should_skip_rest) {             allDoneNow = YES;             break;         }     }     if (allDoneNow) break; } 
like image 59
bbum Avatar answered Sep 28 '22 00:09

bbum


Roughly:

for(int i = 0;i<[arABFBmatches count];i++){     for(int j = 0;j<[arABFBmatches count];j++){         //         goto outer_done;     } } outer_done: 

Objective-C does not have labelled break.

like image 21
Matthew Flaschen Avatar answered Sep 27 '22 23:09

Matthew Flaschen