smilingbuddha
smilingbuddha

Reputation: 14660

C++ Skipping the rest of the current `for` iteration and beginning a new one.

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.

Upvotes: 17

Views: 33009

Answers (3)

Mob
Mob

Reputation: 11098

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

Continue

Upvotes: 0

Kashyap
Kashyap

Reputation: 17451

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*/}
}

Upvotes: 5

0x5f3759df
0x5f3759df

Reputation: 2359

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

Upvotes: 35

Related Questions