For Loop Using UInt16 MaxValue

A demonstration of a For Loop Using UInt16 MaxValue. How would you iterate through all of the values in an unsigned 16-bit integer?

The following two loops will not work:


for (UInt16 i = 0; i < UInt16.MaxValue; i++)
{
    // Do something
}

for (UInt16 i = 0; i <= UInt16.MaxValue; i++)
{
    // Do something
}

The first loop skips the last value; the second loop goes into an infinite loop when it rolls over.

Let’s take a look at the first loop.


for (UInt16 i = 0; i < UInt16.MaxValue; i++)
{
    // Do something
}

First we need to know what UInt16.MaxValue equates to as a value.  UInt16.MaxValue is hexadecimal 0xFFFF which is a constant value of 65535.  So when we are looking into the loop with for (UInt16 i = 0; i < UInt16.MaxValue; i++) the last iteration will not happen because we would need an overflow value of 65536.  However, since UInt16.MaxValue is a constant value of 65535, the last value causes the loop to exit before the last Do Something is completed.

Let’s take a look at the second loop.

for (UInt16 i = 0; i <= UInt16.MaxValue; i++)
{
    // Do something
}

The problem we are seeing here is an infinite loop gets created.  This again takes us to needing to understand how UInt16.MaxValue works.  As we said, UInt16.MaxValue is the hexadecimal value of 0xFFFF.  So if you look at this loop if will actually go through and complete the last iteration at 65535.  However, in terms of this hexadecimal value, when you add 1 to it after the last iteration if wraps around to 0 again. Hence, our infinite loop problem.

Solution? Use a do/while loop.

ushort i = 0;
do
{
    // do something
}
 while(i++ < UInt16.MaxValue);

This is close to the first loop in our question, but it will cause the Do Something to iterate all the way through before it does a check on the UInt16 value.

Here it is as a test console app to confirm all the above from Guy Ellis.


static void Main(string[] args){
    UInt16 i = 0;
    do    {        if (i == UInt16.MinValue)
        {
            Console.WriteLine("MinValue");
        }
        if (i == UInt16.MaxValue)
        {
            Console.WriteLine("MaxValue");
        }
    } while (i++ < UInt16.MaxValue);
    Console.WriteLine("Enter to finish...");
    Console.ReadLine();}


StackOverflow Profile