#159 – A while Loop Might Execute Forever

If the boolean expression that controls a while loop never evaluates to false, the loop might continue to execute indefinitely.  This is known as an infinite loop.

You might create an infinite loop by design, or it might in fact be a programming error.  The only way to end an infinite loop is to kill the parent process.

In the following example, we just use the value of true for the loop expression, so that the loop will continue to execute indefinitely.

            uint digit = 1;

            while (true)

In the example below, we intend for Sisyphus to roll his rock up the hill ten times.  We intend to decrement the numRolls variable, but mistakenly increment it.  This leads to an infinite loop, with Sisyphus doomed to roll his rock up the hill forever.

            uint numRolls = 10;

            while (numRolls > 0)