My first reverse attempt for a loop that does something n times was something like:
for ( unsigned int i = n-1; i >= 0; i-- ) { ... }
This fails because in unsigned arithmetic i always guaranteed to be greater than or equal to zero, so the loop condition will always be true. Fortunately, the gcc compiler warned me of a "pointless comparison" before I had to wonder why the loop was running endlessly.
I am looking for an elegant way to solve this problem, bearing in mind that:
- It should be a reverse cycle.
- The loop index must be unsigned.
- n is a constant constant.
- It should not be based on the "obscure" ring arithmetic of unsigned integers.
Any ideas? Thank:)
c for-loop
Auron Mar 20 '09 at 11:26 2009-03-20 11:26
source share