Infinite Loops


Loops that don't ever stop

It is easy to write a loop that will not stop. For example, suppose that you forget to update a counter. Here is a loop that is intended to print higher and higher powers of 2, starting with 1 = 20

  Let k = 0.
  Let r = 1.
  While k < 20 do
    Displayln $(k) ++ "    " ++ $(r).
    Relet k = k + 1.
    Relet r = 2*r.
  %While
When you run that loop, it starts spewing out lines
 1    1
 1    2
 1    4
 1    8
 1    16
 1    32
 ...
and it will not stop. The web-page tester will stop it eventually, since it has a time limit and an output size limit. But when you are not using the tester, you need to use other means to stop the program. We will see those other means when we look at running programs without the tester.