# Loop issue

• 01-01-2008
Gordon
Loop issue
Code:

```for(int i = 0; i <= x; i++) {     for(int j = 0; j <= y; j++)     {           // Break point     } }```
Hello

When I have two loops like this and i need to break both loops from the inner loop how do I do that?

I tried

Code:

```for(int i = 0; i <= x; i++) {     for(int j = 0; j <= y; j++)     {           break;     } } continue;```
Apparantly didnt work...
• 01-01-2008
rags_to_riches
Code:

```int flag = 0; for(int i = 0; i <= x && !flag; i++) {     for(int j = 0; j <= y; j++)     {           // On break condition:           flag = 1;           break;     } }```
• 01-01-2008
Gordon
Quote:

Originally Posted by rags_to_riches
Code:

```int flag = 0; for(int i = 0; i <= x && !flag; i++) {     for(int j = 0; j <= y; j++)     {           // On break condition:           flag = 1;           break;     } }```

Actually solved it out already... THX anyways
• 01-02-2008
Elysia
Or
Code:

```int flag = 0; for(int i = 0; i <= x; i++) {         for(int j = 0; j <= y; j++)         {                 // On break condition:                 flag = 1;                 break;         }         if (flag) break; }```
Looks better IMO. Complex expressions for loops just confuse you.
• 01-02-2008
novacain
Or just let the loops exit on their own?

Code:

```for(int i=0;i<x;i++) {         for(int j=0;j<y;j++)         {               //leave?               if(some condition)               {                         i=x;                         j=y;               }         } }```
• 01-02-2008
grumpy
Or, if you want to do it without introducing an additional variable;
Code:

```for(int i=0;i<x;i++) {         for(int j=0;j<y;j++)         {               //leave?               if(some condition) goto out;         } } out:```
Responses about the evils of goto will be summarily ignored. ;)
• 01-03-2008
Elysia
That's one example I can't agree on. Goto may have its uses, but this is not one of them.
Using a flag or filling the loop conditions is much better.
• 01-03-2008
laserlight
Quote:

That's one example I can't agree on. Goto may have its uses, but this is not one of them.
Actually, I would consider this as one of the few valid uses of goto: to break from multiple nested loops. The effect is similiar to using a break, except that it requires the label to the placed right after the nested loops so as not to turn the thing into spaghetti code as goto is infamous for.

The trade-off is therefore the same as a break: you may make it easier to understand because the loop condition need not be so complex, but you also introduce multiple points where the loop may end.
• 01-03-2008
Elysia
Well, case in point, there's nothing stopping you from using it, so choose at will. I think I may stay from it, though. Just my own opinion.
• 01-03-2008
abachler
The use of goto is not inherently bad, it just gets the blame for sloppy programmers that misuse/abuse it. This is one case though where it is clearly the more readable choice.
• 01-03-2008
Yarin
gotos being bad is a common mis-conception, they are like any other code, they are only bad if the programmer mis-uses them.
I use them alot recently, they simplify code, makes it easier to manage, and has yet to give me any problems.

And this really should be in the C or C++ forum, not Windows.
• 01-06-2008
vart
Quote:

Originally Posted by Elysia
Using a flag or filling the loop conditions is much better.

It is fully incorrect for timecritical loops. - This solution adds the additional flag check on each iteration of the outer loop (and may - depending on the implementation - add additional check on the inner loop) - adding CPU cycles and also resulting in higher rate of branch mispredicitons.

So when the Profiler shows the performance problems for such loops the usage of goto could be the only possible solution...
• 01-06-2008
twomers
For a doubly nested loop I think goto is not really that necessary, and flag-management can be applied all over the gaff. However... if you had four or five separate loops like so:

Code:

```for(){   for() {     do {     while();   }   for() {     while() {       for() {       }       for() {         while() {           for() {           }         }       }     }   } }```
You might talk about redesigning this process but flagging becomes a hindrance and to be honest it's more likely that that your flag management is going to become more difficult and buggy than goto screwing up, so long as you ensure that all dynamic memory is deleted etc (as it should be even if you're using break).
• 01-06-2008
vart
if you have 4-5 levels of looping - you can start thinking of creating functions for inside loops.
And in this case the breaking outside the loop can be done simply by return
• 01-07-2008
iMalc
Quote:

Originally Posted by Yarin
gotos being bad is a common mis-conception, they are like any other code, they are only bad if the programmer mis-uses them.
I use them alot recently, they simplify code, makes it easier to manage, and has yet to give me any problems.

And this really should be in the C or C++ forum, not Windows.

Anyone who ever states that they use goto's "a lot" is definitely misusing them!

vart: Explicitly filling the loop conditions (as per novacain's post) does not add an "additional flag check on each iteration of the outer loop".

Wrapping it in a seperate function is usually the way to go.