41

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?

Sukitha Udugamasooriya
  • 2,268
  • 1
  • 35
  • 56

5 Answers5

110

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;
}
Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
bbum
  • 162,346
  • 23
  • 271
  • 359
  • 10
    And please, for the sake of readability, let us not forget to put spaces around our operators. Not "for(int i = 0;i<[arABFBmatches count];i++)". Instead: "for (int i = 0; i < [arABFBmatches count]; i++)" – Erik van der Neut Jun 20 '14 at 12:19
12

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.

Jeffrey Berthiaume
  • 4,054
  • 3
  • 35
  • 45
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • 1
    If you're going to use `goto`, be very very very careful that you handle memory management properly. – Dave DeLong Jul 14 '10 at 06:25
  • `goto` is better than setting a flag and testing the flag to tell whether to break out of the outer loop. sure we must be careful. – neevek Apr 09 '12 at 15:11
  • 4
    Not necessarily better at all; if the inner loop is surrounded by some setup/teardown logic, the `goto` may happily jump right over the teardown. If you have to add this kind of logic later, it is easy to forget the short circuit case when refactoring. There is a strong argument to be made for control-flow that is jumpless. – bbum Jan 14 '13 at 03:26
  • I think that the use of "goto" should be avoided. Apart from bbum's excellent reason, it also hurts readability of the code. – Erik van der Neut Jun 20 '14 at 12:23
3
BOOL done = NO;
for(int i = 0;i<[arABFBmatches count] && !done; i++)
{
    for(int i = 0;i<[arABFBmatches count] && !done;i++)
    {
        if (termination condition) 
        {
             // cleanup
             done = YES;
        }
    }
}
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
drphill
  • 31
  • 1
3

From Apple's Objective-C docs:

Objective-C is defined as a small but powerful set of extensions to the standard ANSI C language.

So break and continue can be used wherever they are permitted in C.

continue can be used in looping constructs (for, while and do/while loops).

break can be used in those same looping constructs as well as in switch statements.

1

'break' will only get you out of the innermost loop or switch. You can use 'return' to exit out of a function at any time.Please have a look at the this link.

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Suraj K Thomas
  • 5,773
  • 4
  • 52
  • 64