IF, ELSE and END-IF
IF runs statements only when a condition is true, and ELSE gives the
alternative. END-IF closes the whole thing:
IDENTIFICATION DIVISION.
PROGRAM-ID. RELOPS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-BALANCE PIC S9(5)V99 VALUE 250.00.
01 WS-LIMIT PIC 9(5)V99 VALUE 250.
01 WS-NAME PIC X(10) VALUE "SMITH".
PROCEDURE DIVISION.
IF WS-BALANCE >= WS-LIMIT
DISPLAY "AT OR OVER LIMIT"
ELSE
DISPLAY "UNDER LIMIT"
END-IF
IF WS-BALANCE IS GREATER THAN 1000
DISPLAY "BIG"
END-IF
IF WS-NAME = "SMITH"
DISPLAY "TRAILING SPACES DON'T MATTER"
END-IF
IF WS-NAME NOT = "JONES"
DISPLAY "NOT JONES"
END-IF
STOP RUN.
Output:
AT OR OVER LIMIT
TRAILING SPACES DON'T MATTER
NOT JONES
Relational operators
| Symbol | Words |
|---|---|
= |
EQUAL TO |
> / < |
GREATER THAN / LESS THAN |
>= / <= |
GREATER THAN OR EQUAL TO / LESS THAN OR EQUAL TO |
NOT = |
NOT EQUAL TO |
IS is optional noise (IF WS-A IS > 5). Numeric comparisons are by
value: 250.00 equals 250 whatever the pictures. Alphanumeric comparisons
pad the shorter side with spaces, which is why "SMITH" equals a
PIC X(10) holding SMITH plus five spaces.
Nesting
An IF can contain another IF. Indent each level and close each one with
its own END-IF:
IF WS-AMOUNT > 500
DISPLAY "OVER LIMIT"
ELSE
IF WS-AMOUNT > WS-BALANCE
DISPLAY "NO FUNDS"
ELSE
SUBTRACT WS-AMOUNT FROM WS-BALANCE
END-IF
END-IF
The period trap
Before 1985, COBOL had no END-IF. An IF ended at the next period,
and some shops still write in that style. Mix the two and you get bugs
that look fine at a glance:
IDENTIFICATION DIVISION.
PROGRAM-ID. PERIODS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-BALANCE PIC S9(5)V99 VALUE 250.00.
01 WS-FEE PIC 9(3)V99 VALUE 0.
PROCEDURE DIVISION.
IF WS-BALANCE < 100
DISPLAY "LOW BALANCE"
MOVE 5 TO WS-FEE.
DISPLAY "FEE CHARGED".
DISPLAY "FEE IS " WS-FEE.
STOP RUN.
Output:
FEE CHARGED
FEE IS 000.00
The balance isn't low, yet "FEE CHARGED" appears. The period after
MOVE 5 TO WS-FEE ended the IF, so the next DISPLAY runs
unconditionally. The indentation lies; the compiler ignores it.
House rule for this course
Close every IF with END-IF, and use a period only at the end of a
paragraph. A stray period inside an IF is one of the classic causes
of production incidents in COBOL.
On the job
When you change old period-style code, don't just add a line inside an
IF — check where the sentence really ends. Many teams convert a
paragraph to END-IF style when they touch it, so the next person
can't fall into the same trap.
Your task
An ATM authorisation program reads the account balance (may be
negative, e.g. -20.00) and the withdrawal amount, one per line.
Apply these rules in order:
- Over 500.00 → display
DECLINED: DAILY LIMIT. - Otherwise, more than the balance → display
DECLINED: INSUFFICIENT FUNDS. - Otherwise subtract the amount from the balance and display
APPROVED.
In every case finish with BALANCE: and the balance through WS-OUT:
APPROVED
BALANCE: 150.00
(for balance 250.00, amount 100). Use nested IF/ELSE with
END-IF.