Sukitha Udugamasooriya Sukitha Udugamasooriya - 2 months ago 8
Objective-C Question

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?

Answer

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;
}