-7

The continue statement should act on the inner loop, right?? Or am I missing something?

    for (j=0; j< 100; j++)
    {
    for (i=0 ; i<10; i++)
    {

    bool flag = false;

        //CALL TO A FUNCTION WHICH, BASED ON SOME CONDITION, MODIFIES FLAG AND RETURNS IT TO THIS LOOP

         if(flag)
           {
             continue;  //SHOULDN'T IT CONTINUE THE LOOP ON i??????
           }

       //CALLS TO SOME OTHER FUNCTIONS WHICH SHOULDN'T BE CALLED IF FLAG IS 1

      }
   }
ZeroTwo
  • 307
  • 1
  • 3
  • 12
  • Yes, I did. In my case, it doesn't iterate to the next i value, it moves on to calling the other functions below that condition, even when the flag is 1 now (I know because I checked by printing it) – ZeroTwo Apr 05 '16 at 08:40
  • Sorry I printed the value in the function and it gave 1 but while returning the value there is some error I suppose because in my i loop, now when I printed it, it's 0 again. I'll see what's wrong. – ZeroTwo Apr 05 '16 at 08:42
  • I corrected the returning error and now it iterates to outer loop instead of the inner one. – ZeroTwo Apr 05 '16 at 08:50
  • 1
    Could you paste up the shortest possible version of the code that exhibits the problem? – Bathsheba Apr 05 '16 at 08:51

2 Answers2

2

Continue will move to the next iteration of the closest loop inside which it is called. In your case it will move to the next iteration of the second loop i.e. the one with value i.

If the value of i is 9 when it encounters continue it will go to the second loop and as the value of i becomes 10 after increment it does not satisfy the condition and it will move to the next iteration of the outer loop with value j.

Joyson
  • 3,025
  • 1
  • 20
  • 34
  • I don't know why in my code it moves to next iteration of j. Can you suggest something? – ZeroTwo Apr 05 '16 at 08:49
  • Can you add a print statement inside the if condition to confirm that it does enter the if condition and the continue statement is executed. – Joyson Apr 05 '16 at 10:49
0

If the inner loop counter(i) is at 9 when it hits the continue statement, then the inner loop will exit and one iteration of the outer loop will occur before entering the inner loop with its counter initialized to 0.

eoD .J
  • 302
  • 2
  • 5