4.1 Basic for Loops

One of the reasons that computers are useful is their ability to handle repetitive tasks. Suppose that we wanted to print numbers and their squares from 1 to 5. We could do it this way:

put 1:2, 1*1:5
put 2:2, 2*2:5
put 3:2, 3*3:5
put 4:2, 4*4:5
put 5:2, 5*5:5 

This produces output of:

 1    1
 2    4
 3    9
 4   16
 5   25

This is not much fun and very repetitive. The only thing we change on each line is the number. This would get extremely annoying if we were doing values from 1 to 10000 instead of 1 to 5. Fortunately there is a better way. We can use a for loop. To do the same thing as the previous example we'd just need to type:

for n: 1 .. 5
   put n:2, n * n:5
end for 

The variable n is called the loop control variable. Notice that it is not declared by using a var statement. The variable after the for is automatically declared for you and can only be used between the for and the end for. The 1 .. 5 tells you the starting and ending values that n will use. You repeat the statement(s) between the for and the end for, with n taking on the values 1, 2, 3, 4, and 5. The beauty of the for loop is that if we needed to use values from 1 to 10000 we would just have to change the for loop to say --> for n: 1 .. 10000

For loops can also also work with characters. The following program will print all the capital letters, one to a line.

for letter: 'A' .. 'Z'
   put letter
end for 

The starting and ending values do not have to be constants. They can be expressions as long as they are both characters or integers. You cannot use reals for the loop control variable. Here is an example where both the starting and ending values use variables and calculations to give the starting and ending values.

var start : int := 5
for count : start + 1 .. start * 2
   put "count = ", count
end for 

Since start is 5 the first value for the for loop will be 6. The ending condition is start * 2 (10). So the loop takes on values 6, 7, 8, 9, and 10. The output would be:

count = 6
count = 7
count = 8
count = 9
count = 10

If the final value for the loop control variable is already smaller than the initial value, the body of the loop is not executed at all. The following example illustrates this:

put "Just before loop."
var first : int := -5
for count : first .. first * 3
   put count, "times through the loop"
end for
put "Right after loop."
 

It produces

Just before loop.
Right after loop.

You can't use the loop control variable outside the for loop. If you try to run the following program:

for i: 10 .. 20
   put i
end for
put "i outside the loop is ", i 

The last line will be highlighted and you will get the error message 'i' has not been declared.

You cannot change the value of the loop control variable. You must allow Turing to control what value it has. The following program would have an error:

for i : 1 .. 10
   i := i + 3 % error, can't change value of loop control variable
   put i
end for 


Exercise 4.1

  1. What does each program print
    1. for location : 1 .. 20
         put "*" ..
      end for 
    2. for initial : 'J' .. 'P'
         put "\"", initial, "\""
      end for 
    3. for value : 1 .. 25
         if value mod 3 = 0 then
            put value
         end if
      end for 
    4. for countDown : 5 .. 1
         put countDown, " seconds and counting"
      end for 
    5. var last : int := 5
      for count : -2 .. last
         put count:2, last:2
         last := last - 1
      end for 
    6. for i : 14 .. 14
         put i
      end for 
    1. Write a program that will print a table containing the integers from one to forty along with their squares, cubes, square roots, and reciprocals. The table should contain appropriate headings and be neatly formatted.
       
    2. Modify your program so that it inserts a blank line after every fifth line of the table.

  2. Write a program to find the largest value in a collection of N real numbers, where the value of N is the first data item read by your program. The program should print all the numbers read before printing the largest. Test the program with (at least) the following sets of data.
    1. 3 2.7 -0.63 5.12
    2. 0
    3. 4 -6.2 -1.33 -4 -7.9