Other Control Structures

Unconditional GOTO Statement

GOTO label

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

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

   ...
10 CONTINUE
      WRITE(*,*)'Enter a positive value'
      READ(*,*)A
   IF (A .LE. 0.0) GOTO 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 GOTO 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 GOTO Statement

GOTO (label1, label2, ..., labeln), integer-expression

This form of GOTO statement is obscure and its use in modern programs is strongly discouraged. It is equivalent to this block IF statement:

IF (integer-expression .EQ. 1) THEN
   GOTO label1
ELSE IF (integer-expression .EQ. 2) THEN
   GOTO label2
...
ELSE IF (integer-expression .EQ. n) THEN
   GOTO labeln
ENDIF

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

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

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

...
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, GOTO and STOP statements, sequential execution of statements may be altered by the END= and ERR= keywords in input/output statements.


Obliquity Copyright © 1995-2007 by David Harper and L.M. Stockman
Designed and maintained by Obliquity
Contact us about this page
Last modified on 1 April 2007