views:

89

answers:

2

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?

+2  A: 

Roughly:

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

Objective-C does not have labelled break.

Matthew Flaschen
If you're going to use `goto`, be very very very careful that you handle memory management properly.
Dave DeLong
+3  A: 

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