COBOL Conditions & IF
IF chooses between two paths. Since COBOL-85, an IF is closed with END-IF, which makes nested decisions safe
to write.
IF condition
statements
[ELSE
statements]
END-IFRelation conditions
You can write comparisons with symbols or with words:
| Symbol | Words |
|---|---|
= |
IS EQUAL TO |
> |
IS GREATER THAN |
< |
IS LESS THAN |
>= |
IS GREATER THAN OR EQUAL TO |
<= |
IS LESS THAN OR EQUAL TO |
NOT = / <> |
IS NOT EQUAL TO |
IDENTIFICATION DIVISION.
PROGRAM-ID. IF-DEMO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-BALANCE PIC S9(5)V99 VALUE 250.00.
01 WS-WITHDRAW PIC 9(5)V99 VALUE 300.00.
PROCEDURE DIVISION.
IF WS-WITHDRAW > WS-BALANCE
DISPLAY "Declined: insufficient funds"
ELSE
SUBTRACT WS-WITHDRAW FROM WS-BALANCE
DISPLAY "Approved"
END-IF
IF WS-BALANCE IS GREATER THAN OR EQUAL TO 100
DISPLAY "Balance is at least 100"
END-IF
STOP RUN.Declined: insufficient funds
Balance is at least 100
Comparing text
Alphanumeric items are compared character by character. If the lengths differ, the shorter one is padded with
spaces first, so "ABC" equals "ABC ". That is why padded fields compare as you would hope:
IDENTIFICATION DIVISION.
PROGRAM-ID. TEXT-COMPARE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CITY PIC X(10) VALUE "Paris".
01 WS-CODE PIC X(3) VALUE "abc".
PROCEDURE DIVISION.
IF WS-CITY = "Paris"
DISPLAY "Padding is ignored in comparisons"
END-IF
IF WS-CODE NOT = "ABC"
DISPLAY "Comparisons are case-sensitive"
END-IF
IF "Apple" < "Banana"
DISPLAY "Apple sorts before Banana"
END-IF
STOP RUN.Padding is ignored in comparisons
Comparisons are case-sensitive
Apple sorts before Banana
Combining conditions: AND, OR, NOT
NOT is evaluated first, then AND, then OR. Use parentheses whenever you mix AND and OR.
COBOL also allows abbreviated combined conditions: IF WS-AGE > 17 AND < 65 repeats the subject WS-AGE,
and IF WS-COLOR = "RED" OR "BLUE" repeats both subject and operator.
IDENTIFICATION DIVISION.
PROGRAM-ID. COMBINED.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-AGE PIC 999 VALUE 34.
01 WS-MEMBER PIC X VALUE "Y".
01 WS-COLOR PIC X(5) VALUE "BLUE".
PROCEDURE DIVISION.
IF WS-AGE >= 18 AND WS-MEMBER = "Y"
DISPLAY "Adult member"
END-IF
IF WS-AGE > 17 AND < 65
DISPLAY "Working age"
END-IF
IF WS-COLOR = "RED" OR "BLUE"
DISPLAY "Primary colour"
END-IF
IF NOT (WS-AGE < 18 OR WS-MEMBER = "N")
DISPLAY "Same as the first test"
END-IF
STOP RUN.Adult member
Working age
Primary colour
Same as the first test
Class and sign conditions
| Condition | True when |
|---|---|
IS NUMERIC |
The item contains only digits (and a valid sign, if signed) |
IS ALPHABETIC |
Only letters and spaces |
IS ALPHABETIC-UPPER / -LOWER |
Only upper-case / lower-case letters and spaces |
IS POSITIVE / NEGATIVE / ZERO |
The sign of a numeric value |
IS NUMERIC is the standard way to validate input before moving text into a numeric field:
IDENTIFICATION DIVISION.
PROGRAM-ID. CLASS-TEST.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-INPUT-1 PIC X(4) VALUE "1234".
01 WS-INPUT-2 PIC X(4) VALUE "12A4".
01 WS-NAME PIC X(6) VALUE "Olga".
01 WS-PROFIT PIC S9(4) VALUE -20.
PROCEDURE DIVISION.
IF WS-INPUT-1 IS NUMERIC
DISPLAY WS-INPUT-1 " is numeric"
END-IF
IF WS-INPUT-2 IS NOT NUMERIC
DISPLAY WS-INPUT-2 " is not numeric"
END-IF
IF WS-NAME IS ALPHABETIC
DISPLAY WS-NAME " is alphabetic"
END-IF
IF WS-PROFIT IS NEGATIVE
DISPLAY "We made a loss"
END-IF
STOP RUN.1234 is numeric
12A4 is not numeric
Olga is alphabetic
We made a loss
Nested IFs and the period trap
With END-IF every IF has a clear end, and nesting is easy to read. In old code without END-IF, a period
ends all open IFs at once, and an accidental period inside an IF silently moves the following
statements outside it. Modern style: no periods inside a paragraph, always END-IF.
IDENTIFICATION DIVISION.
PROGRAM-ID. NESTED-IF.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-SCORE PIC 999 VALUE 72.
PROCEDURE DIVISION.
IF WS-SCORE >= 50
IF WS-SCORE >= 80
DISPLAY "Pass with distinction"
ELSE
DISPLAY "Pass"
END-IF
ELSE
DISPLAY "Fail"
END-IF
STOP RUN.Pass
Tip:
CONTINUEis a statement that does nothing. It is handy as a placeholder:IF WS-OK = "Y" CONTINUE ELSE PERFORM REPORT-ERROR END-IF.NEXT SENTENCElooks similar but jumps past the next period, which can skip far more than you expect: avoid it.
Test yourself
Is "ABC" equal to a PIC X(6) item containing "ABC "?
- No, the lengths differ
- Yes, the shorter operand is padded with spaces before comparing
- Only with FUNCTION TRIM
- Only in free format
What does IF WS-AGE > 17 AND < 65 mean?
- A syntax error
- WS-AGE > 17 AND WS-AGE < 65
- WS-AGE > 17 AND 17 < 65
- WS-AGE between 17 and 65 inclusive
Which condition checks that a text field contains only digits?
- IS DIGIT
- IS NUMERIC
- IS INTEGER
- IS ZERO