next up previous contents
Next: Named and Nested Loops Up: Control Flow Previous: Conditional Exit Loops

 

Conditional Cycle Loops

Situations often arise in practice when, for some exceptional reason, it is desirable to terminate a particular pass through a loop and continue immediately with the next repetition or cycle; this can be achieved in Fortran 90 by arranging that a CYCLE statement is executed at an appropriate point in the loop.

For example,

    i = 0
    DO
      i = i + 1
      IF (i >= 50 .AND. i <= 59) CYCLE
      IF (i > 100) EXIT
      PRINT*, "I is", i
    END DO
    PRINT*, "Loop finished. I now equals", i

this will generate

  I is   1
  I is   2
    ....
  I is   49
  I is   60
    ....
  I is   100
  Loop finished. I now equals   101

Here CYCLE forces control to the innermost DO statement (the one that contains the CYCLE statement) and the loop begins a new iteration.

In the example, the statement:

     IF (i >= 50 .AND. i <= 59) CYCLE

if executed, will transfer control to the DO statement. The loop must still contain an EXIT statement in order that it can terminate.

A CYCLE statement which is not within a loop is an error.

Return to corresponding overview page gif


next up previous contents
Next: Named and Nested Loops Up: Control Flow Previous: Conditional Exit Loops

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