Loops for Actions


Loops for actions

Some loops just do something over and over. So they perform a job. As an example, lets write a program that writes a table of factorials, in the form

    n      factorial(n)
    1      1
    2      2
    3      6
    4      24
    5      120
    6      720
If you cannot use a loop, you might write the following sequence of statements.
  Displayln "n      factorial(n)".
  Displayln $(1) ++ "      " ++ $(factorial(1)).
  Displayln $(2) ++ "      " ++ $(factorial(2)).
  Displayln $(3) ++ "      " ++ $(factorial(3)).
  Displayln $(4) ++ "      " ++ $(factorial(4)).
  Displayln $(5) ++ "      " ++ $(factorial(5)).
  Displayln $(6) ++ "      " ++ $(factorial(6)).
But suppose you want it to show factorials up to 20, or larger? This is not a very good approach. But notice how similar the statements are. You should be able to use a loop with a counter. What the loop will do is
  Displayln "n      factorial(n)".
  Let n = 1.
  Displayln $(n) ++ "      " ++ $(factorial(n)).
  Relet n = n + 1.
  Displayln $(n) ++ "      " ++ $(factorial(n)).
  Relet n = n + 1.
  Displayln $(n) ++ "      " ++ $(factorial(n)).
  Relet n = n + 1.
  Displayln $(n) ++ "      " ++ $(factorial(n)).
  Relet n = n + 1.
  Displayln $(n) ++ "      " ++ $(factorial(n)).
  Relet n = n + 1.
  Displayln $(n) ++ "      " ++ $(factorial(n)).
  Relet n = n + 1.
Now exactly the same two statements
  Displayln $(n) ++ "      " ++ $(factorial(n)).
  Relet n = n + 1.
are being repeated over and over, and that is exactly what a loop does well. Here is the program. We incorporate a function that computes factorials, and use one loop control variable, n, that counts up.
  Define
    case factorial(n) = 1                    when n == 1
    case factorial(n) = n * factorial(n-1) 
  %Define

  Execute
    Displayln "n      factorial(n)".
    Let n = 1.
    While n <= 20 do
      Displayln $(n) ++ "      " ++ $(factorial(n)).
      Relet n = n + 1.
    %While
  %Execute


Using loops to make procedures

Recall that a procedure is like a function, but it just does a job, and does not have an answer. In Cinnameg, a procedure has a name that begins with a capital letter. Here is a version of the same program as above, but it defines a procedure that prints the factorials from a given starting number to a given ending number. There is one loop control variable, n.

  Define
    case factorial(n) = 1                    when n == 1
    case factorial(n) = n * factorial(n-1) 
  %Define

  Define PrintFactorials(low: Integer, high: Integer). =
    Displayln "n      factorial(n)".
    Let n = low.
    While n <= high do
      Displayln $(n) ++ "      " ++ $(factorial(n)).
      Relet n = n + 1.
    %While
  %Define

  Execute
    PrintFactorials(1,20).
  %Execute


Problems

  1. [solve] Write a program that writes all of the prime numbers that are less than or equal to 40, one per line. Design the program so that it has one line that defines limit to be 40, then loops up to limit. Make is so that, if you change limit to 50, then it will show all of the prime numbers that are less than or equal to 50.

    For this problem, a function isPrime(n) is available to you, which yields true if n is prime.

  2. [solve] Write a program that writes the first 20 prime numbers, one number per line. Write it by defining howMany to be 20, and then writing the first howMany prime numbers. If you change the definition of howMany to 30, the program should show the first 30 prime numbers.

    (Hint. You will find it useful to have another variable that keeps track of how many numbers have already been written. Keep going until the number written is equal to howMany.)

    For this problem, a function isPrime(n) is available to you, which yields true if n is prime.

  3. [solve] This is the same as the preceding exercise. But this time, you should use a procedure to do the work.

    Write a program that writes the first 20 prime numbers, one number per line. Do that by defining a procedure, WritePrimes(howMany), that writes the first howMany prime numbers. Then make an Execute part that just does WritePrimes(20).

    For this problem, a function isPrime(n) is available to you, which yields true if n is prime.


Summary

Some loops just do a job by performing some sequence of statements over and over, up to some limit. To write such a loop, think about the action that you want to repeat, and how long you want it to go. You probably need loop control variables.


Review

A loop repeats some sequence of statements. The loop body is the sequence of statements that is repeated.

A while-loop is a kind of loop that repeats the statements until some condition is false.

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