Unsigned int reverse iteration with for loops
The following does what you want:
for (unsigned i = 10; i != static_cast<unsigned>(-1); --i)
{
// ...
}
This is perfectly defined and actually works. Arithmetic on signed types is accurately defined by the standard. Indeed:
From 4.7/2 (regarding casting to an unsigned type):
If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2^n where n is the number of bits used to represent the unsigned type)
and 3.9.1/4
Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2^n where n is the number of bits in the value representation of that particular size of integer
You can use
for( unsigned int j = n; j-- > 0; ) { /*...*/ }
It iterates from n-1
down to 0
.