I was wondering if there was a single formula anybody knew of to calculate the number of iterations in a loop beforehand.
I played around with it and got to the equation:
the number of iterations is equal to ((the absolute value of the difference of the final value and initial value) plus (the absolute value of the increment/decrement)) divided by the absolute value of the increment/ decrement
or
expected = (int)((Math.abs(final-initial) + Math.abs(iteration)) / iteration),
for example:
for(int i = 43; i > 9; i-=8) , will run 5 times, because 43-10 (you add one to the initial if it is doesn't include) = 33+8 = 41/8 ~ 5
If it is equal to or greater than- for(int i = 43; I >= 9; i-=8), then you'd do (43-9+8) / 8 (5) according to my formula
I've tested it a few times but not sure yet.
Thanks.