Sanity Checking Loops


Simple checks for loops

Any programmer who has written many loops has made mistakes with them. Loops are one of the places where programmers tend to make mistakes.

There are a few simple checks that you can make to check whether a loop makes sense.

  1. Identify the loop control variables. Are all of the loop control variables initialized? Are they initialized close to the beginning of the loop, not far away?

  2. Are all of the loop control variables updated in a sensible way? Check each one.

  3. Often a loop uses a value, such as n, in the loop heading to decide how far to go. Usually, that value n is not used in the loop body, and using it is probably a mistake. For example, a common mistake is to have a loop with a counter, but to use the end value where you meant to use the counter. Here is an incorrect loop to write the factorials of the numbers from 1 to n.

      Let count = 1.
      While count <= n do
        Displayln $(n) ++ "    " ++ factorial(n).
        Relet count = count + 1.
      %While
    
    Notice that n, which is really there just to decide how far to go, should not occur inside the loop body. But it does. It should be replaced by count.


Problems

  1. [solve] The following loop is supposed to show the factorial of each number from 1 to n. What is wrong with the following loop. Assume that n is known and that the factorial function is available.

      Let k = 1.
      While k <= n do
        Display factorial(n).
        Relet k = k + 1.
      %While
    

  2. [solve] The following loop is supposed to show the factorial of each number from 1 to n. What is wrong with the following loop. Assume that n is known and that the factorial function is available.

      Let k = 1.
      While k <= n do
        Display factorial(k).
      %While
    

  3. [solve] The following loop is supposed to show the factorial of each number from 1 to n. What is wrong with the following loop. Assume that n is known and that the factorial function is available.

      While k <= n do
        Display factorial(k).
        Relet k = k + 1.
      %While
    

  4. [solve] The following loop is supposed to show the factorial of each number from 1 to n. What is wrong with the following loop. Assume that n is known and that the factorial function is available.

      Let k = k + 1.
      While k < n do
        Display factorial(k).
        Relet k = k + 1.
      %While
    


Summary

A few checks can identify problems with loops with very little work. It is worth doing them after you have written a loop.

Check whether you have initialized and updated all loop control variables.

Check whether you are using a limit variable in places where you should be using a loop control variable.


Review

Loop control variables are variables that the loop body changes. Be sure to initialize the loop control variables before the loop.

Loops are places where programmers often make mistakes. Strongly consider doing a careful hand simulation of a loop to see what it does.