If there is no reason to do otherwise, I would always write a loop like this:
const int tmax = 3;
for (int t=0;t<tmax;t++){std::cout << t << std::endl;}
If you only care about how many times the loop is executed but you do not need the value of t
inside the loop, there is actually no reason to use something else. However, if you want to count down, you could do this:
for (int t=tmax;t!=0;t--){std::cout << t << std::endl;}
or
for (int t=0;t<tmax;t++){std::cout << tmax - t << std::endl;}
which one may consider both as ugly (I personally do not like the first too much, because I am a devotee of counting up in for loops whenever possible. The second one isnt nice because you might use the value tmax-t
several times inside the loop, i.e. either you introduce an extra varible or the opportunity for bugs) and prefer to write
int t = tmax;
while (t--){std::cout << t << std::endl;}
which has the little disadantage of introducing an extra variable outside of the loop. However, when tmax
isnt const and the value isnt needed after the loop, probably the most compact way to write the loop is this:
while (tmax--){std::cout << tmax << std::endl;}
I am pretty sure they are all the same in performance and it is just about your prefered coding style, but I hope I could give you a hint, why someone would prefer one over the other.