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

Thanks picture Thanks · May 14, 2009 · Viewed 62.3k times · Source

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.

Answer

Ori Pessach picture Ori Pessach · May 14, 2009

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

for (;;) 
{
    for (;;) 
    {
        break; /* breaks inner loop */
    } 
    for (;;) 
    {
        goto outer; /* breaks outer loop */
    }
} 
outer:;