how to come out of double loop

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-05-2011
suryak
how to come out of double loop
This is the code that I have written..
what I need is when the if statement will become true.. it should break out of the whole loop I mentioned in Italic
how to do it..

Code:

```for(i=0; i<3; i++)   {     for(j=0; j<3; j++)     {       scanf("%d",&sudoku[i][j]);     }   }     for(i=0; i<3; i++)   {     for(j=0; j<3; j++)     {       if ( check_rows(i,j) == 0 )         printf("<<%d, %d>>",i,j);     }   }```
• 07-05-2011
MK27
Some languages allow you to label loops (eg, "inner" and "outer") and break out of nests that way. C kind of does that via goto, but it is generally discouraged as bad style. You can accomplish much the same thing if you place the nest in a function by itself and use return, but that is not always desirable.

So a normative way is via a control variable or "flag":

Code:

```        int i, j, flag = 0;         for (i = 0; i < 3; i++) {                 for (j = 0; j < 3; j++) {                         printf("%d %d\n", i, j);                         if (i + j == 3) {                                 flag = 1;                                 break;                         }                 }                 if (flag) break;         }```
• 07-05-2011
AndrewHunter
Or you could just reuse your if statement in the outer loop:
Code:

```for(i=0; i<3; i++)   {     for(j=0; j<3; j++)     {       if ( check_rows(i,j) == 0 )         printf("<<%d, %d>>",i,j);         break;     }   if( check_rows(i,j)==0)         break; }```
• 07-05-2011
itCbitC
break won't take a numerical argument, so you might have to use a goto unless you can drum up something w/o it.
• 07-05-2011
Matticus
Quote:

Originally Posted by AndrewHunter
Or you could just reuse your if statement in the outer loop:
Code:

```for(i=0; i<3; i++)   {     for(j=0; j<3; j++)     {       if ( check_rows(i,j) == 0 )         printf("<<%d, %d>>",i,j);         break;     }   if( check_rows(i,j)==0)         break; }```

Pardon me for nitpicking, but you forgot the braces around the statements controlled by "if()."

Code:

```for(i=0; i<3; i++)   {     for(j=0; j<3; j++)     {       if ( check_rows(i,j) == 0 ) {         printf("<<%d, %d>>",i,j);         break;         }     }   if( check_rows(i,j)==0)         break; }```
• 07-05-2011
Salem
Or perhaps
Code:

```for ( i = 0 ; i < 3 && !flag ; i++ ) {   for ( j = 0 ; j < 3 && !flag ; j++ ) {     // something sets flag to true, both loops will then exit     // but the side effect is i and j will be incremented anyway (unlike say break or goto)   } }```
• 07-05-2011
AndrewHunter
@Matticus - Thanks for catching that, didn't see it when I was typing up the answer.

@OP - These are all valid suggestions, as MK27 said I would do what you can to stay away from goto. It is a poor programming practice.
• 07-05-2011
nonoob
Bad idea, AndrewHunter. If the test fails in the j loop, that is, the loop completes, then the check outside will have j exceed array bounds.
I'd say this is a perfect example of where a goto is acceptable. Any other solution is ugly and uses more variables.
• 07-05-2011
itCbitC
I'd have to agree with nonoob, as this is one of the "exceptions to the norm" where only a goto would do.
• 07-05-2011
Matticus
Salem's solution seemed like a good clean idea without the need for a "goto," unless the side effect they mentioned (the values of 'i' and 'j' being changed) had a detrimental effect on any code further down the line. I was originally going to suggest updating 'i' and 'j' to values that would fail the "for" loop comparison if the "if()" was executed, but did not for this very reason.
• 07-05-2011
suryak
seems like I got many suggestions here. Thanks, I'll check them and reply again.
• 07-05-2011
iMalc
The best option is to put it in its own function and use a return.

Failing that, the next most efficient option (excluding goto because I would not stoop that low) is to do this:
Code:

```  for(i=0; i<3; i++)   {     for(j=0; j<3; j++)     {         if ( check_rows(i,j) == 0 )         {             i = 3;             break;         }     }   }```
• 07-05-2011
AndrewHunter
Quote:

Originally Posted by nonoob
Bad idea, AndrewHunter. If the test fails in the j loop, that is, the loop completes, then the check outside will have j exceed array bounds.
I'd say this is a perfect example of where a goto is acceptable. Any other solution is ugly and uses more variables.

Thanks for pointing that out; I didn't even consider that.
• 07-05-2011
quzah
Quote:

Originally Posted by nonoob
and uses more variables.

Code:

```for( x = 0; x < this; x++ ) {     for( y = 0; y < that; y++ )     {         if( something )         {             x = this;             y = that;         }     } }```
As long as you don't require the loop control values outside of the loop, you don't need more variables. You don't even need break.

Quzah.
• 07-05-2011
dwks
Personally I prefer to use a flag as Salem has suggested. If side effects are an issue you should be able to get around it with a few extra break statements or "if(flag) break" statements. That's just a general rule, of course you should be flexible depending on exactly what the loop is doing . . . .

Setting the loop control variables to their upper bounds seems like a bad idea to me though. First, it might require more maintenance, especially if the "this/that" boundary expressions are complicated. And a compiler optimizer which would otherwise see a perfect loop with nicely striding loop variables could get confused by your meddling with the control variables. You might get less optimized code as a result.

Finally: using a goto to break out of the outer loop works as well. It's the most socially acceptable use of a goto, but still, it's a goto. I'd use a return statement in a heartbeat but probably not a goto. Although either of these would probably be good in terms of compiler optimization.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last