CONTINUE, NEXT SENTENCE and a rewrite

This lesson closes the part on control flow with the two statements that exist only for the reader: CONTINUE, which does nothing on purpose, and NEXT SENTENCE, which the language kept from the days when a period was the only structure a program had. It also takes the loop of the previous lesson and writes it again without a jump.

A GO TO loop rewritten

The old version counts to three with a label and a jump; the new version uses PERFORM VARYING. Both are in one program, and the output is the same:

IDENTIFICATION DIVISION. PROGRAM-ID. REWRITE. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 0. 01 PASS-N PIC 9 VALUE 0. PROCEDURE DIVISION. DISPLAY "old version:". PERFORM OLD-TOP THRU OLD-DONE. DISPLAY "new version:". PERFORM NEW-WAY. STOP RUN. OLD-TOP. ADD 1 TO N-F. DISPLAY "pass " N-F. IF N-F < 3 GO TO OLD-TOP END-IF. DISPLAY "done " N-F. OLD-DONE. EXIT. NEW-WAY. PERFORM VARYING N-F FROM 1 BY 1 UNTIL N-F > 3 ADD 1 TO PASS-N DISPLAY "pass " N-F END-PERFORM. DISPLAY "done " PASS-N.

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

old version:
pass 1
pass 2
pass 3
done 3
new version:
pass 1
pass 2
pass 3
done 3

The old version is performed with THRU because the jump stays inside its own range, which is the safe way to write that shape. The new one has no label, no jump and no exit paragraph, and a reader sees the four lines that repeat without looking anywhere else.

The counter is not the count

The first attempt at the new version printed done 4 where the old one printed done 3. The reason is the one this course met in the lesson on PERFORM VARYING: the loop adds the step after the last pass, so N-F holds 4 when the statement ends, while the GO TO had left it at 3.

The rewrite in the listing keeps a separate PASS-N that the body increments, and both versions print 3. A rewrite that only changes the control statements and keeps reading the loop counter afterwards can change the output of the program, and this is one of the few defects that a test with a golden file catches immediately.

CONTINUE does nothing

CONTINUE is a statement with no effect. It exists for the branch of an IF that has nothing to do but must be written:

IDENTIFICATION DIVISION. PROGRAM-ID. CONT. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 1. PROCEDURE DIVISION. IF N-F = 1 CONTINUE ELSE DISPLAY "not one" END-IF. DISPLAY "after the IF". STOP RUN.

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

after the IF

The first branch did nothing and the flow went on to the statement after END-IF, which is what CONTINUE means. A program that writes an empty branch with no statement at all is refused by the compiler, and CONTINUE is the accepted way to say that the case needs no action.

NEXT SENTENCE skips to the period

NEXT SENTENCE looks like CONTINUE and is not the same: it transfers control to the statement after the next period, so every statement between the END-IF and that period is skipped:

IDENTIFICATION DIVISION. PROGRAM-ID. NEXTS. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 1. PROCEDURE DIVISION. IF N-F = 1 NEXT SENTENCE ELSE DISPLAY "not one" END-IF DISPLAY "skipped by NEXT SENTENCE" DISPLAY "also skipped". DISPLAY "after the period". STOP RUN.

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

nexts.cob:10: warning: NEXT SENTENCE is archaic in GnuCOBOL [-Warchaic]
8 | PROCEDURE DIVISION.
9 | IF N-F = 1
10 > NEXT SENTENCE
11 | ELSE
12 | DISPLAY "not one"
after the period

Two statements that look unconditional were skipped, because they sat before the period that closed the sentence. The compiler prints a warning for the statement and nothing for the skip: the program is legal COBOL and its output is not what its layout suggests.

NEXT SENTENCE is not in any program this course writes. The whole purpose of END-IF is to make the period unnecessary, and with it the sentence as a unit of control.

Scope terminators and sentences

A scope terminator ends one statement, and it is what the modern style uses in place of the period: END-IF, END-EVALUATE, END-PERFORM, END-READ, END-WRITE. The period is then left to the end of the paragraph, where it separates one unit of work from the next.

That is the rule this course follows, and it is what makes the rewrite in the first section possible: a block that ends with its own terminator can be moved, nested or replaced without searching for the period that closes it. The next part is tables, where the same small set of statements walks an array that lives in working storage.

Exercises

  1. Remove PASS-N from the rewrite and print N-F in the last line of both versions. Which two numbers appear, and why?
  2. Replace CONTINUE with NEXT SENTENCE and put a DISPLAY between END-IF and the period. Which line is skipped?
  3. Write a loop with a flag and an inline PERFORM ... UNTIL flag = "Y" that leaves the loop from two different conditions. How many jumps does the version with GO TO need?
  4. Take any paragraph of an old program you have and mark every period in it. Which of them close an IF or a PERFORM?

Questions

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

  1. What does CONTINUE do?

    Answer

    Nothing: it is a statement that marks an empty branch

    The branch with CONTINUE did nothing and the flow continued after END-IF.

  2. What does NEXT SENTENCE do?

    Answer

    It jumps to the statement after the next period

    Two statements between END-IF and the period were skipped in the run on this page.

  3. Why does the rewritten loop need a separate count field?

    Answer

    The VARYING counter ends one step past the last pass

    The first rewrite printed done 4 because the counter had been incremented once more after the body.

  4. Which statement ends a block without ending the sentence?

    Answer

    A scope terminator such as END-IF or END-PERFORM

    Scope terminators close one statement, which is why the period can be kept for the end of the paragraph.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 1. PROCEDURE DIVISION. IF N-F = 1 CONTINUE ELSE DISPLAY "other" END-IF. DISPLAY "after". STOP RUN.

    Answer

    after

    CONTINUE leaves the branch empty and the flow reaches the DISPLAY after END-IF.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 1. PROCEDURE DIVISION. IF N-F = 1 NEXT SENTENCE END-IF DISPLAY "hidden". DISPLAY "visible". STOP RUN.

    Answer

    visible

    NEXT SENTENCE jumps past the period, so the first DISPLAY is skipped with the statement before it.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 0. 01 C-F PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM VARYING N-F FROM 1 BY 1 UNTIL N-F IS GREATER THAN 3 ADD 1 TO C-F END-PERFORM. DISPLAY "N " N-F " C " C-F. STOP RUN.

    Answer

    N 4 C 3

    The counter goes one past the limit while the field the body increments holds the three passes.

  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. OLD-TOP. ADD 1 TO N-F. IF N-F IS LESS THAN 2 GO TO OLD-TOP END-IF. DISPLAY "N " N-F. STOP RUN.

    Answer

    N 2

    The label is in the main flow, so the loop runs twice and prints once, unlike a jump out of a performed paragraph.

  9. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q9. 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. IF N-F IS LESS THAN 3 GO TO WORK END-IF. WORK-END. EXIT.

    Answer

    back 3

    The jump stays inside the performed range, so the paragraph repeats until the field reaches 3 and the PERFORM returns.

  10. A rewrite replaces a GO TO loop with PERFORM VARYING and the report changes by one. What is the most likely cause?

    Answer

    The old loop left the counter at the last value, the new one leaves it one step further

    That is the difference this page measured: done 3 against done 4, fixed with a count field.

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