MODULE 4 · CONDITIONS · 7/8

CONTINUE vs NEXT SENTENCE

10 min10 XPQuiz

Sometimes a branch should do nothing. A common case is a condition that's easier to write positively, with all the real work in the ELSE. COBOL needs some statement in the empty branch, and there are two candidates that look similar but behave very differently.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. NOOPS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-AMOUNT   PIC 9(5) VALUE 100.
       PROCEDURE DIVISION.
           IF WS-AMOUNT > 0
               CONTINUE
           ELSE
               DISPLAY "ZERO AMOUNT"
           END-IF
           DISPLAY "1 AFTER CONTINUE".

           IF WS-AMOUNT > 0
               NEXT SENTENCE
           ELSE
               DISPLAY "ZERO AMOUNT"
           END-IF
           DISPLAY "2 SKIPPED BY NEXT SENTENCE"
           ADD 1 TO WS-AMOUNT.

           DISPLAY "3 NEXT SENTENCE LANDS HERE, AMOUNT " WS-AMOUNT
           STOP RUN.

Output:

1 AFTER CONTINUE
3 NEXT SENTENCE LANDS HERE, AMOUNT 00100

CONTINUE: a no-op

CONTINUE does nothing. Control carries on after the END-IF, exactly as if the branch had run an empty block. That's why 1 AFTER CONTINUE prints. It's the modern, structured choice, and it works anywhere a statement is allowed — an empty WHEN in an EVALUATE, for example.

NEXT SENTENCE: a hidden jump

A sentence in COBOL is everything up to a period. NEXT SENTENCE means "jump to the statement after the next period", ignoring END-IF completely. In the example it skipped both the DISPLAY "2 ..." and the ADD, so the amount is still 100.

It dates from pre-1985 COBOL, when a period was the only way to end an IF, and there it was harmless: the next period was the end of the IF. Mixed with END-IF it becomes a trap. Someone adds a statement after the END-IF, believes it always runs, and in production it is silently skipped whenever the condition is true.

GnuCOBOL flags it when you compile with warnings enabled, as this course does:

warning: NEXT SENTENCE is archaic in GnuCOBOL [-Warchaic]

IBM Enterprise COBOL also accepts NEXT SENTENCE inside an IF that ends with END-IF (as an IBM extension), so you can't count on the compiler to stop you.

Rule

Write CONTINUE, never NEXT SENTENCE. When you meet NEXT SENTENCE in existing code, find the period it jumps to before you change anything nearby.

Do you even need the empty branch?

Often you can drop it by reversing the condition:

           IF WS-AMOUNT NOT > 0
               DISPLAY "ZERO AMOUNT"
           END-IF

Use CONTINUE when the positive form is much clearer than the negation, or in an EVALUATE where a WHEN deliberately ignores a value:

           EVALUATE WS-REC-TYPE
               WHEN "H"
                   CONTINUE
               WHEN "D"
                   ADD 1 TO WS-DETAIL-COUNT
               WHEN OTHER
                   DISPLAY "BAD RECORD TYPE " WS-REC-TYPE
           END-EVALUATE

(Here header records are recognised but not processed, so they don't fall into WHEN OTHER.)

On the job

Code reviewers and static-analysis tools used in mainframe shops commonly flag NEXT SENTENCE. If you're asked to modernise a program, replacing it with CONTINUE is only safe once you've confirmed the next period really is at the end of the IF — otherwise the behaviour changes.

Check your understanding

1. Where does control go after CONTINUE inside an IF?
2. Where does control go after NEXT SENTENCE?
3. You're adding a line to old period-style code that contains NEXT SENTENCE. What's the safest change?