Iteration (or Looping)

DO Loops

A DO loop allows repetition of a section of program a set number of times. In DO loops the section to be repeated is started with the statement DO and ended with a labelled CONTINUE statement. The repeated part of the program is called the loop body, and the number of times this is repeated is set by the START, STOP and STEP values.

	  DO n X = START, STOP, STEP
         .
         .
	       loop body
	       .
	       .
	       .
n       CONTINUE

where
n is a statement line number containing the region for the repetitive loop. It can be a positive integer of up to five digits. X will be incremented each time round the loop by the value of STEP. Its initial value is START and looping will end when x exceeds the value STOP at which point the program will carry on after the CONTINUE statement. X is called the loop index and must be an integer variable. The name is not restricted to X. It is also possible to have implied DO loops.

Example

     DO 12 J = 1,10

12   CONTINUE

When the STEP value is omitted from the expression, its default value is set to 1. The loop body is normally indented to support readability of programs.

Example

      DO 20 I = 10,0,-1

      Print *, I
 20   CONTINUE

In the above example the value of I is printed. Initially it is 10 and reduces by one each time round the loop until I has a value of -1.

Now go to the exercises and work your way through the looping problems.


Work your way through the following components attempting the exercises as you come across them:

| Programs | Variables | Arithmetic Operations | Input and Output | Looping in Programs | Arrays in Programs | Checking variables | Subprograms and functions |