Back

Iteration

Iteration (sometimes called 'repetition')
Iteration is the name given to the construct that repeats blocks of code. There are three types of iteration construct, each with a subtle difference from the other two!

FOR COUNTER=1 TO MAX DO
If you need to call a block of code a fixed number of times then you should use a FOR loop. Study the following example.

INPUT MAX
FOR COUNTER = 1 TO MAX DO
BEGIN
      PRINT "This is loop number", MAX
END
MORE instructions

When the above code is run, a value is entered from the keyboard and assigned to the variable MAX. The FOR loop is entered. The Counter is assigned to 1 and the code between BEGIN and END is done. The COUNTER is incremented and the code between BEGIN and END is done again. This continues until MAX is reached. After the MAX loop is done, the program drops out of the loop and MORE instructions in the program sequence are done.

WHILE (CONDITION) DO ENDWHILE
The FOR construction is used if you want to do a block of code a fixed number of times. The WHILE (CONDITION) DO ENDWHILE construction is used if you want to do a block of code a number of times, but you don't know how many! The number of times will be determined by a test before the block of code is executed. If the result of the test is TRUE then the code will be run. If the result is FALSE, then you will drop out of the WHILE loop. Study this example of some code used to allow a user to read some instructions and then press a particular key to continue.

WRITE "Press C to continue".
READ KeyPress
WHILE (KeyPress NOT EQUAL TO C) DO
BEGIN
      WRITE "Press C to continue".
      READ KeyPress
END
ENDWHILE

Study this example. It uses both selection (the IF construct) and iteration (the WHILE construct).

RecordFound=FALSE
EndOfFile=FALSE
READ Keyfield of record you want to find
READ First record in file
WHILE ((EndOFFile=FALSE) AND (RecordFound=FALSE)) DO
BEGIN
      COMPARE record you are looking for with record from file
      IF same THEN
           RecordFound=TRUE
           PRINT "Record found"
      ELSE
           IF EndOfFile Then
                EndOfFile=TRUE
                PRINT "End of file reached. Record not found."
           ELSE
                READ next record
           ENDIF
      ENDIF
END
ENDWHILE

Look at the code between WHILE and ENDWHILE. The code between these two keywords is the code that is executed each time the WHILE loop is done. How this code works is described below.

    • When this code is run, the WHILE ((EndOFFile=FALSE) AND (RecordFound=FALSE)) DO line is reached.
    • The WHILE condition is tested - ((EndOFFile=FALSE) AND (RecordFound=FALSE)) must be TRUE.
    • If the (EndOFFile=FALSE) statement is TRUE, and  (RecordFound=FALSE) is TRUE, then ((EndOFFile=FALSE) AND (RecordFound=FALSE)) will be TRUE and the code in the loop is run.
    • When the code has run once, the loop returns to the WHILE ((EndOFFile=FALSE) AND (RecordFound=FALSE)) DO line
    • The WHILE condition is tested again.
    • The code in the WHILE loop will continue to run until either the (EndOFFile=FALSE) statement becomes FALSE or  (RecordFound=FALSE) becomes FALSE. (You might have to think about this!)
    • When this happens, ((EndOFFile=FALSE) AND (RecordFound=FALSE)) becomes FALSE.
    • Because the WHILE condition is false, the code will not be executed. The program continues from the next one in sequence after the ENDWHILE.

You should notice that with the WHILE loop, it is possible that the block of code associated with it may never actually get executed! In the ' Press C to continue' example on the previous page, if C is actually pressed then (KeyPress NOT EQUAL TO C) becomes a FALSE statement. This results in the WHILE loop being skipped completely and the program continues on from after the ENDWHILE instruction. The WHILE block of code doesn't get executed, not even once in this case!

REPEAT UNTIL (CONDITION)
This is very similar to a WHILE loop, except the condition is tested at the end of the code in the loop! This is a tiny difference, but means that the code will always be executed at least once. Compare this to the WHILE loop, where the code might not get executed at all. A program using a REPEAT construct will look like this:

TOTAL=0
REPEAT
BEGIN
       READ VALUE
      TOTAL=TOTAL+VALUE
END
UNTIL (TOTAL > 1000)

It is important to ensure that WHILE and REPEAT constructs have a way of changing the variable that is being tested to see if the loop should run again. If there is no way of changing this variable, the loop might continue forever. This is known as an ‘infinite loop’. In the above REPEAT example, TOTAL = TOTAL + VALUE is used inside the loop to change TOTAL, the variable that gets tested to see if the loop should run again.

Not all languages make use of REPEAT. For example, if you are using Python, then you will only use the FOR and WHILE iterative constructs. This is not at all a problem as you can always construct code to do exactly what you need from those functions and keywords available.

Back