LF Fortran 95

Computed GOTO Statement (obsolescent)

Description

The computed GOTO statement causes transfer of control to one of a list of labeled statements.

Syntax

GO TO ( labels ) [,] scalar-int-expr
Where:

labels is a comma-separated list of labels.

scalar-int-expr is a scalar INTEGER expression.

Remarks

Execution of a computed GOTO statement causes evaluation of scalar-int-expr. If this value is i such that 1 ≤ in, where n is the number of labels in labels, a transfer of control occurs so that the next statement executed is the one identified by the ith label in labels. If i is less than 1 or greater than n, the execution sequence continues as though a CONTINUE statement were executed.

Each label in labels must be the label of a branch target statement in the current scoping unit.

The computed GOTO statement has been identified as a major contributor to a logic-snarled condition known as "spaghetti code", which makes a program difficult to read and debug. The computed GOTO statement is best replaced by the CASE Construct although the IF Construct could be used as well. Although the computed GOTO statement is obsolescent and should never be used when writing new code, it is fully supported.

Example

integer :: i=1 40 write(*,*) " computed goto construct" goto (20,30,40) i write(*,*) " transfer here if no match" goto 10 30 write(*,*) " if i=2 control transfers here" 20 write(*,*) " if i=1 control transfers here" 10 write(*,*) " equivalent case construct" select case (i) case(1) write(*,*) " if i=1 control transfers here" case(2) write(*,*) " if i=2 control transfers here" case(3) write(*,*) " if i=3 control transfers here" case default write(*,*) " transfer here if no match" end select

See Also

CASE Construct

IF Construct