• Keine Ergebnisse gefunden

PROGRAM CONTROL STATEMENTS

Im Dokument COMPUTER SYSTEMS (Seite 139-163)

Program control statements are used when two or more alternative sequences of statements exist and a decision is required, or when a statement sequence is to be repeated, interrupted, or terminated.

The following statements control an execution sequence.

• Unconditional GO TO

Computed GO TO

Assigned GO TO

Arithmetic IF

Logical IF

Conditional block statements

DO

CONTINUE

• STOP

• PAUSE

• END

• CALL (Described in section 7)

• RETURN (Described in section 7)

UNCONDITIONAL GO TO STATEMENT

The format of an unconditional GO TO statement is

EJ

SR-0009

Part 2 4-1

4

J

where s is the statement label of an executable statement in the same program unit.

The space between GO and TO is optional.

Execution of an unconditional GO TO statement causes a transfer of control to the statement identified by the statement label.

Example:

GO TO 910

COMPUTED GO TO STATEMENT

The format of a computed GO TO statement is GO TO (s[,s] ••• ) [,]e

where

e

is an integer expression, and

s is the statement label of an executable statement that appears in the same program unit as the computed GO TO statement. A given statement label can appear more than once in a computed GO TO statement.

The space between GO and TO is optional.

Execution evaluated statement statement execution executed.

result,

Examples:

of a computed GO TO statement causes the expression e to be for an integer result,

i.

A transfer of control to the identified by the ith statement label in the list of n labels is then executed if

l<i<n.

If

i<l

or

i>n,

the sequence proceeds as though

a

CONTINUE statement were If the evaluation of e for

i

produces a non-integer is converted to integer as if

i=e

had been executed.

GO TO (2,4,8,16)A (The value of A is truncated, if necessary, to produce an integer value.)

GO TO (0031,59,728)IX

SR-0009

Part 2

4-2 J

GO TO (003l,59,728)MSIZE/2 GO TO (6,3,6,6,7,2,7),NBRANCH

ASSIGNED GO TO STATEMENT

The format of an assigned GO TO statement is GO TO i[[,] (a[,a] ••• )]

where i is an integer variable name, and

a is the statement label of an executable statement that appears in the same program unit as the assigned GO TO

statement. A given statement label can appear more than once in this statement.

The space between GO and TO is optional.

At the time of execution of an assigned GO TO statement, the variable i must be defined with the value of a statement label appearing in the same program unit. The variable can be defined with a statement label value by an ASSIGN statement in the same program unit as the assigned GO TO statement. Execution of the assigned GO TO statement causes a transfer of control to the statement identified by that statement label.

CFT does not use the optional statement list.

The ANSI FORTRAN Standard specifies that if the optional list is present, i must have been assigned a statement label from the list.

Examples:

ASSIGN 76 TO LAB

GO TO LAB

ASSIGN 999 TO KFIN

GO TO KFIN (997,997,999)

SR-0009

Part 2

4-3 J

ASSIGN 1 TO JAIL

GO TO JAIL, (1,2,3,4,5)

ARITHMETIC IF STATEMENT

The format of the arithmetic IF statement is

where e is an integer, real, or double-precision expression, and

8 1,8 2, and 83

are 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.

Executing an arithmetic IF statement evaluates the expression e.

Control is transferred to one of the statements identified by 81' 82' or 83 if the value of e is less than zero, equal to zero, or greater than zero, respectively.

Examples:

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

LOGICAL IF STATEMENT

The format of a logical IF statement is

SR-0009

Part 2

4-4 J

I

where e st

is a logical expression, and

is any executable statement other than a DO, END, block IF, ELSE IF, ELSE, END IF, or another logical IF statement.

Executing a logical IF statement evaluates the expression e. If the value of e is true, statement st is executed. If the value of e is false, statement st is not executed and the execution sequence proceeds as if a CONTINUE statement were executed. The execution of a function

I

reference in the expression e may affect entities in the statement st.

Examples:

IF(K) K=.NOT.K

IF (A.EQ.B) GO TO 100

CONDITIONAL BLOCK STATEMENTS

I

Conditional block statements delimit groups of executable statements called blocks. They control the execution sequence of the statements in a block. Fbllowing is a list of the conditional block statements.

• Block IF

• END IF

• ELSE IF

• ELSE

The IF-level of a given statement is the number of block IF statements from the beginning of the program unit to that statement minus the number of END IF statements from the beginning of the program unit up to but not including that statement. The IF-level must always be 0 or positive: the IF-level of the END statement of each program unit must always be zero.

IF-BLOCK

An IF-block is a group of executable statements preceded by a block IF statement and followed by another conditional block statement (END IF, ELSE IF, or ELSE) of the same IF-level. An IF-block can be empty.

SR-0009

Part 2

4-5 J

BLOCK I F STATEMENT

The format of the block IF statement is IF (e) THEN

where e is a logical expression.

Executing the block IF statement evaluates the expression e. If the value of e is true, normal execution sequence continues with the first statement in the IF-block. If the value of e is false, control is transferred to the next END IF, ELSE IF, or ELSE statement of the same IF-level. The block IF statement must always have a corresponding END IF statement of the same IF-level.

If a block IF statement appears within the range of a no-loop, the entire block must appear within the range.

I Control cannot be transferred into an IF-block from outside the IF-block.

END IF STATEMENT

The format of the END IF statement follows.

I

END IF

I

The END IF statement indicates the end of an IF-level and must always have a corresponding block IF statement of the same IF-level.

I The space between END and IF is optional.

ELSE IF-BLOCK

An ELSE IF-block is a group of executable statements with an ELSE IF statement preceding the group and a conditional block statement (END IF, ELSE IF, or ELSE) of the same IF-level following the group. An ELSE IF-block can be empty. The IF-level of the ELSE IF-block must be greater than or equal to 1.

SR-0009

Part 2

4-6 J

I

ELSE IF STATEMENT

The format of the ELSE IF statement is ELSE IF (e) THEN

where e is a logical expression.

The ELSE IF statement is executed if none of the preceding blocks have been executed. Execution of the ELSE IF statement causes evaluation of the expression e. If the value of e is true, normal execution sequence continues with the first statement of the ELSE IF-block. If the value of

e

is false, control is transferred to the next ELSE IF, ELSE, or END IF statement that has the same IF-level as the ELSE IF statement. Statement labels on ELSE IF statements are ignored.

The space between ELSE and IF is optional.

Control cannot be transferred into an ELSE IF-block from outside the ELSE IF-block.

ELSE-BLOCK

An ELSE-block is a group of executable statements with an ELSE statement preceding the group and an END IF statement of the same IF-level

following the group. No other conditional block statement at the same level can appear after the ELSE statement or before the END IF

statement. ELSE-blocks can be empty. The IF-level of the ELSE-block must be greater than or equal to 1. Statements in the ELSE-block are executed if none of the preceding blocks were executed.

ELSE STATEMENT

The format of the ELSE statement follows.

The ELSE statement introduces an ELSE-block. Statement labels on ELSE statements are ignored.

SR-0009

Part 2

4-7 J

CONDITIONAL BLOCK STATEMENT EXECUTION

A group of blocks must begin with a block IF statement and end with an END IF statement. No more than one block is executed within each level of blocks. This execution depends on the sequential evaluation of the conditional block statements.

The ELSE IF and ELSE statements are not required to accompany block IF statements. A block begins with a block IF, an ELSE IF, or an ELSE statement and continues until an END IF or the beginning of the next block is encountered. Control must not be transferred to a location within a block from outside that block.

Each statement in a block has an IF-level number assigned to it. (See figure 4-1 for an illustration of blocks and levels.) The first block IF encountered is assigned IF-level 1. All following statements retain that

IF-level number until either another block IF or an END IF statement is encountered.

If another block IF is encountered, the IF-level number of that statement is incremented by one. The following statements reflect that IF-level number until another block IF or END IF statement is encountered.

If an END IF statement is encountered, the IF-level is decremented by 1 and all following statements retain that IF-level number until a block IF or END IF is encountered.

DO STATEMENT

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

DO-loop

consists of a DO

statement, the set of statements to be executed repeatedly, including a labeled terminal statement.

The format of a DO statement is

where 8

i

SR-0009

is the statement label of an executable statement, called the terminal statement;

is the name of an integer, real, or double-precision variable, called the DO variable; and

Part 2

4-8 J

IF (X.GT.O) THEN~ ______________________________ ~

x

= SQRT(X)

:~:_'J_) ____

2_._0 ____________________

-J1

!LSE

IF-block

ELSE~ _________________________ ~

CALL_A_B_OR_T __________________

~I

!LSE-blOCk ' - - - END IF

Figure 4-1. IF-levels and blocks

Part 2

4-9 J

I

el' e2' and e3

are integer, real, or double-precision expressions specifying the initial value, limit value, and

increment value, respectively, of the DO variable. If e3 is omitted, a value of 1 is assumed.

TERMINAL STATEMENT

The terminal statement is an executable statement ending the DO-loop.

The terminal statement of a DO-loop must not be an unconditional GO TO, assigned GO TO, arithmetic IF, conditional block, RETURN, STOP, END, or another DO statement. If the terminal statement of a DO-loop is a logical IF statement, it can contain any executable statement except a DO, conditional block, END, or another logical IF statement.

DO VARIABLE

The DO variable is an index which, during the execution of the DO-loop, is set to an initial value and incremented (or decremented) until its value reaches or exceeds the limit value. The DO variable can be used in subscript or nonsubscript calculations within the DO-loop. The absolute value of an integer DO variable must not exceed 223 -1.

The ANSI FORTRAN Standard does not limit the value of a DO variable.

RANGE OF A DO-LOOP

The range of a DO-loop consists of all executable statements, beginning with the first executable statement following the DO statement and ending with the terminal statement of the DO-loop.

A DO-loop can appear within a DO-loop and must be entirely contained within the outer DO-loop range. More than one DO-loop can have the same terminal statement. However, no more than 15 DO-loops can terminate on the same terminal statement.

The ANSI FORTRAN Standard does not specify a limit to the number of DO-loops that can terminate on the same terminal statement.

SR-0009

Part 2

4-10 J

A DO-loop can appear within a conditional block but it 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 also appear within the range of that DO-loop.

The following example contains constructs classified by the ANSI FORTRAN standard as illegally jumping into the range of a DO-loop. This type of construct should be avoided.

Example:

DO 10 1=1,20 A(I)=O.

IF(I.GT.lO)GO TO 10 DO 10 J=l,lOO

B(J,I)=O.

10 CONTINUE

ACTIVE AND INACTIVE DO-LOOPS

A DO-loop is either active or inactive. A DO-loop is initially inactive and becomes active only when its DO statement is executed.

An active DO-loop becomes inactive under any of the following conditions.

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

• A RETURN or STOP statement is executed in the same program unit •.

• It is in the range of another DO-loop that becomes inactive.

• It is in the range of another DO-loop having an executed DO statement.

When a DO-loop becomes inactive, the DO variable retains its last defined value unless it became undefined due to earlier action.

EXECUTING A DO STATEMENT

Executing a DO statement initiates the following sequence of steps.

1. The initial, limit, and increment value expressions

(el' e2'

and

e3)

are evaluated, producing the

initial parameter ml'

the

terminal parametep m2,

and the

incrementation parameter m3•

If necessary, types are converted to the type of the DO variable,

SR-0009

Part 2

4-11 J

I

according to the rules for arithmetic conversion. If e3 has been omitted from the DO statement, m3 is assigned a value of 1. m3 can be positive or negative but must not be O. If the DO variable

is of type integer, then ml' m2' m3 and (m2-ml+m3) must all be less than 223_1 (8,388,607) in absolute value or undetected bad code may be produced.

The ANSI PORT.RAN Standard does not limit the values of m or of the quanti ty (m2-m1 +m3 ) •

2. The DO variable i becomes defined with the value of the initial parameter mI.

3. The

itepation aount

is established as an integer value equal to the integer portion of the expression

or as 0 in the event that ml >m2, and m3>0 or ml<m2' and m3<0.

m

3=0 is not explicitly detected, but results in a floating-point error when the iteration count is evaluated at run time.

The iteration count must be less than 223 (8,388,608). Once the

iteration count is established, the DO variable and entities named in the initial, limit, and incrementation value expressions el' e2' and e3

can be redefined with no effect on loop control processing. The DO variable cannot be redefined by a subsequent nested DO statement.

At completion of DO statement execution, loop control processing begins.

The ANSI PORT.RAN Standard does not permit the DO variable to be redefined during execution of the DO-loop range.

The ANSI PORT.RAN Standard does not specify a maximum iteration count.

SR-0009

Part 2

4-12 J-03

I

LOOP CONTROL PROCESSING

Loop control processing determines if execution in the range of the DO-loop is required. If the iteration count is not 0, control transfers to the first statement in the range of the DO-loop. If the iteration count is 0, the DO-loop becomes inactive. However, specifying ON=J in

the CFT control statement overrides this feature and causes execution of all DO-loops at least once. If, as a result, all DO-loops sharing the terminal statement of this DO-loop are inactive, control is transferred to the first executable statement after the terminal statement. However, if any DO-loops sharing the terminal statement are active, execution resumes with incrementation processing, described below.

EXECUTION OF THE RANGE

Statements in the range of a DO-loop are executed until the terminal statement is reached.

TERMINAL STATEMENT EXECUTION

Execution of the terminal statement occurs during a normal execution sequence or through transfer of control. If execution of the terminal statement does not cause a transfer of control, execution continues with incrementation processing, as described below.

INCREMENTATION PROCESSING

Incrementation processing has the effect of performing the following steps in sequence.

1. The value of the DO variable is incremented by the value of m3.

2. The iteration count is decremented by 1.

3. Execution continues with loop control processing of the same DO-loop whose iteration count was decremented.

A DO variable can increase or decrease in value during incrementation processing.

The value of the DO variable at termination is not defined if the DO variable was redefined in the range of the DO loop.

SR-0009

Part 2

4-13 J

TRANSFER INTO THE RANGE OF A DO-LOOP

Control must not transfer into the range of an inactive DO-loop.

Examples:

PARAMETER (N=SO) DIMENSION TABLE (N)

DO 2 I=l,N

IF(TABLE(I»2,2,1 1 TABLE (I) =-TABLE(I) 2 TABLE(I)=-TABLE(I+l)

100 M=O DO 100 J=I DO 100 L=K M=M+l

PARAMETER (I=2,J=200)

DIMENSION GRID(I,J), PGRID(I,J) DO 22 L=J,l,-l

PGRID(K,L) = GRID(K,L) IF(PGRID(K,L»21,22,22 21 PGRID(K,L) = -PGRID(K,L) 22 GRID(K,L) = 0

1=1,10 K=l,S

In the last example, 1=11, J=lO, K=6, L=S, and M=SO after the last statement is executed for the last time.

CONTINUE STATEMENT

The format of a CONTINUE statement follows.

I

CONTINUE

I

Execution of a CONTINUE statement has no effect.

A CONTINUE statement is commonly used as the terminal statement of a DO-loop. As with any statement so used, the next statement executed depends on the result of DO-loop incrementation processing. This action

is the result of DO-loop processing and not of CONTINUE statement execution.

SR-0009

Part 2

4-14 J

I

Examples:

DIMENSION ARRAY6(16) 00 22,1=16,1,-1

IF (ARRAY6 (I) .NE.O) ARRAY6(I)=1.O/ARRAY6(I) 22 CONTINUE

STOP STATEMENT

The format of a STOP statement is

where

id

is an unsigned integer constant of up to eight digits, a character constant of up to eight characters, or the name of a character variable, array element, or

function containing (or providing) eight characters.

The ANSI FORTRAN Standard limits noncharacter

id

to five digits, sets no limit on the length of character constants, and does not permit

id

I to be the name of a variable, an array element, or a function.

A STOP statement terminates execution of a main program, subroutine subprogram, or function subprogram.

Specification or nonspecification of

id

has no effect on the executable program. The characters specified by

id

appear in a logfile message to identify the STOP statement encountered during program execution.

PAUSE STATEMENT

The format of a PAUSE statement is PAUSE

rid]

SR-0009

Part 2

4-15 J

I

where

id

is an unsigned integer constant of up to eight digits, a character constant of up to eight characters

enclosed in parentheses, or the name of a character variable, array element, or function containing or providing eight characters.

The ANSI FORTRAN Standard limits noncharacter

id

to five digits, sets no limit on the length of character constants, and does not permit

id

• to be the name of a variable, an array element, or a function.

A PAUSE statement suspends or terminates a main program, subroutine

subprogram, or function subprogram. An installation parameter determines whether the execution can be resumed or is unconditionally terminated.

Specification or nonspecification of

id

has no effect on the executable program. The characters specified by

id

appear in a logfile message to identify the PAUSE statement encountered during program execution.

The ANSI FORTRAN Standard does not provide for the option of resuming or terminating execution.

END STATEMENT

The format of an END statement follows.

An END statement is required at the physical end of the sequence of statements and lines of every program unit. When executed in a

subprogram, it has the effect of a RETURN statement. When executed in a main program, it has the effect of a STOP statement.

No other statement in a program unit can be expressed with an initial line containing an END statement. Embedded comments can be included on an END statement when preceded by an exclamation point.

No other statement in a program unit can be expressed with an initial line containing an END statement. Embedded comments can be included on an END statement when preceded by an exclamation point.

Im Dokument COMPUTER SYSTEMS (Seite 139-163)