EXIT statements specify that
the remaining statements in the current iteration of a
particular active (enclosing)
DO loop are to be skipped.
CYCLE specifies that these statements are skipped,
END DO statement that marks the end of the
DO loop be executed—that is, the next iteration,
if any, is to be started.
If the statement marking the end of the
DO loop is
END DO—in other words, if the loop is not
CYCLE statement does not
execute that statement, but does start the next iteration (if any).
EXIT specifies that the loop specified by the
DO construct is terminated.
DO loop affected by
is the innermost enclosing
DO loop when the following
forms are used:
Otherwise, the following forms specify the construct name
of the pertinent
CYCLE construct-name EXIT construct-name
EXIT can be viewed as glorified
However, they cannot be easily thought of as
GO TO statements
in obscure cases involving FORTRAN 77 loops.
DO 10 I = 1, 5 DO 10 J = 1, 5 IF (J .EQ. 5) EXIT DO 10 K = 1, 5 IF (K .EQ. 3) CYCLE 10 PRINT *, 'I=', I, ' J=', J, ' K=', K 20 CONTINUE
In particular, neither the
above are equivalent to a
GO TO statement to either label
`10' or `20'.
To understand the effect of
EXIT in the
above fragment, it is helpful to first translate it to its equivalent
using only block
DO I = 1, 5 DO J = 1, 5 IF (J .EQ. 5) EXIT DO K = 1, 5 IF (K .EQ. 3) CYCLE 10 PRINT *, 'I=', I, ' J=', J, ' K=', K END DO END DO END DO 20 CONTINUE
Adding new labels allows translation of
GO TO so they may be more easily understood by programmers
accustomed to FORTRAN coding:
DO I = 1, 5 DO J = 1, 5 IF (J .EQ. 5) GOTO 18 DO K = 1, 5 IF (K .EQ. 3) GO TO 12 10 PRINT *, 'I=', I, ' J=', J, ' K=', K 12 END DO END DO 18 END DO 20 CONTINUE
CYCLE statement in the innermost loop skips over
EXIT statement in the middle loop ends that
loop but not the outermost loop.