• Aucun résultat trouvé

DO Statem ent

Dans le document 5/7 (Page 27-30)

These statements are used to control the repetitive execution of a group of statements. The number of repetitions depends on the value of a variable. The DO statement may be written

DO k v

=

e l' e 2' e 3' or DO k v =-e

1, e 2 where

k is a statement label not defined before the DO statement.

v is a nonsubscripted integer variable.

e l' e2' and e3 are integer constants greater than zero or unsigned nonsubscripted integer variables whose value is positive.

In the second form, e3 and the preceding comma are omitted; in this case the value 1 is assumed for e3.

RETURN/DO Statement 21

A DO statement indicates that the block of statements following it are to be executed repetitively. Such a block is called a DO loop, and all statements within it, except for the opening DO statement, constitute the range of the DO statement. The last statement in a DO loop is the terminus and bears the statement label k.

The execution of a DO loop proceeds in the following manner:

1. The variable v is assigned the value of e1.

2. The range of statements is executed for one iteration.

3. After each iteration, the value of v is incremented by the value of e 3. If e

3 is not present, the value 1 is used.

4. The value of v is then compared with the terminal value (e 2).

5. If v is greater than e2' control is passed to the statement following the terminus (i. e., to the statement follow-ing the one whose label is k). Otherwise, the process is repeated from step 2.

6. The actua I number of iterations defined by the DO statement is given by

where the brackets represent the largest integral value not exceeding the value of the expression.

The range of a DO loop wIll always be executed at least once, even if the conditions for termination are met initial-ly. For this reason, it is recommended that initially satisfied DO loops should not be used, especially since other FORTRAN systems may interpret this situation differently.

The terminal statement of a DO range (i.e., the statement whose label is k) may be any executable statement other than one of the following:

DO statement GO TO statement Arithmetic IF statement

RETURN statement STOP statement PAUSE statement Logical IF statements are specifically allowed as terminal statements of a DO range.

Example:

22 D054I=1,15

25 SUM = SUM + QO)

IF (SUM. L T. 0.0) SUM = 0.0 SIGMA = SUM + R(I)

IF (SIGMA - H

**

3 IT) 54,54, 12

54 CONTINUE

12 L = YO)

In the example that begins with statement 22, the range of statements 25 through 54 wi II be executed 15 times, unless the arithmetic IF statement causes a transfer to statement 12. If ail 15 iterations are completed, control is passed to statement 12 at the end of the fifteenth iteration.

If the range of a DO loop terminates on a statement with

's'

in column 1 (in-line coding), note that the code gen-erated for the end of a DO loop immediately follows that statement.

Example:

DO 10 I = 1, 10 X =X + I S10 LW,3 X

<

end DO loop code>

S STW,3 Z

It is not a recommended practice to put a DO close on an in-I ine coded statement. This is because the compi ler can not diagnose incorrect terminal statements of a DO range, such as

22 DO Statement

S10

DO 10 I = 1, 10

x=y

B 20S

20 Y =X

The va lue of the variable v appearing in a DO statement depends on the number of iterations canpleted. The va lue of v during anyone iteration is

e 1 = (i - 1)

*

e3

where i is the number of the current iteration, and el and e3 have the meanings discussed previously. If a transfer is made out of the range of a DO before all iterations have been completed, the value of v will be that of the itera-tion during which the transfer occurred. However, should the entire number of iteraitera-tions be executed, the value of v is

e1 + m

*

e3

where m is the tota I number of iterations specified by the DO statement.

Thus, in the example beginning with statement 22, if all iterations are completed, statement 12 wi II be equivalent to 12 L =Y(16)

However, if the arithmetic IF statement causes a transfer to statement 12 during the eighth iteration, the statement will mean

12 L = Y(8)

The value of the indexing parameters (v,el,e2,e3) cannot be modified within the range of the DO, nor can they be modified by a subprogram called within the range of the DO.

A transfer out of the range of a DO loop is permissible at any time; however, a transfer into the range of a DO may only occur if there has been a prior transfer out of the DO range (assuming that none of the indexing parameters (v, e

1, e 2, e

3) are changed outside the range of the DO). For example:

DO 25 H = K, Y, 1

GO TO 8605

24 A

=

H /8

25 JGU = Y(H)

**

3

8605 R = SIN(G(H)) + JSU

8606 GO TO 24

is permissible; in fact, the statements 8605 through 8606 are considered part of the DO range. The sequence GO TO 11

DO 32 J= 2,36,2 11 R(J) = 47. E-7

*

T(J) 32 T(J) = Q

is not valid because no transfer could possibly occur out of the DO range.

A DO loop may include another DO loop. Do loops may be nested; however, they cannot be overlapped. In a nest of DO loops, the same statement may be used as the terminal statement for any number of DO ranges; however, transfers to this statement can be made only from the innermost DO loop. There is no limit to the number of DO

DO Statement 23

ranges that can be nested. Only if a transfer is made out of the range of the innermost DO loop can a return transfer into the range of nested DO loops be made. In this case, the return transfer must be to the innermost DO loop.

Examples:

Legal III egal

DO 1000 I = 1, II DO 200 W = 1 WW

,

DO 100 J = 1, JJ

DO 20 OX = 1, X X

-~o

10

K~ 1~

DO 20 Y = 1, YY

10 CONTINUE

200 CONT INUE

ZZ

--.J

DO 100 L = 1, LL 201 DO 20

o

Z = 1,

~OlM~l'MMJ

002 U= 1, U U

-A=B 2000 Q = R

100 CONTINUE 20 CONT INUE

1000 THIS = DO END 2 IT=W RONG

The termi nal statement of a range may not physi call y precede the DO statement, as is shown in the case of state-ments 200 and 201 in the iI!ega! example above.

Dans le document 5/7 (Page 27-30)

Documents relatifs