NavigationRecommended BooksSearch.....Syndicate. |
One safe way to decrement an unsigned in a for loopdon.neufeld points out that there's only one safe way to use a for loop to decrement an unsigned int to 0: for( i = n; i-- > 0; ) "There's a trick here, for the last loop iteration you will have i = 1 at the top of the loop, i-- > 0 passes because 1 > 0, then i = 0 in the loop body. On the next iteration i-- > 0 fails because i == 0, so it doesn't matter that the postfix decrement rolled over the counter." By stienman at Apr 29 2009 - 10:51pm | printer friendly version
|