02.07.2013 Views

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

HP Fortran Programmer's 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.

Execution control<br />

Control constructs and statement blocks<br />

DO label index = init, limit [ , step ]<br />

statement-sequence<br />

label terminal-statement<br />

A third form, combining elements of the other two, is also supported:<br />

[construct-name :] DO label index = init, limit [, step]<br />

Execution logic<br />

The following execution steps apply to all three syntactic forms, except as noted:<br />

1. The loop becomes active, and index is set to init.<br />

2. The iteration count is determined by the following expression:<br />

108<br />

MAX( INT ( limit - init + step ) / step, 0 )<br />

step is optional, with the default value of 1. It may not be 0.<br />

Note that the iteration count is 0 if either of the following conditions is true:<br />

step (ifpresent)isapositivenumberandinit is greater than limit.<br />

step is a negative number and init is less than limit.<br />

3. If the iteration count is 0, the construct becomes inactive and the normal flow of execution<br />

resumes with the first executable statement following the END DO or terminal statement.<br />

4. statement-block executes. (In the case of the old-style syntactic form, both<br />

statement-sequence and terminal-statement execute.)<br />

5. The iteration count is decremented by 1, and index is incremented by step, orby1if<br />

step is not specified.<br />

6. Go to Step 3.<br />

NOTE To ensure compatibility with older versions of <strong>Fortran</strong>, you can use the<br />

+onetrip compile-line option to ensure that, when a counter-controlled DO loop<br />

is encountered during program execution, the body of the loop executes at least<br />

once.<br />

Examples<br />

This example uses nested DO loops to sort an array into ascending order:<br />

INTEGER :: scores(100)<br />

DO i = 1, 99<br />

Chapter 6

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

Saved successfully!

Ooh no, something went wrong!