condition to do something only once every x number of loops

c++, conditional-statements, math

Solution

How about a static variable:

void doSomething(int _arg){
    static int x = 0;

    if (_arg > x) {
        x += 250;  //update x to be crossed next time
        // do something
    }
}

Problem

I can't figure this out so maybe someone here can help me. I have a function with one argument and inside that function there's code I want to run every x number of times. ``` void doSomething(int _arg){ // do something every 250 times } ``` The function is called repeatedly but the argument isn't augmented by 1. So for example the function calls might look like this and I want a condition inside the function to be run every 250 times. ``` doSomething(1); // do something doSomething(130); doSomething(230); doSomething(310); // 250 was passed, do something doSomething(420); doSomething(570); // 2*250 was passed, do something ``` I can't just do something like `if(_arg % 250 == 0)` because the value of `_arg` is irregular (but the intervals are always smaller than 250).

Original source