GOTO, arithmetic IF and labelled DO

Old Fortran has no DO ... END DO in the code that used it, no SELECT CASE and no block IF: a loop ended at a labelled statement, a choice between three paths was a jump into a list of labels, and a three-way branch on the sign of a number was a statement of its own. This lesson runs one program with a labelled DO and a computed GOTO, a second with the arithmetic IF and the assigned GOTO, and then the same computation written the way the course has taught it.

The labelled DO

DO 10 I = 1, 5 runs the statements that follow it down to the statement labelled 10, which is usually a CONTINUE. The label is in columns 1 to 5, the CONTINUE is the do-nothing statement, and the loop is closed there rather than by an END DO.

The file oldflow.f:

C LABELLED DO AND COMPUTED GOTO PROGRAM OLD_FLOW IMPLICIT NONE INTEGER I, TOTAL, MODE TOTAL = 0 DO 10 I = 1, 5 TOTAL = TOTAL + I 10 CONTINUE PRINT 90, TOTAL 90 FORMAT('TOTAL = ', I3) MODE = 2 GO TO (20, 30, 40), MODE 20 PRINT 90, 111 GO TO 50 30 PRINT 90, 222 GO TO 50 40 PRINT 90, 333 50 CONTINUE END

gfortran -Wall -Wextra -std=legacy -ffixed-form oldflow.f -o oldflow
./oldflow

TOTAL =  15
TOTAL = 222

The loop adds one to five and prints fifteen, which is what the modern form would do. The label 90 on the FORMAT statement is not a jump target: a PRINT or WRITE takes the label of a format statement as its second argument, which is how old code passed a format around without a character variable.

The computed GOTO

GO TO (20, 30, 40), MODE is a branch table: with MODE equal to one it goes to the first label, with two to the second, and with three to the third. Here MODE is two, so the program prints the second number and jumps over the third arm to the label 50.

A value outside one to three is undefined behaviour in the standard, and gfortran implements it as a jump to the next statement, which is the one case a reader should test before trusting an old program: a mode that arrives as four does not fail, it continues somewhere.

The arithmetic IF

IF (X) 10, 20, 30 is three-way branch on a real: negative to the first label, zero to the second, positive to the third. It is the only place the language tests a real for zero, and it does so on the value as computed, which is the reason old numerical code is full of it.

The file arith.f:

C ARITHMETIC IF AND ASSIGNED GOTO PROGRAM ARITH IMPLICIT NONE REAL X INTEGER ADD X = -2.5 IF (X) 10, 20, 30 10 PRINT 90, 'negative' GO TO 40 20 PRINT 90, 'zero' GO TO 40 30 PRINT 90, 'positive' 40 CONTINUE ASSIGN 100 TO ADD GO TO ADD, (100, 200) 100 PRINT 90, 'lane one' GO TO 300 200 PRINT 90, 'lane two' 300 CONTINUE 90 FORMAT(A) END

gfortran -Wall -Wextra -std=legacy -ffixed-form arith.f -o arith
./arith

negative
lane one

The FORMAT(A) with a single A descriptor prints a character constant of any length, which is how the program prints its three words without writing the text into a variable.

The assigned GOTO

The last two labels do something stranger. ASSIGN 100 TO ADD puts a label number into an integer variable, and GO TO ADD, (100, 200) jumps to the label the variable holds, provided it is one of the two in the list. It is a computed branch whose table lives in the variable, and it is the feature that made old Fortran programs impossible to read with a text editor: the target of a jump is a value.

ASSIGN and the assigned GO TO were deleted from the standard in Fortran 2018 and gfortran still accepts them under -std=legacy. They are the one pair in this lesson that no modern compiler has to support, which makes them the first thing to replace in a program that is being ported.

The same program today

The same two computations, written in free form with the statements of parts 2 and 3 of the course:

The file modern_flow.f90:

program modern_flow implicit none integer :: i, total, mode total = 0 do i = 1, 5 total = total + i end do print '(a,i3)', 'TOTAL = ', total mode = 2 select case (mode) case (1) print '(a,i3)', 'TOTAL = ', 111 case (2) print '(a,i3)', 'TOTAL = ', 222 case default print '(a,i3)', 'TOTAL = ', 333 end select end program modern_flow

gfortran -Wall -Wextra -std=f2018 modern_flow.f90 -o modern_flow
./modern_flow

TOTAL =  15
TOTAL = 222

The output is identical and the shape is not. The loop has an end instead of a label, the branch has a CASE per value and a CASE DEFAULT that catches anything else, and no statement can jump into the middle of another. A reader of the old version has to follow labels to see the control flow; a reader of the new one reads it top to bottom.

Rules for old control flow

Read a labelled DO from its label backwards: find the statement with the label and everything between is the body. Draw the labels of a unit on paper before changing it, because two GO TO statements can make a loop that no keyword shows. Replace the assigned GOTO first, then the computed ones with SELECT CASE, then the arithmetic IF with a block IF and a comparison, checking each step against the old output. The next lesson does exactly that to a whole program and shows the diff and the two outputs side by side.

Exercises

  1. Set MODE = 4 in oldflow.f and run it. What does the computed GOTO do, and does the program still print two lines?
  2. Set X = 0.0 in arith.f. Which line runs, and why is the third arm never reached?
  3. Rewrite the arithmetic IF with a block IF and SIGN(1.0, X). Does the output change?
  4. Add a second loop inside the labelled DO of oldflow.f with the same label. What does the compiler say?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How does a labelled DO end?

    Answer

    At the statement with the label the DO names

    DO 10 I = 1, 5 ends at label 10.

  2. What does GO TO (20, 30, 40), MODE do with MODE equal to 2?

    Answer

    Jumps to the label 30

    The second label of the list.

  3. What does IF (X) 10, 20, 30 test?

    Answer

    The sign of X: negative, zero, positive

    It is the three-way arithmetic IF.

  4. What does ASSIGN 100 TO ADD do?

    Answer

    Puts the label 100 into the integer ADD

    The assigned GOTO jumps to that label.

  5. Which feature has been deleted from the standard?

    Answer

    ASSIGN and the assigned GOTO

    Deleted in Fortran 2018, accepted by gfortran with -std=legacy.

  6. Why does a PRINT take a label as its second argument?

    Answer

    The label names a FORMAT statement

    That is how old code passed a format.

  7. What is undefined when a computed GOTO gets a value outside its list?

    Answer

    The branch itself; gfortran continues to the next statement

    One of the cases worth testing in an old program.

  8. Which modern statement replaces the computed GOTO?

    Answer

    SELECT CASE

    A case per value, plus CASE DEFAULT.

  9. Why is the modern program easier to read?

    Answer

    No statement jumps into the middle of another

    Control flow follows the page.

  10. What is the first thing to replace when porting?

    Answer

    ASSIGN and the assigned GOTO

    No modern compiler has to accept them.

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.

Article author: Arthur Isaev