IF and ELSE

Control flow in COBOL starts with IF, and the shape of the statement has two traps that a programmer from another language walks into: the period, which ends a statement group rather than a line, and the missing END-IF, which lets the next statement into the branch. Both are shown below with real runs.

IF and END-IF

An IF takes a condition and one or more statements. The statements that belong to it end at END-IF, and an ELSE may put a second group in front of it:

IDENTIFICATION DIVISION. PROGRAM-ID. IFELSE. DATA DIVISION. WORKING-STORAGE SECTION. 01 SCORE-F PIC 99 VALUE 85. 01 GRADE-F PIC X VALUE SPACE. PROCEDURE DIVISION. IF SCORE-F >= 50 IF SCORE-F >= 90 MOVE "A" TO GRADE-F DISPLAY "pass, grade A" ELSE MOVE "B" TO GRADE-F DISPLAY "pass, grade B" END-IF ELSE MOVE "F" TO GRADE-F DISPLAY "fail" END-IF. DISPLAY "score " SCORE-F " grade " GRADE-F. STOP RUN.

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

pass, grade B
score 85 grade B

The condition operators are =, <, >, <=, >= and NOT =, and each has a spelled form: EQUAL TO, LESS THAN, GREATER THAN, LESS THAN OR EQUAL TO, GREATER THAN OR EQUAL TO and NOT EQUAL TO. The two forms are the same statement to the compiler.

Nested IF and the ELSE that belongs to it

The ELSE on the inner level belongs to the inner IF, because it pairs with the nearest open IF above it. The second ELSE, aligned with the outer END-IF, belongs to the outer one; the indentation in the listing is for the reader and means nothing to the compiler.

A score of 85 is at least 50 and below 90, so the program took the inner ELSE. The grade was moved into GRADE-F before the message was printed, and both lines agree.

The branch that did not run

With a score below the pass mark the other branch runs, and nothing of the first one does:

IDENTIFICATION DIVISION. PROGRAM-ID. FAIL. DATA DIVISION. WORKING-STORAGE SECTION. 01 SCORE-F PIC 99 VALUE 45. PROCEDURE DIVISION. IF SCORE-F >= 50 DISPLAY "pass" ELSE DISPLAY "fail" END-IF. STOP RUN.

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

fail

A branch may hold one statement or twenty; the group is what END-IF closes, and the whole statement ends with the one period that follows it. That period is optional at the end of a paragraph, because the paragraph header ends the sentence as well, which is the subject of the lesson on paragraphs.

A period ends more than a sentence

A period closes the whole sentence, so every IF that is still open is closed by it. Here the writer meant two statements inside the branch and put a period after the first one:

IDENTIFICATION DIVISION. PROGRAM-ID. BADPER. DATA DIVISION. WORKING-STORAGE SECTION. 01 QTY-F PIC 9 VALUE 0. 01 FLAG-F PIC X VALUE "N". PROCEDURE DIVISION. IF QTY-F > 0 MOVE "Y" TO FLAG-F. DISPLAY "stock available". DISPLAY "flag " FLAG-F. STOP RUN.

cobc -x -free -Wall -o badper badperiod.cob
./badper

stock available
flag N

The quantity is zero and the flag stayed N, yet the program announced stock. The period after the MOVE closed the IF, so the DISPLAY that follows it is unconditional; the compiler has nothing to report, because both forms are legal COBOL. The fix is END-IF:

IDENTIFICATION DIVISION. PROGRAM-ID. GOODPER. DATA DIVISION. WORKING-STORAGE SECTION. 01 QTY-F PIC 9 VALUE 0. 01 FLAG-F PIC X VALUE "N". PROCEDURE DIVISION. IF QTY-F > 0 MOVE "Y" TO FLAG-F DISPLAY "stock available" END-IF. DISPLAY "flag " FLAG-F. STOP RUN.

cobc -x -free -Wall -o goodper goodperiod.cob
./goodper

flag N

Same data, one line of output instead of two. The rule this course keeps is to write END-IF always and to use a period only at the end of a paragraph. It costs one line and removes a class of bug that no compiler in this image reports.

Class conditions

Besides comparing values, an IF can ask what kind of characters a field holds: IS NUMERIC, IS ALPHABETIC and IS ALPHABETIC-UPPER test every byte:

IDENTIFICATION DIVISION. PROGRAM-ID. CLASSIF. DATA DIVISION. WORKING-STORAGE SECTION. 01 CODE-F PIC X(4) VALUE "12AB". 01 NAME-F PIC X(4) VALUE SPACES. 01 WORD-F PIC X(4) VALUE "Ada". 01 AMT-F PIC 9(3) VALUE 7. 01 BLANK-F PIC X(4) VALUE SPACES. PROCEDURE DIVISION. IF CODE-F IS NUMERIC DISPLAY "code is numeric" ELSE DISPLAY "code is not numeric" END-IF. IF WORD-F IS ALPHABETIC DISPLAY "word is alphabetic" ELSE DISPLAY "word is not alphabetic" END-IF. IF NAME-F IS ALPHABETIC DISPLAY "spaces are alphabetic" ELSE DISPLAY "spaces are not alphabetic" END-IF. IF BLANK-F = SPACES DISPLAY "blank equals SPACES" END-IF. IF AMT-F IS NUMERIC DISPLAY "amount is numeric" END-IF. STOP RUN.

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

code is not numeric
word is alphabetic
spaces are alphabetic
blank equals SPACES
amount is numeric

The fourth line is the surprise: a field of spaces is alphabetic in COBOL, because the space is a letter of the alphabet in the standard. A test for an empty field uses = SPACES, never IS ALPHABETIC. The next lesson gives these tests a name in the data division, so that a condition can be written once and used everywhere.

Exercises

  1. Change SCORE-F to 95 and to 45 and run the first program each time. Which branch runs, and what does the last line print?
  2. Move the inner END-IF of the nested program one line down and see what the compiler says. Which statement does the ELSE belong to now?
  3. Add a third statement to the branch in the period program, after the DISPLAY, and leave the period where it is. How many lines print when the quantity is zero?
  4. Write a condition that is true for a letter or a space but not for a digit, and test it on "AB ", "AB1" and " ".

Questions

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

  1. Which IF does an ELSE belong to?

    Answer

    The nearest IF above it that is still open

    Indentation is for the reader; the ELSE pairs with the closest unterminated IF.

  2. What does a period do inside an IF?

    Answer

    It ends the whole sentence, so every open IF is closed there

    A period terminates the sentence, which closes the IF, so statements after it are unconditional.

  3. What is true of a field that holds only spaces?

    Answer

    IS ALPHABETIC is true and = SPACES is true

    The space is a letter of the COBOL alphabet, so a blank field passes both tests; a test for an empty field must use = SPACES.

  4. Why is END-IF worth writing even where a period would work?

    Answer

    It closes the IF where the writer meant, so a later period cannot close it early

    The compiler accepts a period, and a period after the first statement of a branch silently makes the rest unconditional.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 5. PROCEDURE DIVISION. IF N-F > 3 DISPLAY "big" ELSE DISPLAY "small" END-IF. STOP RUN.

    Answer

    big

    Five is greater than three, so the first branch runs and the ELSE is skipped.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 5. PROCEDURE DIVISION. IF N-F > 3 DISPLAY "big". DISPLAY "checked". STOP RUN.

    Answer

    big / checked

    The period after the first DISPLAY closes the IF, so the second DISPLAY runs as well and both lines print.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 1. PROCEDURE DIVISION. IF N-F > 3 DISPLAY "big". DISPLAY "checked". STOP RUN.

    Answer

    checked

    The condition is false and the period closed the branch after the first statement, so only the unconditional DISPLAY runs.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 F-F PIC X(3) VALUE " ". PROCEDURE DIVISION. IF F-F IS ALPHABETIC DISPLAY "alphabetic" ELSE DISPLAY "not alphabetic" END-IF. STOP RUN.

    Answer

    alphabetic

    A space is a letter of the alphabet in COBOL, so a blank field is alphabetic.

  9. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q9. DATA DIVISION. WORKING-STORAGE SECTION. 01 C-F PIC X(4) VALUE "12AB". 01 N-F PIC 9(2) VALUE 12. PROCEDURE DIVISION. IF C-F IS NUMERIC DISPLAY "numeric" ELSE DISPLAY "text" END-IF. IF N-F IS NUMERIC DISPLAY "number field" END-IF. STOP RUN.

    Answer

    text / number field

    The letters in the first field fail the class test, while the numeric field is numeric by its picture.

  10. A program prints "stock available" for a quantity of zero. The branch holds a MOVE, a period and then a DISPLAY. What is wrong?

    Answer

    The period closed the IF, so the DISPLAY is unconditional and should be END-IF

    That is exactly the bug on this page: the period ended the sentence after the MOVE and the message printed with the flag still N.

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