9. Loops

For repeated execution of similar things, loops are used. If you are familiar with other programming languages you have probably heard about for-loops, while-loops, and until-loops. Fortran 77 has only one loop construct, called the do-loop. The do-loop corresponds to what is known as a for-loop in other languages. Other loop constructs have to be built using the if and goto statements.

do-loops

The do-loop is used for simple counting. Here is a simple example that prints the cumulative sums of the integers from 1 through n (assume n has been assigned a value elsewhere):

```      integer i, n, sum

sum = 0
do 10 i = 1, n
sum = sum + i
write(*,*) 'i =', i
write(*,*) 'sum =', sum
10  continue
```

The number 10 is a statement label. Typically, there will be many loops and other statements in a single program that require a statement label. The programmer is responsible for assigning a unique number to each label in each program (or subprogram). Recall that column positions 1-5 are reserved for statement labels. The numerical value of statement labels have no significance, so any integers can be used, in any order. Typically, most programmers use consecutive multiples of 10.

The variable defined in the do-statement is incremented by 1 by default. However, you can define the step to be any number but zero. This program segment prints the even numbers between 1 and 10 in decreasing order:

```      integer i

do 20 i = 10, 1, -2
write(*,*) 'i =', i
20  continue
```

The general form of the do loop is as follows:

```      do label  var =  expr1, expr2, expr3
statements
label continue
```

var is the loop variable (often called the loop index) which must be integer. expr1 specifies the initial value of var, expr2 is the terminating bound, and expr3 is the increment (step).

Note: The do-loop variable must never be changed by other statements within the loop! This will cause great confusion.

The loop index can be of type real, but due to round off errors may not take on exactly the expected sequence of values.

Many Fortran 77 compilers allow do-loops to be closed by the enddo statement. The advantage of this is that the statement label can then be omitted since it is assumed that an enddo closes the nearest previous do statement. The enddo construct is widely used, but it is not a part of ANSI Fortran 77.

It should be noted that unlike some programming languages, Fortran only evaluates the start, end, and step expressions once, before the first pass thought the body of the loop. This means that the following do-loop will multiply a non-negative j by two (the hard way), rather than running forever as the equivalent loop might in another language.

```      integer i,j

do 20 i = 1, j
j = j + 1
20  continue
write (*,*) j
```

while-loops

The most intuitive way to write a while-loop is

```      while (logical expr) do
statements
enddo
```

or alternatively,

```      do while (logical expr)
statements
enddo
```

The program will alternate testing the condition and executing the statements in the body as long as the condition in the while statement is true. Even though this syntax is accepted by many compilers, it is not ANSI Fortran 77. The correct way is to use if and goto:

```label if (logical expr) then
statements
goto label
endif
```

Here is an example that calculates and prints all the powers of two that are less than or equal to 100:

```     integer n

n = 1
10 if (n .le. 100) then
write (*,*) n
n = 2*n
goto 10
endif
```

until-loops

If the termination criterion is at the end instead of the beginning, it is often called an until-loop. The pseudocode looks like this:

```      do
statements
until (logical expr)
```

Again, this should be implemented in Fortran 77 by using if and goto:

```label continue
statements
if (logical expr) goto label
```

Note that the logical expression in the latter version should be the negation of the expression given in the pseudocode!