GO TO and why not

GO TO is the oldest control statement the language has, and every COBOL program of a certain age is built out of it. PERFORM was added later and is the reason new code does not need the jump. This lesson shows what GO TO does, what it costs, and the statement that replaces it.

GO TO and a paragraph

GO TO names a paragraph and transfers control to it. Nothing is saved and nothing returns:

IDENTIFICATION DIVISION. PROGRAM-ID. GOTOLOOP. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 0. PROCEDURE DIVISION. LOOP-PARA. ADD 1 TO N-F. DISPLAY "pass " N-F. IF N-F < 3 GO TO LOOP-PARA END-IF. DISPLAY "done, N is " N-F. STOP RUN.

cobc -x -free -Wall -o gotoloop gotoloop.cob
./gotoloop

pass 1
pass 2
pass 3
done, N is 3

A loop without PERFORM

The program above is a loop. The paragraph name at the top is the label, the GO TO at the bottom is the jump back, and the condition in the middle is the exit. It has no END-PERFORM and no scope of its own: the paragraph runs from its label to the next label or to the end of the program.

A reader can follow this one because it is ten lines long. In a program of two thousand lines the same shape has three ways in and four ways out per paragraph, and the reader has to hold the whole flow in mind to answer a question about one line. That is the cost, and it is the reason the structured statements were added to the language: not to save typing but to make a paragraph readable on its own.

GO TO ... DEPENDING ON

A computed jump takes a number and a list of paragraph names; the number picks one of them. It is the dispatch that EVALUATE replaced:

IDENTIFICATION DIVISION. PROGRAM-ID. GOTODEP. DATA DIVISION. WORKING-STORAGE SECTION. 01 CHOICE-F PIC 9 VALUE 2. PROCEDURE DIVISION. GO TO P-ONE P-TWO P-THREE DEPENDING ON CHOICE-F. P-ONE. DISPLAY "one". GO TO P-END. P-TWO. DISPLAY "two". GO TO P-END. P-THREE. DISPLAY "three". P-END. DISPLAY "end of the dispatch". STOP RUN.

cobc -x -free -Wall -o gotodep gotodep.cob
./gotodep

two
end of the dispatch

The paragraphs have to be arranged so that a jump back into the list does not fall into the next case, which is why each of them ends with a GO TO to the exit. With a number outside the list, as zero or four, the statement does nothing and the next paragraph runs, so a program that uses it needs a check of the value before the jump. EVALUATE has WHEN OTHER for that case and no list to keep in order.

A jump out of a PERFORM

The dangerous jump leaves the paragraph that a PERFORM is running. The program below sets a total, jumps over the statement that marks the total as final, and lands on the exit paragraph:

IDENTIFICATION DIVISION. PROGRAM-ID. SKIPFLAG. DATA DIVISION. WORKING-STORAGE SECTION. 01 TOTAL-F PIC 9(3) VALUE 0. 01 FLAG-F PIC X VALUE "N". PROCEDURE DIVISION. PERFORM ADD-UP. IF FLAG-F = "N" DISPLAY "total not final" END-IF. DISPLAY "total " TOTAL-F. STOP RUN. ADD-UP. MOVE 100 TO TOTAL-F. GO TO ADD-UP-EXIT. MOVE "Y" TO FLAG-F. ADD-UP-EXIT. EXIT.

./skipflag
echo "exit status $?"

exit status 0

The program printed nothing at all. The PERFORM had put a return address in place, the GO TO landed on a paragraph outside that PERFORM, and when that paragraph ended there was nothing to return to: the flow left the procedure division and the run ended with status zero. Neither the compiler nor the run reported a problem, and the total, the flag and the two DISPLAY statements of the main flow never ran.

This is the strongest argument against the statement, and it is not a theoretical one. A jump that stays inside one paragraph is a loop; a jump that leaves a performed paragraph destroys the call. Old code keeps the second kind down by convention, with one exit paragraph per block, and the convention holds only as long as every writer knows it.

Why the course avoids it

Everything GO TO does, the structured statements do with a visible scope: IF for a branch, EVALUATE for a dispatch, PERFORM ... UNTIL for a loop. A reader of the structured version sees where a block starts and where it ends without searching the program for a label, and the compiler checks that every block ends.

The one place the language still needs a jump is a loop with two exits, such as a read loop that also stops on an error code, and even there a flag or a PERFORM WITH TEST AFTER is cheaper to read than a label at the top. The next lesson takes a program written with GO TO and rewrites it without one, with the same output as the proof.

Exercises

  1. Change the first program to stop at 5 passes. How many lines print?
  2. Add a fourth paragraph to the dispatch and a value of 4 to CHOICE-F. Which branch runs, and why?
  3. Set CHOICE-F to 0 and run the dispatch again. What happens, and where does the flow go?
  4. Remove the GO TO ADD-UP-EXIT from the last program and run it. What prints now, and why does the return work?

Questions

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

  1. What does GO TO LOOP-PARA do?

    Answer

    It transfers control to that paragraph and saves nothing to return to

    GO TO is a jump, unlike PERFORM, which remembers where to come back to.

  2. What does GO TO P-ONE P-TWO P-THREE DEPENDING ON C do?

    Answer

    It jumps to the paragraph whose position matches the value of C

    The value of the field selects one name from the list by its position.

  3. What does a GO TO out of a performed paragraph do to the return?

    Answer

    It loses it: the flow does not come back to the statement after the PERFORM

    The program on this page printed nothing and ended with status zero, because the flow never returned to the main code.

  4. Which statement replaces GO TO ... DEPENDING ON in structured COBOL?

    Answer

    EVALUATE, with WHEN OTHER for the value outside the list

    EVALUATE selects one branch by value, closes with END-EVALUATE and has a default branch.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 0. PROCEDURE DIVISION. TOP-PARA. ADD 1 TO N-F. IF N-F IS LESS THAN 2 GO TO TOP-PARA END-IF. DISPLAY N-F. STOP RUN.

    Answer

    2

    The jump back runs the paragraph twice, and the field is printed once after it.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 0. PROCEDURE DIVISION. A-PARA. ADD 1 TO N-F. GO TO C-PARA. B-PARA. ADD 10 TO N-F. C-PARA. DISPLAY N-F. STOP RUN.

    Answer

    1

    The jump skips the paragraph that would add ten, so only the first addition happened.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 C-F PIC 9 VALUE 3. PROCEDURE DIVISION. GO TO P-ONE P-TWO DEPENDING ON C-F. DISPLAY "no branch". STOP RUN. P-ONE. DISPLAY "one". STOP RUN. P-TWO. DISPLAY "two". STOP RUN.

    Answer

    no branch

    A value past the end of the list makes the statement do nothing, and the statement after it runs.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM WORK THRU WORK-END. DISPLAY "back " N-F. STOP RUN. WORK. ADD 1 TO N-F. GO TO WORK-END. ADD 1 TO N-F. WORK-END. EXIT.

    Answer

    back 1

    The jump skips the second ADD, and this time the exit paragraph is inside the performed paragraph, so the return happens normally.

  9. A program has a loop written with a label, a GO TO and an IF. Which problem does a reader of the same program in ten years have?

    Answer

    They cannot see the loop as a block, and every jump must be followed by hand

    The cost of the jump is on the reader: the flow is a graph of labels rather than a block with a visible scope.

  10. Why does the course keep GO TO out of its own programs?

    Answer

    IF, EVALUATE and PERFORM do the same work with a scope the reader can see

    The structured statements give the same control flow with a visible start and end, and the compiler checks the ends.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image