If I use a break
statement, it will only break inner loop and I need to use some flag to break the outer loop. But if there are many nested loops, the code will not look good.
Is there any other way to break all of the loops? (Please don't use goto stmt
.)
for(int i = 0; i < 1000; i++) {
for(int j = 0; j < 1000; j++) {
if(condition) {
// both of the loops need to break and control will go to stmt2
}
}
}
stmt2
What about:
if(condition) {
i = j = 1000;break;
}
No, don't spoil the fun with a break
. This is the last remaining valid use of goto
;)
If not this then you could use flags to break out of deep nested loops.
Another approach to breaking out of a nested loop is to factor out both loops into a separate function, and return from that function when you want to exit.
Summarized - to break out of nested loops:
- use
goto
- use flags
- factor out loops into separate function calls
Couldn't resist including xkcd here :)
Goto's are considered harmful but as many people in the comments suggest it need not be. If used judiciously it can be a great tool. Anything used in moderation is fun.
bool stop = false;
for (int i = 0; (i < 1000) && !stop; i++)
{
for (int j = 0; (j < 1000) && !stop; j++)
{
if (condition)
stop = true;
}
}
One way is to put all the nested loops into a function and return from the inner most loop incase of a need to break out of all loops.
function()
{
for(int i=0; i<1000; i++)
{
for(int j=0; j<1000;j++)
{
if (condition)
return;
}
}
}
I think goto
will solve the problem
for(int i = 0; i < 1000; i++) {
for(int j = 0; j < 1000; i++) {
if (condition) {
goto end;
}
}
}
end:
stmt2
You'll need a boolean variable, if you want it readable:
bool broke = false;
for(int i = 0; i < 1000; i++) {
for(int j = 0; j < 1000; i++) {
if (condition) {
broke = true;
break;
}
}
if (broke)
break;
}
If you want it less readable you can join the boolean evaluation:
bool broke = false;
for(int i = 0; i < 1000 && !broke; i++) {
for(int j = 0; j < 1000; i++) {
if (condition) {
broke = true;
break;
}
}
}
As an ultimate way you can invalidate the initial loop:
for(int i = 0; i < size; i++) {
for(int j = 0; j < 1000; i++) {
if (condition) {
i = size;
break;
}
}
}
Use this wise advice from LLVM team:
"Turn Predicate Loops into Predicate Functions"
See:
http://llvm.org/docs/CodingStandards.html#turn-predicate-loops-into-predicate-functions
If you need the values of i and j, this should work but with less performance than others
for(i;i< 1000; i++){
for(j; j< 1000; j++){
if(condition)
break;
}
if(condition) //the same condition
break;
}
Caution: This answer shows a truly obscure construction.
If you are using GCC, check out this library.
Like in PHP, break
can accept the number of nested loops you want to exit.
You can write something like this:
for(int i = 0; i < 1000; i++) {
for(int j = 0; j < 1000; j++) {
if(condition) {
// break two nested enclosing loops
break(2);
}
}
}
for(int i = 0; i < 1000; i++) {
for(int j = 0; j < 1000; i++) {
if(condition) {
goto end;
}
}
end:
int i = 0, j= 0;
for(i;i< 1000; i++){
for(j; j< 1000; j++){
if(condition){
i = j = 1001;
break;
}
}
}
Will break both the loops.
for(int i = 0; i < 1000; i++) {
for(int j = 0; j < 1000; i++) {
if(condition) {
func(para1, para2...);
return;
}
}
}
func(para1, para2...) {
stmt2;
}
i = 0;
do
{
for (int j = 0; j < 1000; j++) // by the way, your code uses i++ here!
{
if (condition)
{
break;
}
}
++i;
} while ((i < 1000) && !condition);
来源:https://stackoverflow.com/questions/9695902/how-to-break-out-of-nested-loops