17

Consider this C++ code

for(int i=0; i<=N; ++i)

{

if(/* Some condition 1 */) {/*Blah Blah*/}

if(/* Some condition 2  */){/*Yadda Yadda*/}

}

Is there any keyword/command so that if condition 1 evaluates to true and execution of /*Blah Blah*/ I can skip the rest of the current iteration and begin a new iteration by incrementing i.

The closest thing I know to this kind of statement skipping is break but that terminates the loop entirely.

I guess one could do this by using some flags and if statements, but a simple keyword would be very helpful.

smilingbuddha
  • 13,492
  • 29
  • 105
  • 180

3 Answers3

35

Use the keyword continue and it will 'continue' to the next iteration of the loop.

0x5f3759df
  • 2,319
  • 1
  • 19
  • 24
  • So in this case I should use `continue` at the tail-end of the `/*Blah Blah*/` statements, correct? – smilingbuddha Sep 22 '11 at 22:03
  • Correct. You place `continue;` at whatever point in the flow you want it to move on to the next iteration of the loop. – 0x5f3759df Sep 22 '11 at 22:06
  • I `#define skip continue` in my main header file. `skip` then becomes a synonym for `continue` and it means to "skip" a loop iteration, (and hence `continue` on with the next) – bobobobo Jul 27 '13 at 17:12
5

This case seems better suited for if..else.. than a continue, although continue would work fine.

for(int i=0; i<=N; ++i)
{
    if(/* Some condition 1 */)
    {/*Blah Blah*/}
    else if(/* Some condition 2  */)
    {/*Yadda Yadda*/}
}
Kashyap
  • 13,836
  • 12
  • 58
  • 96
0

Using the Continue statement, stops the current loop and continues to the next, rather than breaking altogether

Continue

Mob
  • 10,739
  • 6
  • 39
  • 57