Loops can be written which cycle a fixed number of times.
The syntax is as follows,
DODO-var
=
expr1
![]()
,
expr2
[
,
expr3
]
exec-stmts
![]()
END DO
The loop can be named and the exec-stmts
could
contain EXIT or CYCLE statements, however, a WHILE clause cannot
be used but this
can be simulated with an EXIT statement if desired.
The number of iterations, which is evaluated before execution of the loop begins, is calculated as
MAX(INT((in other words the loop runs fromexpr2
-
expr1
+
expr3
)/
expr3
),0)
If expr3
is absent it is assumed to be 1.
The iteration count is worked out as follows (adapted from the standard, [1]):
The execution cycle is performed as follows (adapted from the standard):
For example,
DO i = 1, 100, 1 ... END DO
is a DO loop that will execute 10 times, it is exactly equivalent to
DO i = 1, 100 ... END DO
More complex examples may involve expressions and loops running from high to low:
DO i1 = 24, k*j, -1 DO i2 = k, k*j, j/k ... END DO END DO
An indexed loop could be achieved using an induction variable and EXIT statement, however, the indexed DO loop is better suited as there is less scope for error.
The DO variable cannot be assigned to within the loop.