Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a neat way of breaking out of many for loops at once?

Tags:

c

for-loop

break

Suppose I need to break out of three or four nested for loops at once at the occurence of some event inside the innermost loop. What is a neat way of doing that?

what I do is use flags like this:

int i, j, k;
int flag1 = 0;
int flag2 = 0;

for (i = 0; i < 100; i++) {
    for (j = 0; j < 100; j++) {
        for (k = 0; k < 100; k++) {
            if (k == 50) {
                flag1 = 1;
                flag2 = 1;
                break;
            }
        }
        if (flag1 == 1)break;
    }
    if (flag2 == 1)break;
}

I don't think this is particularly neat.

How would you accomplish the same thing? (w/o using jumps)

like image 744
Moeb Avatar asked Oct 19 '09 04:10

Moeb


People also ask

How do you break out of a loop in a loop?

Breaking Out of For Loops. To break out of a for loop, you can use the endloop, continue, resume, or return statement.

Does Break break out of multiple loops?

In a nested loop, a break statement only stops the loop it is placed in. Therefore, if a break is placed in the inner loop, the outer loop still continues. However, if the break is placed in the outer loop, all of the looping stops.

Can you break out of a for loop Python?

The break statement can be used in both while and for loops. If you are using nested loops, the break statement stops the execution of the innermost loop and start executing the next line of code after the block.


5 Answers

use goto. it's clean and simple.

like image 147
Test Avatar answered Sep 22 '22 12:09

Test


Put all the loops in a function and just return instead of break.

like image 38
Foole Avatar answered Sep 22 '22 12:09

Foole


If you're using Java, you can associate labels with each for block and then reference the label after a continue statement. For example:

outerfor:
for (int i=0; i<5; i++) {
    innerfor:
    for (int j=0; j<5; j++) {
        if (i == 1 && j == 2) {
             continue outerfor;
        }
    }
}
like image 43
Asaph Avatar answered Sep 22 '22 12:09

Asaph


How would you accomplish the same thing? (w/o using jumps)

Why? Nothing is universally evil, and every put-upon tool has its uses (except gets()). Using goto here makes your code look cleaner, and is one of the only choices we have (assuming C). Look:

int i, j, k;

for (i = 0; i < 100; i++) {
    for (j = 0; j < 100; j++) {
        for (k = 0; k < 100; k++) {
            if (k == 50) {
                goto END;
            }
        }
    }
}
END:

Much cleaner than all those flag variables, and it even shows more clearly what your code is doing.

like image 45
Chris Lutz Avatar answered Sep 19 '22 12:09

Chris Lutz


Just a wee bit better.

int i, j, k;
int flag1 = 0;
int flag2 = 0;

for (i = 0; i < 100 && !flag2; i++) {
    for (j = 0; j < 100 && !flag1; j++) {
        for (k = 0; k < 100; k++) {
            if (k == 50) {
                flag1 = 1;
                flag2 = 1;
                break;
            }
        }
    }
}

But if you really need to have those loops, then it makes sense explicitly declaring in each loop what conditions must hold for it to continue, for readability.

like image 36
moogs Avatar answered Sep 22 '22 12:09

moogs