8.7. Branching

Branching is a transfer of control from the current statement to another statement or construct in the program unit. A branch alters the execution sequence. This means that the statement or construct immediately following the branch is usually not executed. Instead, some other statement or construct is executed, and the execution sequence proceeds from that point. The terms branch statement and branch target statement are used to distinguish between the transfer statement and the statement to which the transfer is made.

An example of branching is provided by the GO TO statement. It is used to transfer control to a statement in the execution sequence that is usually not the next statement in the program, although this is not prohibited.

The statements that can be branch target statements are those classified as action statements, ASSOCIATE statements, DO statements, IF-THEN statements, SELECT CASE statements, WHERE statements, and a few additional statements in limited situations.

The additional statements that can be branch targets in limited contexts are as follows:

The standard does not permit a branch to a statement within a block from outside the block. The Cray Fortran Compiler, however, permits these branches; such branches are diagnosed as being unsafe.

Note: The Fortran standard does not permit branches into executable blocks.

8.7.1. Use of Labels in Branching

A statement label is a means of identifying the branch target statement. Any statement in a Fortran program can have a label. However, if a branch statement refers to a statement label, some statement in the program unit must have that label, and the statement label must be on an allowed branch target statement.

As described in Section 3.2.5, a label is a string of from one to five decimal digits; leading zeros are not significant. Note that labels can be used in both free and fixed source forms.

8.7.2. GO TO Statement

The GO TO statement is an unconditional branching statement that alters the execution sequence.

8.7.2.1. Form of the GO TO Statement

The GO TO statement is defined as follows:

Table 8-12.

 

goto_stmt

is

GO TO label

The label must be a branch target statement in the same scoping unit as the GO TO statement (that is, in the same program unit, excluding labels on statements in internal procedures, derived-type definitions, and interface blocks).

8.7.2.2. Execution of the GO TO Statement

When the GO TO statement is executed, the next statement that is executed is the branch target statement identified with the label specified. Execution proceeds from that point.

GO TO 200   ! This is an unconditional branch and
            ! always goes to 200.
            !
X = 1.0     ! Because this statement follows a GO
            ! TO statement and is unlabeled, it is
            ! not reachable.
GO TO 10
GO TO 010   ! 10 and 010 are the same label.  

8.7.3. Computed GO TO Statement

The computed GO TO statement transfers to one of a set of branch target statements based on the value of an integer expression, selecting the branch target from a list of labels. The CASE construct provides a similar functionality in a more structured form.

Note: The Fortran standard has declared the computed GO TO statement to be obsolescent.

The computed GO TO statement is defined as follows:

Table 8-13.

 

computed_goto_stmt

is

GO TO (label_list) [ , ]  scalar_int_expr

If there are n labels in the list and the expression has one of the values from 1 to n, the value identifies a statement label in the list: the first, second, ... , or nth label. A branch to the statement with that label is executed.

If the value of the expression is less than 1 or greater than n, no branching occurs and execution continues with the next executable statement or construct following the computed GO TO statement.

Each label in the list must be the label of a branch target statement in the same scoping unit as the computed GO TO statement.

A label can appear more than once in the list of target labels.

GO TO ( 10, 20 ), SWITCH
GO TO ( 100, 200, 3, 33 ),  2*I-J

In the following example, if SWITCH has the value 1 or 3, the assignment statement labeled 10 is executed; if it has the value 2, the assignment statement labeled 11 is executed. If it has a value less than 1 or greater than 3, the assignment statement Y = Z is executed, because it is the next statement after the computed GO TO statement, and the statement with label 10 is executed next.

   SWITCH = . . .
   GO TO (10, 11, 10) SWITCH
   Y = Z
10 X = Y + 2.
   . . .
11 X = Y

8.7.4. CONTINUE Statement

The CONTINUE statement is defined as follows:

Table 8-14.

 

continue_stmt

is

CONTINUE

Typically, the statement has a label and is used for DO termination; however, it can serve as some other place holder in the program or as a branch target statement. It can appear without a label. The statement by itself does nothing and has no effect on the execution sequence or on program results. The following are examples of CONTINUE statements:

100 CONTINUE
CONTINUE

8.7.5. STOP Statement

The STOP statement terminates the program whenever and wherever it is executed. The STOP statement is defined as follows:

Table 8-15.

 

stop_stmt

is

STOP [ stop_code ]

 

stop_code

is

scalar_char_constant

EXT

 

or

digit ...

The character constant or list of digits identifying the STOP statement is optional and is called a stop code.

When the stop_code is a string of digits, leading zeros are not significant; 10 and 010 are the same stop_code. You can specify from 1 to 80 digits.

The stop code is accessible following program termination. The Cray Fortran Compiler sends it to the standard error file (stderr). The following are examples of STOP statements:

STOP
STOP 'Error #823'
STOP 20

Note: The Fortran standard specifies from 1 to 5 digits in the stop_code.