C++ – For loop with variable step size – C++

cfor-loop

I want to use a for loop with variable step size, in particular I want that the i variable in for loop is equal to:

  1. All numbers except the ones which can be divided by 3 so:

i = 1-2-4-5-7-8-10-11-13-14-16-17…

  1. All numbers except the ones which can be divided by 3 and 2 so:

i = 1-5-7-11-13-17…

The base code:

#include <iostream>

int main()
{
    int N = 100;
    for ( int i=0; i<N; i++) { //<-----
        //instructions
    }
    return 0;
}

Is it possible with for loop?

Best Answer

for(int i = 0; i < N; i++)
{
    if(i % 3 == 0) // if(i % 3 == 0 || i % 2 == 0) // if(is_skippable(i))
        continue;
    //instructions
}