 |
» |
|
|
 |
Flow
control statements alter the normal flow of program execution or
the execution logic of a control construct. For example, the GO TO statement can be used to transfer control to another statement
within a program unit, and the EXIT statement can terminate execution of a DO construct. This section describes the operations performed by the following
flow control statements: Unconditional GO TO statement
For additional information about these statements, see Chapter
10, “HP Fortran Statements.” CONTINUE statement |  |
The CONTINUE statement has no effect on program execution.
It is generally used to mark a place for a statement label, especially
when it occurs as the terminal statement of a FORTRAN 77-style DO loop. SyntaxExecution logicNo action occurs. Example! find the 50th triangular number triangular_num = 0 DO 10 i = 1, 50 triangular_num = triangular_num + i 10 CONTINUE PRINT *, triangular_num |
CYCLE statement |  |
The CYCLE statement interrupts execution of the current
iteration of a DO loop. SyntaxCYCLE [ do-construct-name ] |
Execution logicThe current iteration of the enclosing DO loop terminates. Any statements following the CYCLE statement do not execute. If do-construct-name is specified, the iteration count for the named DO loop decrements. If do-construct-name is not specified, the iteration count for the immediately
enclosing DO loop decrements. If the iteration count is nonzero, execution resumes
at the start of the statement block in the named (or enclosing) DO loop. If it is zero, the relevant DO loop becomes inactive.
ExampleLOGICAL :: even INTEGER :: number loop: DO i = 1, 10 PRINT *, ”Enter an integer: ” READ *, number IF (number == 0) THEN PRINT *, ”Must be nonzero.” CYCLE loop END IF even = (MOD(number, 2) == 0) IF (even) THEN PRINT *, ”Even” ELSE PRINT *, ”Odd” END IF END DO loop |
EXIT statement |  |
The EXIT statement terminates a DO loop. If it specifies the name of a DO loop within a nest of DO loops, the EXIT statement terminates all loops by which it is
enclosed, up to and including the named DO loop. SyntaxEXIT [ do-construct-name ] |
Execution logicIf do-construct-name is specified, execution terminates for all DO loops that are within range, up to and including
the DO loop with that name. If no name is specified,
execution terminates for the immediately enclosing DO loop. ExampleDO PRINT *, ”Enter a nonzero integer: ” READ *, number IF (number == 0) THEN PRINT *, ”Bye” EXIT END IF even_odd = MOD(number, 2) IF (even_odd == 0) THEN PRINT *, ”Even” ELSE PRINT *, ”Odd” END IF END DO |
Assigned GO TO statement |  |
The assigned GO TO statement transfers control to the statement whose
statement label was assigned to an integer variable by an ASSIGN statement. SyntaxGO TO integer-variable [ , ( label-list ) ] |
If label-list is present, then the label previously assigned to integer-variable must be in the list. Execution logicControl transfers to the executable statement at integer-variable. ExampleINTEGER int_label . . . ASSIGN 20 TO int_label . . . GOTO int_label . . . 20 ... |
Computed GO TO statement |  |
The computed GO TO statement transfers control to one of several
labeled statements, as determined by the value of an arithmetic
expression. SyntaxGO TO ( label-list ) [ , ] integer-expression |
Execution logicinteger-expression is evaluated. The resulting integer value (the index) specifies
the ordinal position of the label that is selected from label-list. Control transfers to the executable statement with
the selected label. If the value of the index is less than 1 or
greater than the number of labels in label-list, the computed GO TO statement has no effect, and control passes to
the next executable statement in the program.
Example DO PRINT *, ”Enter a number 1-3: ” READ *, k GO TO (20, 30, 40) k PRINT *, ”Number out of range.” EXIT 20 i = 20 GO TO 100 30 i = 30 GO TO 100 40 i = 40 100 print *, i END DO |
Unconditional GO TO statement |  |
The unconditional GO TO statement transfers control to the statement with
the specified label. SyntaxExecution logicControl transfers to the statement at label. ExampleOlder, “dusty-deck” Fortran programs often
combine the GO TO statement with the logical IF statement to form a kind of leap-frog logic, as
in the following: IF ( num1 /= num2) GO TO 10 PRINT *, ”num1 and num2 are equal.” GO TO 30 10 IF ( num1 > num2 ) GO TO 20 PRINT *, ”num1 is smaller than num2.” GO TO 30 20 PRINT *, ”num1 is greater than num2.” 30 CONTINUE |
Arithmetic IF statement |  |
The arithmetic IF transfers control to one of three labeled statements,
as determined by the value of an arithmetic expression. SyntaxIF ( arithmetic-expression ) label1, label2, label3 |
Execution logicarithmetic-expression is evaluated. If the resulting value is negative, control transfers
to the statement at label1. If the resulting value is 0, control transfers to
the statement at label2. If the resulting value is positive, control transfers
to the statement at label3.
ExampleAs shown in this example, two or more labels in the label
list can be the same. i = MOD(total, 3) + 1 IF ( i ) 10, 20, 10 |
Logical IF statement |  |
The logical IF statement executes a single statement, conditional
upon the value of a logical expression. The statement it executes
must not be: A statement used to begin a construct
SyntaxIF ( logical-expression ) executable-statement |
Execution logiclogical-expression is evaluated. If it evaluates to true, executable-statement executes. The normal flow of execution resumes with the first
executable statement following the IF statement. (If executable-statement is an unconditional GO TO statement, control resumes with the statement
specified by the GO TO statement.)
ExampleLOGICAL :: finished . . . IF ( finished ) PRINT *, ”Done.” |
PAUSE statement |  |
The PAUSE statement causes a temporary break in program
execution. Syntaxwhere pause-code is a character constant or a list of up to 5 digits. Execution logicExecution of the program is suspended, and the following message
is written to standard output: To resume execution, type 'go'. |
If pause-code is specified, the following message is written: To resume execution, type 'go'. |
The normal flow of execution resumes after the user
types the word go followed by RETURN. If the user enters anything
other than go, program execution terminates.
If the standard input device is other than a terminal, the
message is: To resume execution, execute a kill -15 pid command. |
pid is the unique process identification number of the
suspended program. The kill command can be issued at any terminal at which
the user is logged in. ExampleSTOP statement |  |
The STOP statement terminates program execution. Syntaxwhere stop-code is a character constant, a named constant, or a list
of up to 5 digits. Execution logicProgram terminates
execution. If stop-code is specified, the following is written to standard output: ExampleSTOP ”Program has stopped executing.” |
|