Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Fortran Compiler for HP-UX: HP Fortran Programmer's Reference > Chapter 6 Execution control

Flow control statements

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

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:

  • CONTINUE statement

  • CYCLE statement

  • EXIT statement

  • Assigned GO TO statement

  • Computed GO TO statement

  • Unconditional GO TO statement

  • Arithmetic IF statement

  • Logical IF statement

  • PAUSE statement

  • STOP 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.

Syntax

CONTINUE

Execution logic

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

Syntax

CYCLE [ do-construct-name ]

Execution logic

  1. The current iteration of the enclosing DO loop terminates. Any statements following the CYCLE statement do not execute.

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

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

Example

LOGICAL :: 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.

Syntax

EXIT [ do-construct-name ]

Execution logic

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

Example

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

Syntax

GO TO integer-variable [ , ( label-list ) ]

If label-list is present, then the label previously assigned to integer-variable must be in the list.

Execution logic

Control transfers to the executable statement at integer-variable.

Example

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

Syntax

GO TO ( label-list ) [ , ] integer-expression

Execution logic

  1. integer-expression is evaluated.

  2. The resulting integer value (the index) specifies the ordinal position of the label that is selected from label-list.

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

Syntax

GO TO label

Execution logic

Control transfers to the statement at label.

Example

Older, “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.

Syntax

IF ( arithmetic-expression ) label1, label2, label3

Execution logic

  1. arithmetic-expression is evaluated.

  2. If the resulting value is negative, control transfers to the statement at label1.

  3. If the resulting value is 0, control transfers to the statement at label2.

  4. If the resulting value is positive, control transfers to the statement at label3.

Example

As 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

  • Any END statement

  • Any IF statement

Syntax

IF ( logical-expression ) executable-statement

Execution logic

  1. logical-expression is evaluated.

  2. If it evaluates to true, executable-statement executes.

  3. 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.)

Example

LOGICAL :: finished
.
.
.
IF ( finished ) PRINT *, ”Done.”

PAUSE statement

The PAUSE statement causes a temporary break in program execution.

Syntax

PAUSE [ pause-code ]

where pause-code is a character constant or a list of up to 5 digits.

Execution logic

  1. Execution 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'.
    PAUSE pause-code
  2. 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.

Example

PAUSE 999

STOP statement

The STOP statement terminates program execution.

Syntax

STOP [ stop-code ]

where stop-code is a character constant, a named constant, or a list of up to 5 digits.

Execution logic

Program terminates execution. If stop-code is specified, the following is written to standard output:

STOP stop-code

Example

STOP ”Program has stopped executing.”
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.