• Aucun résultat trouvé

ARITHMETIC IF STATEMENT

Dans le document CRAY® COMPUTER SYSTEMS (Page 194-198)

PROGRAM CONTROL

6.2 OTHER IF STATEMENTS

6.2.2 ARITHMETIC IF STATEMENT

The arithmetic IF statement transfers control to one of three statements, depending on the value of a specified expression.

e Integer, real, or double-precision expression. If the expression's value is less than 0, control transfers to the statement identified by sl: if 0, statement s2:

greater than 0, statement s3.

sl,S2' and s3

Statement labels of executable statements that appear in the same program unit as the arithmetic IF statement. The same statement label can appear more than once in this statement.

Examples:

IF (VTEST) 20,21,20 IF (B**2-4*A*C) 70,80,90

6.3 DO LOOPS

A DO loop consists of a DO statement and a set of statements to be executed repeatedly. The range of a DO loop consists of all executable statements from the first executable statement following the DO statement and ending with the terminal statement of the DO loop.

The number of times a DO loop is to be repeated is the trip count, which is initially determined from expressions in the DO statement, and

decremented for each iteration of the loop. The DO variable is set to an initial value and incremented or decremented by the increment value until its value reaches or exceeds the limit value. -e j on the cft77 command or ON=J in the CFT77 control statement causes at least one execution of any DO loop whose DO statement is reached.

Example:

DO 20 I=1,10

ARR(I) = SQRT(100.*I) 20 CONTINUE

I is the loop's DO variable

This statement is in the loop's range Terminal statement

In the DO statement above, I is the DO variable; the DO variable's initial value is 1; its limit value is 10; and its increment value is the default of 1. The resulting trip count is 10. Notice that the DO variable I is used within the loop.

A DO loop can appear within another DO loop and must be entirely contained within the outer DO loop range. More than one DO loop can have the same

terminal statement, but separate terminal statements improve programming clarity.

A DO loop can appear within a conditional block but must be entirely contained within that block. If a block-IF statement appears within the range of a DO loop, the corresponding ENDIF statement must appear within the same DO loop.

A DO loop is either active or inactive. A DO loop is initially inactive and becomes active only when its DO statement is executed. Control must not transfer into the range of an inactive DO loop. An active DO loop becomes inactive under any of the following conditions.

• Its trip count is tested and determined to be zero.

• A RETURN statement is executed within the DO loop.

• A STOP statement is executed, or program execution is terminated in any other way.

• Control is transferred outside the DO loop within the same program unit.

When a DO loop becomes inactive, the DO variable is unaffected: it retains its last defined value or remains undefined.

I

6.3.1 DO STATEMENT

A DO statement specifies necessary information to control the repeated execution of a set of statements.

DO label [,] dvar

=

init,lim[,incr]

label

dvar

Statement label of the loop's terminal statement, which must be executable

Name of the DO variable; must be a simple variable of type integer, real, double-precision, or pointer. dvar is initially defined with init and is incremented by incr on each iteration of the loop. Within the range of a loop, dvar can be used but cannot be redefined, including by a nested DO statement.

init, lim, and incr

Initial value, limit, and increment of the DO variable; must be integer, real, or double-precision expressions. If

necessary, types are converted to the type of the DO

variable, according to the rules for arithmetic conversion.

incr defaults to 1 and cannot equal O.

init, lim, and incr can be redefined with no effect on loop control processing. -e J ~n the cft77 command or ON=J in the CFT77 control statement causes at least one execution of each DO loop.

The initial trip count is established as the integer portion of the following expression, or as zero if either condition shown in the next paragraph is true. (incr cannot equal 0.)

(lim-init+incr) / incr

The initial trip count must be less than 232 _1 on the CRAY-2 computer system and less than 224_1 on other Cray systems.

Execution of the loop ends when the trip count is zero, which occurs when either of the following conditions is true:

init > lim and incr > 0

init < lim and incr < 0

If either of the above conditions is true initially, the loop is not executed and a message is issued; see example 2 in 6.3.3.

6.3.2 TERMINAL STATEMENT AND CONTINUE STATEMENT

The terminal statement is an executable statement that ends the DO loop.

The statement can be executed in the normal sequence, or control can be transferred to it. The statement must not be a block IF, ELSEIF, ENDIF, arithmetic IF, unconditional GOTO, assigned GOTO, RETURN, STOP, END, or another DO statement. It can be a logical IF statement.

The CONTINUE statement is commonly used as a terminal statement; i t has no effect. As with any terminal statement, the next statement executed

depends only on the result of DO loop processing.

Examples:

1)

2)

3)

DIMENSION ARRAY(16) DO 10,1=1,16

IF(ARRAY(I).NE.O) ARRAY(I)=1.0/ARRAY(I) 10 CONTINUE

The ,above loop replaces nonzero elements of ARRAY with their

reciprocals. The initial trip count is (16-1+1)/1 or 16, the number of elements in the array.

DIMENSION TABLE(50) DO 10 1=1,49

IF(TABLE(I).LE.O) THEN TABLE(I)=-TABLE(I) ELSE

TABLE(I)=-TABLE(I+1) ENDIF

10 CONTINUE

9

The above loop uses an IF block to determine the effect of each iteration.

INTEGER A(10,9) DO 10 1=1,10

DO 9 J=1,9 A(I,J)=I*J CONTINUE

!Nested loop

10 CONTINUE

The above example shows a loop nested within another loop, which is used for a two-dimensional array.

Dans le document CRAY® COMPUTER SYSTEMS (Page 194-198)