Iterating from MinValue to MaxValue with overflow

Not taking into consideration an obvious

output all except MaxValue, then output MaxValue

approach, I see one solution.
It works, but looks weird and throws OverflowException if checked :)

sbyte i = sbyte.MinValue;
do
{
    Console.WriteLine(i++);
} while (i != sbyte.MinValue);

You can try this :

for (sbyte i = sbyte.MinValue; i <= sbyte.MaxValue; i++)
{
    Console.WriteLine(i);
    if(i==sbyte.MaxValue)
     break;
}

You can use an int just for the condition and the sbyte inside.

int checkVal = sbyte.MinValue;
for (sbyte i = sbyte.MinValue; checkVal <= sbyte.MaxValue; i++, checkVal++)
{
    Console.WriteLine(i);
}

Tags:

C#

For Loop