HP Fortran Programmer's Reference (September 2007)

Execution control
Control constructs and statement blocks
Chapter 6144
money = 1000
invest = money
interest = .04
years = 0
DO WHILE (money < 2*invest) ! doubled our money?
years = years + 1
money = money + (interest * money)
END DO
PRINT *, ”Years =”, years
Infinite DO loop
The DO statement for the infinite DO loop contains no loop control logic. It executes a statement
block for an indefinite number of iterations, until it is terminated explicitly by a statement
within the block; for example, a RETURN or EXIT statement.
Syntax
[
construct-name
:] DO
statement-block
END DO [
construct-name
]
Execution logic
The execution sequence of an infinite DO loop is as follows:
1. The loop becomes active.
2.
statement-block
executes.
3. Go to Step 2.
Example
! Compute the average of input values; press 0 to exit
INTEGER :: i, sum, n
sum = 0
n = 0
average: DO
PRINT *, 'Enter a new number or 0 to quit'
READ *, i
IF (i == 0) EXIT
sum = sum + i
n = n + 1
END DO average
PRINT *, 'The average is ', sum/n