IF, ELSE and Condition Names

COBOL GnuCOBOL 3.2.0 · ✓ verified by execution on 2026-08-16

Conditionals are the bedrock of any program’s logic. In COBOL, testing conditions is primarily done using the IF statement. While the syntax might seem familiar at first glance, COBOL has a few unique quirks regarding how it handles the end of an IF block and how it deals with boolean values.

The IF Statement

A basic IF statement in COBOL reads like English. You test a condition, optionally provide an ELSE branch, and close the block.

cobol ✓ verified output
       IDENTIFICATION DIVISION.
       PROGRAM-ID. BASIC-IF.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 AGE PIC 99 VALUE 20.
       PROCEDURE DIVISION.
           IF AGE >= 18
               DISPLAY 'ADULT'
           ELSE
               DISPLAY 'MINOR'
           END-IF.
           STOP RUN.
Output
ADULT

The Crucial END-IF

In the example above, notice the END-IF. This is an explicit scope terminator. It tells the compiler exactly where the IF statement ends.

In older dialects of COBOL (prior to COBOL-85), END-IF didn’t exist. Instead, you had to close an IF block using a period (.). This led to one of the most infamous bugs in COBOL history: the runaway period bug.

If you accidentally placed a period inside an IF branch, it would implicitly close all currently open IF statements.

Predict the output cobol

Read the code. What exactly will it print? Commit to an answer before you look.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. RUNAWAY.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 COUNTER PIC 9 VALUE 1.
       PROCEDURE DIVISION.
           IF COUNTER = 0
               DISPLAY 'ZERO'.
               ADD 1 TO COUNTER.
           DISPLAY COUNTER.
           STOP RUN.
Output
2

A novice would expect this program to output 1 because COUNTER is not 0, so the entire indented block should be skipped. But because of the rogue period after 'ZERO', the IF statement closes immediately. The ADD 1 TO COUNTER statement is entirely outside the IF logic and executes unconditionally, resulting in 2! This is why modern COBOL developers strongly prefer END-IF.

The SVG: Scope Terminations

This diagram visualizes how END-IF safely encapsulates logic, whereas a stray period slices through scopes unexpectedly.

Explicit Scope (Safe) IF SCORE >= 80 DISPLAY 'A' END-IF DISPLAY 'DONE' Implicit Scope (Danger) IF SCORE >= 80 DISPLAY 'A' . Premature Exit DISPLAY 'DONE'

CONTINUE vs NEXT SENTENCE

Sometimes you want an IF branch to explicitly do nothing. You have two options, but they behave very differently.

cobol ✓ verified output
       IDENTIFICATION DIVISION.
       PROGRAM-ID. NEXT-TEST.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 AGE PIC 99 VALUE 25.
       PROCEDURE DIVISION.
           IF AGE > 20
               IF AGE = 25
                   NEXT SENTENCE
               ELSE
                   DISPLAY 'NOT 25'
               END-IF
               DISPLAY 'INSIDE OUTER IF'
           END-IF.
           DISPLAY 'AFTER OUTER IF'.
           STOP RUN.
Output
AFTER OUTER IF

Notice how INSIDE OUTER IF was completely skipped! NEXT SENTENCE bypassed the END-IFs and jumped directly to the period at the end of the outer IF block. IBM’s official documentation notes that NEXT SENTENCE is considered an archaic construct. Always use CONTINUE.

88-Levels: COBOL’s Boolean Variables

COBOL does not have a native boolean data type (like true or false). Instead, it uses 88-level condition names.

An 88-level item doesn’t occupy any memory itself. It sits immediately below a variable and acts as an alias for a specific value (or range of values) that the parent variable might hold.

cobol ✓ verified output
       IDENTIFICATION DIVISION.
       PROGRAM-ID. COND-NAME.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 USER-ROLE PIC X VALUE 'A'.
          88 IS-ADMIN VALUE 'A'.
          88 IS-GUEST VALUE 'G'.
       PROCEDURE DIVISION.
           IF IS-ADMIN
               DISPLAY 'WELCOME ADMIN'
           END-IF.
           STOP RUN.
Output
WELCOME ADMIN

Instead of writing IF USER-ROLE = 'A', we can simply write IF IS-ADMIN. This makes the PROCEDURE DIVISION highly readable and self-documenting.

Check yourself

Why is it highly recommended to use END-IF instead of periods to close an IF statement in modern COBOL?

Reveal answer

END-IF explicitly delimits the scope, avoiding the 'runaway period' bug. — A stray period closes ALL open IF scopes implicitly. END-IF is an explicit scope terminator that prevents this bug.

What is the difference between CONTINUE and NEXT SENTENCE?

Reveal answer

CONTINUE passes control to the next instruction; NEXT SENTENCE jumps to the next period. — CONTINUE is a no-op that just proceeds. NEXT SENTENCE behaves like a GOTO that targets the closest following period.

Do 88-level condition names occupy their own memory?

Reveal answer

No, they are merely aliases for checking if a parent variable holds a specific value. — 88-levels do not define memory. They are condition names that provide a readable way to test the value of a previously defined data item.

Challenges

Challenge 1 +20 XP

This program uses NEXT SENTENCE, which skips the inner print statement! Replace it with the correct keyword so that 'PROCESS COMPLETE' prints.

cobol ✓ solution verified at build time

COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the fixed code should be, then open the solution and compare. The expected output below is real — it came from compiling the solution with GnuCOBOL when this page was built.

  • Test 1 — expects "PROCESS COMPLETE\n"
Need a hint? (−25% XP)

Change NEXT SENTENCE to CONTINUE. CONTINUE acts like a pass statement without jumping to the next period.

Show solution (0 XP)
IDENTIFICATION DIVISION.
PROGRAM-ID. FIX-NEXT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 FLAG PIC 9 VALUE 1.
PROCEDURE DIVISION.
    IF FLAG = 1
        CONTINUE
    END-IF.
    DISPLAY 'PROCESS COMPLETE'.
    STOP RUN.

Challenge 2 +20 XP

Modify the program to use an 88-level condition name called `IS-SENIOR` which is true when `AGE-CODE` is 'S'. Then update the `IF` statement to use it.

cobol ✓ solution verified at build time

COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the fixed code should be, then open the solution and compare. The expected output below is real — it came from compiling the solution with GnuCOBOL when this page was built.

  • Test 1 — expects "SENIOR DISCOUNT APPLIED\n"
Need a hint? (−25% XP)

Declare `88 IS-SENIOR VALUE 'S'.` immediately under `01 AGE-CODE`, then replace `AGE-CODE = 'S'` with `IS-SENIOR`.

Show solution (0 XP)
IDENTIFICATION DIVISION.
PROGRAM-ID. SENIOR-TEST.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AGE-CODE PIC X VALUE 'S'.
   88 IS-SENIOR VALUE 'S'.
PROCEDURE DIVISION.
    IF IS-SENIOR
        DISPLAY 'SENIOR DISCOUNT APPLIED'
    END-IF.
    STOP RUN.