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 />

Transfer of control into the range of a DO-loop from outside the range is not permitted.<br />

A DO-loop may be executed 0 or more times. The following sequence occurs when a DO statement is<br />

encountered.<br />

(i)<br />

An initial value, m1, is calculated by evaluating expression e1. A terminal value, m2, is<br />

calculated by evaluating expression e2. An incrementation value, m3, is calculated by<br />

evaluating expression e3 if it is present; otherwise m3 has the value one. If e3 is specified,<br />

m3 must not be zero. The type of m1, m2, and m3 is determined from the DO-variable<br />

and any conversions of type are done as required.<br />

(ii) The DO-variable is defined with the initial value m1.<br />

(iii)<br />

The iteration count (i.e., the maximum number of times that the DO-loop will be executed)<br />

is calculated as follows:<br />

MAX( INT((m2 - m1 + m3)/m3), 0 )<br />

The iteration count will be zero whenever:<br />

m1 > m2 and m3 > 0, or<br />

m1 < m2 and m3 < 0.<br />

The number of times that the DO-loop is executed may be reduced if control is transferred<br />

outside the range of the DO-loop, or if a RETURN or STOP statement is executed.<br />

The steps involved in each iteration of the DO-loop are as follows:<br />

(i)<br />

(ii)<br />

(iii)<br />

(iv)<br />

Check the iteration count. If it is not zero then start execution of the first executable<br />

statement of the DO-loop. If the count is zero then iteration of the DO-loop is complete.<br />

Execute statements until the terminal statement is encountered. During this time, the<br />

DO-variable may not be redefined.<br />

Execute the terminal statement. Unless execution of the terminal statement causes a<br />

transfer of control, proceed with the next step which is "incrementation" processing.<br />

The DO-variable is incremented by the value m3. The iteration count is decremented by<br />

one. Go back to step (i).<br />

Example:<br />

DO 10 I = -5, 5<br />

PRINT *, I, I*I<br />

10 CONTINUE<br />

In this example, the initial value is -5, the terminal value is 5, and the incrementation value is 1 (the<br />

default). The DO-variable is I. The DO-loop is executed<br />

MAX( INT((5 - (-5) + 1)/1), 0 )<br />

or 11 times. The successive values of I, inside the range of the DO-loop, are -5, -4, -3, ..., 0, 1, ..., 4, 5.<br />

When the DO-loop is terminated, the value of I will be 6. It should be noted that when a DO-loop variable<br />

is of type real, the iteration count may be one less than expected. Because of rounding errors, the value of<br />

m2 - m1 + m3 may be slightly less than the exact value and when the INT function is applied, the<br />

resulting iteration count is one less than expected.<br />

46 DO Statement

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

Saved successfully!

Ooh no, something went wrong!