Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop inner and outer loop using break statement [duplicate]

I have really simple code where I use break inside inner loop:

for (int it=0; it<5; it++) {
    for (int it2=0; it2<5; it2++) {
        if (it2==2) 
            break; //break here not it2, but it loop
        NSLog(@"it=%d it2=%d", it, it2);
    }
}

I receive an output:

it=0 it2=0, it=0 it2=1, 
it=1 it2=0, it=1 it2=1, 
it=2 it2=0, it=2 it2=1,
it=3 it2=0, it=3 it2=1,
it=4 it2=0, it=4 it2=1

I know in some programic languages there is possibility to determine on which loop break statement should affect. Is it possible to call break to stop outer loop (for with it variable)?

like image 782
Szu Avatar asked Apr 09 '14 12:04

Szu


3 Answers

If you really want to do this, then bite the bullet and use goto.

for (int it=0; it<5; it++) {
    for (int it2=0; it2<5; it2++) {
        if (it2==2) 
            goto end_outer_loop;
        NSLog(@"it=%d it2=%d", it, it2);
    }
}

end_outer_loop:

// more stuff here.

This is a legitimate goto (most gotos in the downward direction are). In fact break is a special kind of "approved" goto, and because C is a minimal language you have to use an explicit goto in complex cases.

However as many people here have pointed out, it's better if you can use return. You should not contort your program just to avoid goto, but in most cases it is a clue that your function is become too complex and should therefore be broken up.

like image 63
Adrian Ratnapala Avatar answered Nov 15 '22 06:11

Adrian Ratnapala


You can use a bool flag to serve the purpose, something like this:

bool flag = false;
for (int it=0; it<5; it++) {
    for (int it2=0; it2<5; it2++) {
        if (it2==2) { flag=true; break; }
        NSLog(@"it=%d it2=%d", it, it2);
    }
    if(flag) {break;}
}

This method shall be used only if you cannot use the return method as specified by others. return improves code-readability. This is just a workaround.

The goto method is also a valid one, but it is always advised not to use goto, just because it breaks the control flow and can cause hours of debugging if the code is large enough!

like image 21
CinCout Avatar answered Nov 15 '22 04:11

CinCout


In C/C++, you really don't have something like that. There are clearly some answers already, such as use goto or something. Personally, I don't like gotos and thing they kinda lead to some sloppy code (IMO).

To me you have two good options here.

  1. Write the loop conditions so that they include the exit condition in both. This is the way I would personally go since I don't like the idea of using break or something in a loop, unless I absolutely have to. I just think it makes for a little cleaner product is all, and it's what I was taught.

  2. If you do decide to keep the break, then instead of doing a single-line if() to control it, break it into two parts and create a "break flag" that will be set if the break condition is met. From there, either place an additional condition in your outer loop to exit if that flag is set, or have another if() inside the outer loop (but not inside the inner loop) that will break if that flag is set.

Hope this helps.

like image 20
Josh Braun Avatar answered Nov 15 '22 06:11

Josh Braun