Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 38 ยท Procedures and Control Flow

IF, ELSE and Conditions in COBOL

Master COBOL decisions: IF/ELSE/END-IF, relational, class, sign and condition-name tests, AND/OR/NOT, abbreviated conditions, nested IFs, CONTINUE and the period trap.

Programs decide. COBOL's IF is more flexible than it first looks, because a condition can compare values, test whether a field is numeric, check the sign of a number, or use a named level-88 condition. This lesson covers all of them and the one famous trap: the period.

The IF statement

COBOL
           IF BALANCE < 0
               DISPLAY "Overdrawn"
           ELSE
               DISPLAY "OK"
           END-IF
  • THEN is optional (IF A > B THEN ...).
  • ELSE is optional.
  • Always close with END-IF. The period belongs at the end of the whole sentence, not after each IF.
  • Statements inside may be any number; no braces are needed.
  • To do nothing in a branch, write CONTINUE (not NEXT SENTENCE, which jumps past the next period and surprises people).
COBOL
           IF STATUS-CODE = "A"
               CONTINUE
           ELSE
               PERFORM REPORT-ERROR
           END-IF

Relational conditions

Symbol Words Meaning
= EQUAL TO equal
NOT = NOT EQUAL TO not equal (there is no != or <>)
> GREATER THAN greater
< LESS THAN less
>= GREATER THAN OR EQUAL TO greater or equal
<= LESS THAN OR EQUAL TO less or equal

Comparison rules matter:

  • Numeric vs numeric: compared by value, so 007 equals 7 and decimal points line up.
  • Alphanumeric vs alphanumeric: compared character by character using the machine's collating sequence (ASCII on Linux, EBCDIC on mainframes, where digits sort after letters). A shorter operand is padded with spaces, so "AB" equals "AB ".
  • Comparing a numeric field to an alphanumeric one is allowed only in limited cases; keep types consistent.

Combined conditions

AND, OR and NOT combine conditions. NOT binds tightest, then AND, then OR; use parentheses to be clear.

COBOL
           IF AGE >= 18 AND (COUNTRY = "DE" OR COUNTRY = "FR")
               PERFORM ALLOW-ENTRY
           END-IF

COBOL allows abbreviated conditions, where the left subject and the operator are carried forward:

COBOL
           IF X > 1 AND < 10           *> same as X > 1 AND X < 10
           IF CODE = "A" OR "B" OR "C" *> same as CODE = "A" OR CODE = "B" OR CODE = "C"

They are compact, but the expansion rules are subtle (they combine with NOT in surprising ways), so many shops forbid anything beyond the simple OR list.

Python Abbreviated conditions (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Class conditions

Test True when the field contains
IS NUMERIC digits only (a numeric item: digits and a valid sign)
IS ALPHABETIC letters and spaces only
IS ALPHABETIC-LOWER / -UPPER lowercase / uppercase letters and spaces
IS class-name only characters of your own CLASS (see environment division)
COBOL
           ACCEPT AMOUNT-TEXT
           IF AMOUNT-TEXT IS NUMERIC
               MOVE AMOUNT-TEXT TO AMOUNT
           ELSE
               DISPLAY "Please type digits only"
           END-IF

For an alphanumeric field, NUMERIC is true only if every character is a digit: no decimal point, no minus sign, no spaces. That is why you test before you move text into a number.

Sign conditions

COBOL
           IF BALANCE IS POSITIVE ...
           IF BALANCE IS NEGATIVE ...
           IF BALANCE IS ZERO ...

They are equivalent to > 0, < 0 and = 0. NOT NEGATIVE includes zero.

Condition names

COBOL
           IF ACCOUNT-ACTIVE AND NOT ON-HOLD
               PERFORM PROCESS-ACCOUNT
           END-IF

If the 88-level names are defined well, an IF becomes a readable sentence.

Python Class conditions (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Nested IFs and the period trap

COBOL
           IF A > B
               IF B > C
                   DISPLAY "A > B > C"
               ELSE
                   DISPLAY "A > B, B not > C"
               END-IF
           ELSE
               DISPLAY "A not > B"
           END-IF

An ELSE belongs to the nearest unmatched IF. With END-IF the structure is exact. Without it, a single period closes everything:

COBOL
           IF A > B
               IF B > C
                   DISPLAY "yes".
               DISPLAY "this runs even when A <= B".

Here the first period ended both IF statements, so the next line is always executed. This is the most common beginner bug and the reason for the rule "use scope terminators and put one period at the end of each paragraph".

For many mutually exclusive cases, an EVALUATE is clearer than a chain of nested IF statements.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What belongs at the end of an IF block in modern COBOL?

    END-IF is the scope terminator. A period would end the whole sentence, closing every open nested statement.
  2. 2. How is a not-equal test written?

    COBOL uses NOT = (or NOT EQUAL TO). The symbols != and <> do not exist.
  3. 3. IF CODE = "A" OR "B" is an example of what?

    The subject CODE and the operator = are carried forward, so it means CODE = "A" OR CODE = "B".
  4. 4. An alphanumeric field holds "12.5". What does IS NUMERIC return?

    For an alphanumeric item, NUMERIC means every character is a digit; the decimal point makes it false.

Summary

  • IF cond ... [ELSE ...] END-IF; use CONTINUE for an empty branch.
  • Relational operators include NOT =; numeric compares by value, alphanumeric by collating sequence with space padding.
  • AND, OR, NOT, parentheses and abbreviated conditions combine tests.
  • Class (NUMERIC, ALPHABETIC), sign (POSITIVE, NEGATIVE, ZERO) and 88-level condition names make tests readable.
  • A stray period closes all open IFs: always use END-IF.

Next: EVALUATE: Multi-Way Decisions