11 CONTROL STATEMENTS

Control statements may be used to control the execution sequence.

There are sixteen control statements:

  1. Unconditional GO TO
  2. Computed GO TO
  3. Assigned GO TO
  4. Arithmetic IF
  5. Logical IF
  6. Block IF
  7. ELSE IF
  8. ELSE
  9. END IF
  10. DO
  11. CONTINUE
  12. STOP
  13. PAUSE
  14. END
  15. CALL
  16. RETURN

The CALL and RETURN statements are described in Section 15.

11.1 Unconditional GO TO Statement

The form of an unconditional GO TO statement is:
GO TO s
where s is the statement label of an executable statement that appears in the same program unit as the unconditional GO TO statement.

Execution of an unconditional GO TO statement causes a transfer of control so that the statement identified by the statement label is executed next.

11.2 Computed GO TO Statement

The form of a computed GO TO statement is:
GO TO (s [,s]...)  i

Execution of a computed GO TO statement causes evaluation of the expression i . The evaluation of i is followed by a transfer of control so that the statement identified by the ith statement label in the list of statement labels is executed next, provided that 1 <= i <= n , where n is the number of statement labels in the list of statement labels. If i<1 or i>n , the execution sequence continues as though a CONTINUE statement were executed.

11.3 Assigned GO TO Statement

The form of an assigned GO TO statement is:
GO TO i [ (s [,s]...)]

At the time of execution of an assigned GO TO statement, the variable i must be defined with the value of a statement label of an executable statement that appears in the same program unit. Note that the variable may be defined with a statement label value only by an ASSIGN statement ( 10.3) in the same program unit as the assigned GO TO statement. The execution of the assigned GO TO statement causes a transfer of control so that the statement identified by that statement label is executed next.

If the parenthesized list is present, the statement label assigned to i must be one of the statement labels in the list.

11.4 Arithmetic IF Statement

The form of an arithmetic IF statement is:
IF (e) s  , s  , s
                            1    2    3

Execution of an arithmetic IF statement causes evaluation of the expression e followed by a transfer of control. The statement identified by s , s , or s 1 2 3 is executed next as the value of e is less than zero, equal to zero, or greater than zero, respectively.

11.5 Logical IF Statement

The form of a logical IF statement is:
IF (e) st

Execution of a logical IF statement causes evaluation of 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 continues as though a CONTINUE statement were executed.

Note that the execution of a function reference in the expression e of a logical IF statement is permitted to affect entities in the statement st .

11.6 Block IF Statement

The block IF statement is used with the END IF statement and, optionally, the ELSE IF and ELSE statements to control the execution sequence.

The form of a block IF statement is:

IF (e) THEN
where e is a logical expression.

11.6.1 IF-Level.

The IF-level of a statement s is
n  - n
                           1    2
where n 1 is the number of block IF statements from the beginning of the program unit up to and including s , and n 2 is the number of END IF statements in the program unit up to but not including s .

The IF-level of every statement must be zero or positive. The IF-level of each block IF, ELSE IF, ELSE, and END IF statement must be positive. The IF-level of the END statement of each program unit must be zero.

11.6.2 IF-Block.

An IF-block consists of all of the executable statements that appear following the block IF statement up to, but not including, the next ELSE IF, ELSE, or END IF statement that has the same IF-level as the block IF statement. An IF-block may be empty.

11.6.3 Execution of a Block IF Statement.

Execution of a block 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 IF-block. If the value of e is true and the IF-block is empty, control is transferred to the next END IF statement that has the same IF-level as the block IF statement. 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 block IF statement.

Transfer of control into an IF-block from outside the IF-block is prohibited.

If the execution of the last statement in the IF-block does not result in a transfer of control, control is transferred to the next END IF statement that has the same IF-level as the block IF statement that precedes the IF-block.

11.7 ELSE IF Statement

The form of an ELSE IF statement is:
ELSE IF (e) THEN
where e is a logical expression.

11.7.1 ELSE IF-Block.

An ELSE IF-block consists of all of the executable statements that appear following the ELSE IF statement up to, but not including, the next ELSE IF, ELSE, or END IF statement that has the same IF-level as the ELSE IF statement. An ELSE IF-block may be empty.

11.7.2 Execution of an ELSE IF Statement.

Execution of an 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 true and the ELSE IF-block is empty, control is transferred to the next END IF statement that has the same IF-level as the ELSE IF statement. 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.

Transfer of control into an ELSE IF-block from outside the ELSE IF-block is prohibited. The statement label, if any, of the ELSE IF statement must not be referenced by any statement.

If execution of the last statement in the ELSE IF-block does not result in a transfer of control, control is transferred to the next END IF statement that has the same IF-level as the ELSE IF statement that precedes the ELSE IF-block.

11.8 ELSE Statement

The form of an ELSE statement is:
ELSE

11.8.1 ELSE-Block.

An ELSE-block consists of all of the executable statements that appear following the ELSE statement up to, but not including, the next END IF statement that has the same IF-level as the ELSE statement. An ELSE-block may be empty.

An END IF statement of the same IF-level as the ELSE statement must appear before the appearance of an ELSE IF or ELSE statement of the same IF-level.

11.8.2 Execution of an ELSE Statement.

Execution of an ELSE statement has no effect.

Transfer of control into an ELSE-block from outside the ELSE-block is prohibited. The statement label, if any, of an ELSE statement must not be referenced by any statement.

11.9 END IF Statement

The form of an END IF statement is:
END IF
Execution of an END IF statement has no effect.

For each block IF statement there must be a corresponding END IF statement in the same program unit. A corresponding END IF statement is the next END IF statement that has the same IF-level as the block IF statement.

11.10 DO Statement

A DO statement is used to specify a loop, called a DO-loop.

The form of a DO statement is:

DO s  i = e , e , [,e ]
                               1   2     3

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

11.10.1 Range of a DO-Loop.

The range of a DO-loop consists of all of the executable statements that appear following the DO statement that specifies the DO-loop, up to and including the terminal statement of the DO-loop.

If a DO statement appears within the range of a DO-loop, the range of the DO-loop specified by that DO statement must be contained entirely within the range of the outer DO-loop. More than one DO-loop may have the same terminal statement.

If a DO statement appears within an IF-block, ELSE IF-block, or ELSE-block, the range of that DO-loop must be contained entirely within that IF-block, ELSE IF-block, or ELSE-block, respectively.

If a block IF statement appears within the range of a DO-loop, the corresponding END IF statement must also appear within the range of that DO-loop.

11.10.2 Active and Inactive DO-Loops.

A DO-loop is either active or inactive. Initially inactive, a DO-loop becomes active only when its DO statement is executed.

Once active, the DO-loop becomes inactive only when:

  1. its iteration count is tested ( 11.10.4) and determined to be zero,
  2. a RETURN statement is executed within its range,
  3. control is transferred to a statement that is in the same program unit and is outside the range of the DO-loop, or
  4. any STOP statement in the executable program is executed, or execution is terminated for any other reason ( 12.6).

Execution of a function reference or CALL statement that appears in the range of a DO-loop does not cause the DO-loop to become inactive, except when control is returned by means of an alternate return specifier in a CALL statement to a statement that is not in the range of the DO-loop.

When a DO-loop becomes inactive, the DO-variable of the DO-loop retains its last defined value.

11.10.3 Executing a DO Statement.

The effect of executing a DO statement is to perform the following steps in sequence:
  1. The initial parameter m 1 , the terminal parameter m 2 , and the incrementation parameter m 3 are established by evaluating e 1 , e 2 , and e 3 , respectively, including, if necessary, conversion to the type of the DO-variable according to the rules for arithmetic conversion (Table 4). If e 3 does not appear, m 3 has a value of one. m 3 must not have a value of zero.
  2. The DO-variable becomes defined with the value of the initial parameter m 1 .
  3. The iteration count is established and is the value of the expression

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

11.10.4 Loop Control Processing.

Loop control processing determines if further execution of the range of the DO-loop is required. The iteration count is tested. If it is not zero, execution of the first statement in the range of the DO-loop begins. If the iteration count is zero, the DO-loop becomes inactive. If, as a result, all of the DO-loops sharing the terminal statement of this DO-loop are inactive, normal execution continues with execution of the next executable statement following the terminal statement. However, if some of the DO-loops sharing the terminal statement are active, execution continues with incrementation processing, as described in 11.10.7.

11.10.5 Execution of the Range.

Statements in the range of a DO-loop are executed until the terminal statement is reached. Except by the incrementation described in 11.10.7, the DO-variable of the DO-loop may neither be redefined nor become undefined during execution of the range of the DO-loop.

11.10.6 Terminal Statement Execution.

Execution of the terminal statement occurs as a result of the normal execution sequence or as a result of transfer of control, subject to the restrictions in 11.10.8. Unless execution of the terminal statement results in a transfer of control, execution then continues with incrementation processing, as described in 11.10.7.

11.10.7 Incrementation Processing.

Incrementation processing has the effect of the following steps performed in sequence:
  1. The DO-variable, the iteration count, and the incrementation parameter of the active DO-loop whose DO statement was most recently executed, are selected for processing.
  2. The value of the DO-variable is incremented by the value of the incrementation parameter m 3 .
  3. The iteration count is decremented by one.
  4. Execution continues with loop control processing ( 11.10.4) of the same DO-loop whose iteration count was decremented.

For example:

  1. N=0
  2. DO 100 I=1,10
  3. J=I
  4. DO 100 K=1,5
  5. L=K
  6. 100 N=N+1
  7. 101 CONTINUE

After execution of these statements and at the execution of the CONTINUE statement, I=11, J=10, K=6, L=5, and N=50.

Also consider the following example:

  1. N=0
  2. DO 200=I=1,10
  3. J=I
  4. D0 200 K=5,1
  5. L=K
  6. 200 N=N+1
  7. 201 CONTINUE

After execution of these statements and at the execution of the CONTINUE statement, I=11, J=10, K=5, and N=0. L is not defined by these statements.

11.10.8 Transfer into the Range of a DO-Loop.

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

11.11 CONTINUE Statement

The form of a CONTINUE statement is:
CONTINUE
Execution of a CONTINUE statement has no effect.

If the CONTINUE statement is the terminal statement of a DO-loop, the next statement executed depends on the result of the DO-loop incrementation processing ( 11.10.7).

11.12 STOP Statement

The form of a STOP statement is:
STOP [n]
where n is a string of not more than five digits, or is a character constant.

Execution of a STOP statement causes termination of execution of the executable program. At the time of termination, the digit string or character constant is accessible.

11.13 PAUSE Statement

The form of a PAUSE statement is:
PAUSE [n]
where n is a string of not more than five digits, or is a character constant.

Execution of a PAUSE statement causes a cessation of execution of the executable program. Execution must be resumable. At the time of cessation of execution, the digit string or character constant is accessible. Resumption of execution is not under control of the program. If execution is resumed, the execution sequence continues as though a CONTINUE statement were executed.

11.14 END Statement

The END statement indicates the end of the sequence of statements and comment lines of a program unit ( 3.5). If executed in a function or subroutine subprogram, it has the effect of a RETURN statement ( 15.8). If executed in a main program, it terminates the execution of the executable program.

The form of an END statement is:

END
An END statement is written only in columns 7 through 72 of an initial line. An END statement must not be continued. No other statement in a program unit may have an initial line that appears to be an END statement.

The last line of every program unit must be an END statement.


[Contents] [Previous] [Next] [Home]

 

NDP77
http://www.ndp77.net
webmaster MFA (main)
webmaster@ndp77.net