02.07.2013 Views

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Execution control<br />

Flow control statements<br />

Example<br />

! find the 50th triangular number<br />

triangular_num = 0<br />

DO 10 i = 1, 50<br />

triangular_num = triangular_num + i<br />

10 CONTINUE<br />

PRINT *, triangular_num<br />

CYCLE statement<br />

The CYCLE statement interrupts execution of the current iteration of a DO loop.<br />

Syntax<br />

CYCLE [ do-construct-name ]<br />

Execution logic<br />

1. The current iteration of the enclosing DO loop terminates. Any statements following the<br />

CYCLE statement do not execute.<br />

2. If do-construct-name is specified, the iteration count for the named DO loop decrements.<br />

If do-construct-name is not specified, the iteration count for the immediately enclosing<br />

DO loop decrements.<br />

3.Iftheiterationcountisnonzero,executionresumesatthestartofthestatementblockin<br />

the named (or enclosing) DO loop. If it is zero, the relevant DO loop becomes inactive.<br />

Example<br />

LOGICAL :: even<br />

INTEGER :: number<br />

loop: DO i = 1, 10<br />

PRINT *, ”Enter an integer: ”<br />

READ *, number<br />

IF (number == 0) THEN<br />

PRINT *, ”Must be nonzero.”<br />

CYCLE loop<br />

END IF<br />

even = (MOD(number, 2) == 0)<br />

IF (even) THEN<br />

PRINT *, ”Even”<br />

ELSE<br />

PRINT *, ”Odd”<br />

END IF<br />

END DO loop<br />

114<br />

Chapter 6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!