13

What happens when breaking in nested loops?

suppose the following code:

for(int x = 0; x < 10; x++)
{
    do {
        if(x == 4)
            break;
        x++;
    } while(x != 1);
}

Which loop will exit on encountering the break statement, the for loop or the do while loop ?

Community
  • 1
  • 1
sgupta
  • 1,214
  • 2
  • 15
  • 29
  • 8
    `break` will only exit from the inner loop – Paul R Aug 31 '12 at 14:06
  • it will exit the do/while, if you want to exit out of the OUTER loop you'll have to roll with a goto statement... – Shark Aug 31 '12 at 14:07
  • 3
    You can break from the outer loop several ways. You can `goto`, you can `return`, you can set `x = 10;`, and so onl. – David Schwartz Aug 31 '12 at 14:09
  • David, thanks for the tip on setting condition for outerloop as false, but goto was exactly what i was trying to avoid. – sgupta Aug 31 '12 at 14:12
  • 2
    This question would have been answered, by yourself, by actually testing this code. Did you try that before asking? – HonkyTonk Aug 31 '12 at 14:35
  • @user1075375: It's a judgment call which method you like best, and in each particular case your list of options can be a bit different. The danger with setting the outerloop condition is that it makes the logic of the inner loop dependent on the logic of the outer loop. If that's fundamental to the particular section of code, then that's no problem. But if they're otherwise semi-independent, that dependency can make the code fragile and harder to maintain and understand. Consider all options and use good judgment. – David Schwartz Aug 31 '12 at 22:16
  • Honky Tonk, i would if my company wouldn't be using some obscure remote compiling box, so i cannot make assumptions whether their compiler behaves the same way as gcc on my machine. Hence i stick to c standard implementation. – sgupta Sep 01 '12 at 06:06

4 Answers4

12

The break always breaks the innermost loop.

6.8.6.3

A break statement terminates execution of the smallest enclosing switch or iteration statement.


If you want to break out of both loops, use a label after the for and jump with goto.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • Thanks, but goto was exactly the thing i was trying to avoid, it's a bit ugly but that's my opinion. though it closely resembles conditional execution in assembly with branches. – sgupta Aug 31 '12 at 14:10
  • @user1075375 I like goto. I understand it's bad form in C++ and I won't argue. But in C it's quite nice :-) – cnicutar Aug 31 '12 at 14:11
  • Put an extra condition in the for loop which won't be true when you want to break out of it? – teppic Aug 31 '12 at 14:12
  • 1
    @teppic That may not be a choice in a heavy loop. I once programmed on hardware that performed absolutely horribly with branches. – cnicutar Aug 31 '12 at 14:13
  • considering the actual loop will (most of the times) execute a lot of times (otherwise why not just unroll the loop and save branches), that would hurt performance. – sgupta Aug 31 '12 at 14:14
  • @cnicuta yeah, agreed -it'd be bad to have an extra check in those circumstances, but on something this small that kind of thing will avoid a goto (though I do think goto is fine when deeply nested inside loops). – teppic Aug 31 '12 at 14:15
  • 1
    Personally, I prefer `if(x == 4) { x = 10; break; }` – Jack Aug 31 '12 at 15:11
4

Alternatively, you can use a flag if you don't want to use goto:

int flag = 0;

for(int x = 0; x < 10; x++)
{
    do {
        if(x == 4) {
            flag = 1;
            break;
        }
        x++;
    } while(x != 1);

if(flag) 
   break; // To break the for loop
}
P.P
  • 117,907
  • 20
  • 175
  • 238
  • considering the waste of 4 bytes in cache and extra conditional check in loop, goto seems better interms of performance. More so since i expect the loop to execute a lot of times. – sgupta Aug 31 '12 at 14:23
  • 2
    If you were after performance, you wouldn't be writing this code in the first place. No need to have the outer for loop at all, if you are going to break when you get to a particular value. – Floris Jan 29 '13 at 14:14
  • This isn't the code I wrote, It was simply for illustration purpose to help people understand what I meant. – sgupta Dec 27 '13 at 20:00
  • But this is not an answer to the question. – Sany Jul 27 '20 at 05:33
  • @Sany OP asked how to break out of nested loops (as OP said in many of his comments and him stating "he wanted to avoid goto"). Besides, the answer starts with "alternatively" -- meaning it's intended as supplement to other answers. You seem to take just "What happens when breaking in nested loops?" and disregarding rest of the description in the question and various comments by OP. – P.P Jul 27 '20 at 07:39
3

the while will break and the for loop will keep running.

tehdoommarine
  • 1,868
  • 3
  • 18
  • 31
1

Break will kill the nearest/innermost loop that contains the break. In your example, the break will kill the do-while, and control jumps back up to the for() loop, and simply start up the next iteration of the for().

However, since you're modifying x both in the do() AND the for() loops, execution is going to be a bit wonky. You'll produce an infinite loop once the outer X reaches 5.

Marc B
  • 356,200
  • 43
  • 426
  • 500