I have an integer parameter which is supposed to control how many times in a particular run an event occurs.
For example, if the number of iterations for each run is 1000, then the parameter FREQ
would be 5, if I wanted to have the event occur every 200 iterations. However, I want to be able to change the number of iterations, but keep the ratio the same, and also to be able to set the FREQ
parameter to 0 if I don't want the event to occur at all.
Here is what I am currently using:
int N_ITER = 1000;
int FREQ = 5;
void doRun(){
int count = 0;
for (int i = 0; i < N_ITER; ++i){
if (FREQ > 0){
if (count < N_ITER/FREQ){
doSomething();
count++;
}
else{
doSomethingElse();
count = 0;
}
}
else{
doSomething();
}
}
}
This works fine, but it doesn't sit right with me having the nested conditionals, especially when I have two lots of doSomething()
, I feel like it should be able to be accomplished more easily than that.
I tried making the one conditional if (FREQ > 0 && count < N_ITER/FREQ)
but obviously that threw a Floating point exception
because of the divide by zero.
I also tried using a try/catch block, but it really was no different, in terms of messiness, to using the nested conditionals. Is there a more elegant solution to this problem?