HP Fortran Programmer's Reference (September 2007)

Execution control
Control constructs and statement blocks
Chapter 6142
A third form, combining elements of the other two, is also supported:
[
construct-name
:] DO
label index
=
init
,
limit
[,
step
]
Execution logic
The following execution steps apply to all three syntactic forms, except as noted:
1. The loop becomes active, and
index
is set to
init
.
2. The iteration count is determined by the following expression:
MAX( INT (
limit
-
init
+
step
) /
step
, 0 )
step
is optional, with the default value of 1. It may not be 0.
Note that the iteration count is 0 if either of the following conditions is true:
step
(if present) is a positive number and
init
is greater than
limit
.
step
is a negative number and
init
is less than
limit
.
3. If the iteration count is 0, the construct becomes inactive and the normal flow of execution
resumes with the first executable statement following the END DO or terminal statement.
4.
statement-block
executes. (In the case of the old-style syntactic form, both
statement-sequence
and
terminal-statement
execute.)
5. The iteration count is decremented by 1, and
index
is incremented by
step
, or by 1 if
step
is not specified.
6. Go to Step 3.
NOTE To ensure compatibility with older versions of Fortran, you can use the
+onetrip compile-line option to ensure that, when a counter-controlled DO loop
is encountered during program execution, the body of the loop executes at least
once.
Examples
This example uses nested DO loops to sort an array into ascending order:
INTEGER :: scores(100)
DO i = 1, 99
DO j = i+1, 100
IF (scores(i) > scores(j)) THEN
temp = scores(i)
scores(i) = scores(j)
scores(j) = temp