问题
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)?
回答1:
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.
回答2:
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!
回答3:
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 goto
s and thing they kinda lead to some sloppy code (IMO).
To me you have two good options here.
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.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 anotherif()
inside the outer loop (but not inside the inner loop) that will break if that flag is set.
Hope this helps.
回答4:
You should wrap your loops in a function and then use return
to return from that function.
回答5:
In programming using goto statement makes the logic of the program complex and tangled. In modern programming, goto statement is considered a harmful construct and a bad programming practice.
You can use it as a function and when condition occurs function exits
int loop(int it,int it2)
{
for (int it=0; it<5; it++) {
for (int it2=0; it2<5; it2++) {
if (it2==2) return 0; //function returns here
NSLog(@"it=%d it2=%d", it, it2);
}
}
return 1;
}
回答6:
Unfortunately, c++ does not have that kind of break. A possibility considered abomination is using goto. There are two acceptable solution:
Put this code in a separate function, and use return
Use a boolean flag
bool stopFlag = false; for (int it=0; it<5 && !stopFlag; it++) { for (int it2=0; it2<5; it2++) { if (whatever) { stopFlag=true; break; } }
来源:https://stackoverflow.com/questions/22962811/how-to-stop-inner-and-outer-loop-using-break-statement