The classic way to do your iteration efficiently with a single test is the do / while :
unsigned i = 0; do { f(i); } while (i++ != UINT_MAX);
If you insist on using a for loop:
for (unsigned i = 0;; i++) { f(i); if (i == UINT_MAX) break; }
Here is another option with two variables, where all the logic is inside the for statements:
for (unsigned int i = 0, not_done = 1; not_done; not_done = (i++ - UINT_MAX)) { f(i); }
It can generate slower code due to an additional variable, but, as BeeOnRope commented, clang and icc compile its very efficient code .
chqrlie Nov 05 '16 at 1:31 on 2016-11-05 13:31
source share