Let's consider following loop in C++, where A is vector or other container using .size()
:
for(int n=0; n < A.size(); ++n)
cout << A[n];
I think it is equivalent to the loop below (at least in this case, if it is not really absolutely equivalent, can you help me to figure out why? I cannot find a counter-example to that)
for(int n=-1; ++n < A.size(); )
cout << A[n];
Is using first loop somehow better than second one? I see people using first loop everywhere, but never saw a second one. Why no one do this like in second example? Are there some counter-indication to not do this?
In both cases, value of n
is the same when we execute second line of code, also while exiting loop, we have the same value. Can anything go wrong in the second loop?
For me, second one seems even simpler.