Other Control Structures

Unconditional GO TO Statement

GO TO label

When an unconditional GO TO is encountered, control is immediately transferred to the executable statement labelled with the label. Whilst it is sometimes necessary to use unconditional GO TO statements in a program, they should be used sparingly lest unreadable 'spaghetti' code result.

A GO TO statement may be used to transfer control out of a block IF or DO loop but not in to them. The unconditional GO TO statement is often used in conjunction with logical or block IF statements to construct loops when a simple DO loop is not appropriate.

Example

   10 CONTINUE
         WRITE(*,*)'Enter a positive value'
         READ(*,*)A
      IF (A .LE. 0.0) GO TO 10

The statement labelled 10 is a CONTINUE statement which is often used at the beginning or end of a loop structure. This program fragment executes the CONTINUE statement (which does nothing) and then prints out the statement Enter a positive value. The program then reads in a value from the standard input device and stores it in the variable A. At this point A is tested to see if it is positive. If it is, then control passes to the next executable statement, but if it isn't, then the GO TO 10 statement is executed and the program returns to the CONTINUE statement. The program then goes through the WRITE and READ statements again and tests the new value of A. The program will not break out of this loop until A is positive.

The indentation of the loop body makes the structure of the program that much more readable.

Computed GO TO Statement

GO TO (label1, label2, …, labeln), integer-expression

This form of GO TO statement is obscure and its use is strongly discouraged.

It is equivalent to this block IF statement:

IF (integer-expression .EQ. 1) THEN
   GO TO label1
ELSE IF (integer-expression .EQ. 2) THEN
   GO TO label2
…
ELSE IF (integer-expression .EQ. n) THEN
   GO TO labeln
END IF

If the integer-expression is less than 1 or greater than n, then control passes on and no GO TO is executed. It is permissible for two or more of the labels to be the same.

As with the unconditional GO TO statement, unrestrained use of the computed GO TO statement rapidly leads to unreadable code.

ASSIGN and Assigned GO TO Statements

ASSIGN label TO integer-variable

The ASSIGN statement is the only way to assign a statement label value to an INTEGER variable. The statement label must be a label in the same program unit as the ASSIGN statement and it must be the label of an executable statement or a FORMAT statement. When an INTEGER variable is assigned a statement label in such a way, it may not be referenced as a number as it no longer has a numerical value.

GO TO integer-variable, (label1, label2, …, labeln)

This form of GO TO statement is obscure and its use is strongly discouraged.

The integer-variable following the GO TO in an assigned GO TO must be assigned a statement label in an earlier ASSIGN statement. The ASSIGN and GO TO statements, plus the labelled statements, must all be in the same program unit. The comma and the list of labels in parentheses in the assigned GO TO statement are optional but if present, then the statement label assigned to the integer-variable must match one of the labels in the list.

PAUSE Statement

PAUSE 'string'

A PAUSE statement halts the program in such a way that execution can be resumed in some manner by the user. When a PAUSE statement is encountered, the optional string is printed out and the program waits for the user to reactivate it. This string must be a constant, not a variable. Alternatively, it can be an integer up to five digits long.

Implementation of the PAUSE statement is system-dependent and its use is strongly discouraged.

Instead, the PAUSE statement can be replaced by one WRITE and one READ statement which is more flexible and reliable.

Example

      IF (DEBUG) PAUSE 'Entering main DO loop'

In this program fragment, the program pauses if the LOGICAL variable DEBUG is true. The string Entering main DO loop is printed out to the standard output device as the program waits for the user.

Because it is impossible to predict how any system will treat a PAUSE statement, it is safer to use a WRITE/READ pair of statements instead:

      IF (DEBUG) THEN
         WRITE(*,*)'Entering main DO loop'
         READ(*,*)
      END IF

In this program fragement, if the LOGICAL variable DEBUG is true, then the program writes the string Entering main DO loop to the standard output device. The user then has to press the Enter or Return key on the keyboard to resume running the program.

STOP Statement

STOP 'string'

A STOP statement stops the program and returns control to the computer's operating system. When a STOP statement is encountered, the optional string is printed out and the program ends. This string must be a constant, not a variable. Alternatively, it can be an integer up to five digits long. Although ideally a program stops only at the end of the program, it may be necessary, perhaps as an error-trapping device, to have multiple STOP statements in a program. In this instance, putting a meaningful message in the string following the STOP can help with debugging.

It is not necessary for a program to contain a STOP statement. The program will automatically terminate when the END statement in the main program is encountered.

Example

      IF (X .EQ. 0.0) STOP 'Emergency stop - Denominator is zero'

In this program fragment, the program stops if X is zero. The string Emergency stop - Denominator is zero is printed out to the standard output device as the program terminates.

Other Considerations

Besides the DO, IF, GO TO and STOP statements, sequential execution of statements may be altered by the END= and ERR= keywords in input/output statements, and by alternate entries and returns from subprograms.