GOTO, labels and CONTINUE
Before Fortran 90 there was one way to change the flow of a program outside a loop: a statement label and a GO TO. The form still compiles, numerical libraries still use it, and reading it is a skill. This lesson covers the label, the three forms of the jump, the CONTINUE that exists to carry a label, and the reason new code uses none of them.
Statement labels
Any statement may carry a number from 1 to 99999 in front of it. The number is a name for the statement and nothing else: it does not declare a variable, it is not counted and it does not have to be in order. In free form it sits at the start of the line; in fixed form it sits in columns 1 to 5, which is where the column rule comes from.
A label is only useful when something refers to it, and in modern Fortran the only things that do are GO TO, the labelled DO and the legacy FORMAT statement of an old WRITE. A label with no reference is a warning from -Wall in most compilers and a piece of history in all of them.
GO TO
go to 10 continues at the statement labelled 10. That is the whole statement, and a loop written with it needs a label, an increment, a test and a jump back.
The file goto_loop.f90:
program goto_loop
implicit none
integer :: i
i = 0
10 continue
i = i + 1
print *, "i:", i
if (i < 3) go to 10
print *, "done"
end program goto_loop
gfortran -Wall -Wextra -std=f2018 goto_loop.f90 -o goto_loop
./goto_loop
i: 1
i: 2
i: 3
done
Compare the four statements of that loop with the three of do i = 1, 3 ... end do: the jump version makes the reader follow the label to see where the loop ends, and the structured version states the loop in the header. Multiply that by a screen of code and the reason for the change is clear.
The spacing is free: goto, go to and GO TO are the same statement to the compiler, like every keyword in the language.
Computed GOTO
The computed form takes a list of labels and an integer, and jumps to the one the integer selects: go to (10, 20, 30), i goes to 10 for i = 1, to 20 for i = 2 and to 30 for i = 3. A value outside the range falls through to the next statement, which is the detail that made it a dispatch with a hole in it.
The file computed.f90:
C A computed GOTO, the way FORTRAN 77 wrote a dispatch.
PROGRAM COMPUTED
INTEGER I
I = 2
GO TO (10, 20, 30), I
10 PRINT *, "one"
GO TO 40
20 PRINT *, "two"
GO TO 40
30 PRINT *, "three"
40 CONTINUE
END
gfortran -Wall -Wextra -std=legacy -ffixed-form computed.f90 -o computed
./computed
two
The dispatch is a select case written as a jump table, and the GO TO 40 after each branch is the end select. The modern form states the same thing, checks the selector type and has no label to get wrong, which is why SELECT CASE replaced it.
CONTINUE
continue is a statement that does nothing. It exists because a label needs a statement to sit on, and the end of a loop had nothing to label; the conventional closing line of a labelled DO is therefore continue. In the fixed-form program above it is the landing point of three jumps and the end of the construct at the same time.
In new code continue appears only as an empty body that a reader is meant to see, and even there a comment is usually better. A CONTINUE in the middle of a modern program is a sign that the code came from somewhere else.
A label that does not exist
The compiler checks every label reference against the labels defined in the unit, which is the one thing the old form has in common with a named construct.
The file nolabel.f90:
program nolabel
implicit none
integer :: i
i = 1
if (i > 0) go to 99
print *, i
end program nolabel
gfortran -Wall -Wextra -std=f2018 nolabel.f90 -o nolabel
nolabel.f90:5:21:
5 | if (i > 0) go to 99
| 1
Error: Label 99 referenced at (1) is never defined
The message names the label and the line, and the caret points at the end of the reference. A jump to a label in another program unit is not possible at all, which is why the error says "in the unit" rather than "in the program".
Why new code has none
Three reasons, in order of weight:
- A structured construct states the flow in one place. A loop header says which values the body runs for; a jump says only where control goes next, and the loop has to be reconstructed by following labels.
- Labels are global to the unit, so a label added in one place can collide with one added in another. The structured forms nest, and nesting has no names to collide.
- The compiler can check a construct. It can put an iteration counter in a do, vectorise a loop whose body it understands, and report an exit outside a loop; a jump to a label gives it nothing to check but the label.
A reader still has to read the old form, because the libraries that this course calls were written with it. The rule for reading is the same one for writing: follow the labels once, write down the loop or the branch they mean, and then read the code as if it had been written that way.
Exercises
- Rewrite the first program with do i = 1, 3 ... end do. How many lines does it take now?
- In the computed GOTO, set I = 4 and run it. What happens, and why is that a bug in a dispatch?
- Put a label on the print statement of the first program and jump to it twice. What does the output look like?
- Try go to 10 from inside a do loop into the body of the same loop. Does it build, and what does it do?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is a statement label?
Answer
A number in front of a statement that other statements can name
The label is a name for the statement and does not declare anything.
-
What does
go to 10do?Answer
It continues at the statement labelled 10
The number is a label, not an offset.
-
What does a computed GOTO do for a value outside its list?
Answer
It falls through to the next statement
An out-of-range selector does not select, so control continues below the statement.
-
Why does CONTINUE exist?
Answer
It gives a label a statement to sit on
It is the do-nothing statement used at the end of a labelled DO.
-
What error does a reference to an undefined label give?
Answer
Error: Label 99 referenced at (1) is never defined
The compiler checks every reference against the labels of the unit.
-
What does this program print?
program p implicit none integer :: i i = 0 10 continue i = i + 1 if (i < 3) go to 10 print *, i end program pAnswer
3
The loop runs for i = 1 and 2 and jumps while i < 3, so it ends with 3.
-
Which construct replaced the computed GOTO?
Answer
SELECT CASE
The case construct states a dispatch over one selector.
-
Where is a label written in fixed-form source?
Answer
In columns 1 to 5
Columns 1 to 5 hold the label, column 6 a continuation, and the statement starts at 7.
-
Why do structured constructs make optimisation easier?
Answer
The compiler can see the loop body and its bounds instead of following jumps
A construct gives the compiler a region it can analyse and reorder.
-
Can a GO TO jump into another program unit?
Answer
No: labels are local to the unit
Labels belong to the unit, and the compiler reports a reference it cannot resolve.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container. The computed GOTO was built with -std=legacy -ffixed-form.
Article author: Arthur Isaev