Unsigned int reverse iteration with for loops

11,013

Solution 1

You can use

for( unsigned int j = n; j-- > 0; ) { /*...*/ }

It iterates from n-1 down to 0.

Solution 2

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

Solution 3

My pattern for this is usually...

for( unsigned int i_plus_one = n; i_plus_one > 0; --i_plus_one )
{
    const unsigned int i = i_plus_one - 1;
    // ...
}

Solution 4

Are you really iterating down from some number greater than std::numeric_limits<int>::max()? If not, I would actually suggest just using a normal int as your loop variable and static_cast it to unsigned in the places in your code that expect it to be unsigned. This way you can use the intuitive >= 0 or > -1 condition and in general I would expect it to be more readable than any of the unsigned alternatives.

The static_cast would just be to tell the compiler how to operate on the variable and have no performance implications at all.

Solution 5

Avoiding underflow

unsigned int i = n;
while (i != 0) {
  --i;
  ...
}
Share:
11,013
deceleratedcaviar
Author by

deceleratedcaviar

Updated on June 06, 2022

Comments

  • deceleratedcaviar
    deceleratedcaviar almost 2 years

    I want the iterator variable in a for loop to reverse iterate to 0 as an unsigned int, and I cannot think of a similar comparison to i > -1, as you would do if it was a signed int.

    for (unsigned int i = 10; i <= 10; --i) { ... }
    

    But this seems very unclear, as it is relying on the numerical overflow of the unsigned integer to be above 10.

    Maybe I just don't have a clear head, but whats a better way to do this...

    Disclaimer: this is just a simple use case, the upper limit of 10 is trivial, it could be anything, and i must be an unsigned int.