next up previous contents
Next: DO ... WHILE Loops Up: Control Flow Previous: Conditional Cycle Loops

 

Named and Nested Loops

Sometimes it is necessary to jump out of more than the innermost DO loop. To allow this, loops can be given names and then the EXIT statement can be made to refer to a particular loop. An analogous situation also exists for CYCLE,

0|    outa: DO
1|     inna: DO
2|      ...
3|      IF (a.GT.b) EXIT outa  ! jump to line 9
4|      IF (a.EQ.b) CYCLE outa ! jump to line 0
5|      IF (c.GT.d) EXIT inna  ! jump to line 8
6|      IF (c.EQ.a) CYCLE      ! jump to line 1
7|     END DO inna
8|    END DO outa
9|    ...

The (optional) name following the EXIT or CYCLE highlights which loop the statement refers to.

For example,

   IF (a.EQ.b) CYCLE outa

causes a jump to the first DO loop named outa (line 0).

Likewise,

   IF (c.GT.d) EXIT inna

jumps to line 9.

If the name is missing then the directive is applied, as usual, to the next outermost loop so

   IF (c.EQ.a) CYCLE
causes control to jump to line 1.

The scope of a loop name is the same as that of any construct name.

Now try this question gif

Return to corresponding overview page gif


next up previous contents
Next: DO ... WHILE Loops Up: Control Flow Previous: Conditional Cycle Loops

©University of Liverpool, 1997
Wed May 28 20:20:27 BST 1997
Not for commercial use.