24.11.2014 Views

Open Watcom FORTRAN 77 Language Reference

Open Watcom FORTRAN 77 Language Reference

Open Watcom FORTRAN 77 Language Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Language</strong> <strong>Reference</strong><br />

9.4 DO - END DO<br />

DO init-expr,end-value[,inc-value] [: block-label]<br />

statement(s)<br />

END DO<br />

This extension to <strong>FORTRAN</strong> <strong>77</strong> allows the creation of DO-loops without the introduction of statement<br />

numbers. An optional block label may be specified (see the CYCLE, EXIT or QUIT statement for more<br />

information). The END DO statement is used to indicate the end of the range of its corresponding DO<br />

statement. A statement number may not be specified in the corresponding DO statement. Nested DO-loops<br />

of this form require separate END DO statements to terminate the range of the corresponding DO statement.<br />

Since a statement number may appear on the END DO statement, the number may be used to terminate<br />

outer DO-loops. This is not a recommended practice (a CONTINUE statement or a structured DO statement<br />

should be used). A transfer of control from within the DO-loop to a statement number on the END DO<br />

statement is treated in the same manner as if the word CONTINUE had been used instead of END DO.<br />

Some examples follow.<br />

Example:<br />

DO I = 1, 3<br />

DO J = 1, 5<br />

PRINT *, MATRIX( I, J )<br />

END DO<br />

END DO<br />

The above is equivalent to the following example which uses statement numbers.<br />

Example:<br />

DO 10 I = 1, 3<br />

DO 20 J = 1, 5<br />

PRINT *, MATRIX( I, J )<br />

20 CONTINUE<br />

10 CONTINUE<br />

The next example demonstrates the use of a GO TO statement to control execution of all or part of a<br />

DO-loop.<br />

Example:<br />

DO I = 1, 3<br />

DO J = 1, 5<br />

PRINT *, ’INNER LOOP - J=’, J<br />

IF( J .LE. 3 )GO TO 20<br />

PRINT *, ’J > 3’<br />

20 END DO<br />

PRINT *, ’OUTER LOOP - J=’, J<br />

END DO<br />

A result of this example is that the character string J > 3 is printed 6 times (i.e., twice for each iteration<br />

of the outer loop). Of course there is a much better way of coding this algorithm using the IF-END IF<br />

construct. The example is included to illustrate the behaviour of transfers of control to an END DO<br />

statement. The following example is an equivalent algorithm to the one above but the intent is much<br />

clearer.<br />

196 DO - END DO

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

Saved successfully!

Ooh no, something went wrong!