TExcelTAD
Home › COBOL › COBOL Level 88 Condition Names

COBOL Level 88 Condition Names

A condition name (level 88) gives a meaningful name to one or more values of a data item. Instead of IF WS-STATUS = "A" you write IF ACCOUNT-ACTIVE. It is one of COBOL's best features for readable code.

cobol
       01  WS-STATUS          PIC X.
           88  ACCOUNT-ACTIVE     VALUE "A".
           88  ACCOUNT-CLOSED     VALUE "C".
           88  ACCOUNT-FROZEN     VALUE "F".

The 88 entries take no storage: they are names for tests on the item directly above them.

Testing and setting

IF condition-name is true when the item has one of the listed values. SET condition-name TO TRUE moves the (first) value into the item, so you never have to remember which code means what:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. LEVEL-88.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-STATUS          PIC X       VALUE "A".
           88  ACCOUNT-ACTIVE             VALUE "A".
           88  ACCOUNT-CLOSED             VALUE "C".
           88  ACCOUNT-FROZEN             VALUE "F".
       PROCEDURE DIVISION.
           IF ACCOUNT-ACTIVE
               DISPLAY "Account is active (code " WS-STATUS ")"
           END-IF
           SET ACCOUNT-FROZEN TO TRUE
           DISPLAY "Status code is now " WS-STATUS
           IF NOT ACCOUNT-ACTIVE
               DISPLAY "Account can no longer be used"
           END-IF
           STOP RUN.
Account is active (code A)
Status code is now F
Account can no longer be used

Several values and ranges

A condition name can list several values and ranges with THRU (or THROUGH):

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. RANGES-88.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-SCORE           PIC 999.
           88  GRADE-A                    VALUE 90 THRU 100.
           88  GRADE-B                    VALUE 80 THRU 89.
           88  PASSED                     VALUE 50 THRU 100.
           88  VALID-SCORE                VALUE 0 THRU 100.
       01  WS-DAY             PIC X(3).
           88  WEEKEND                    VALUE "SAT" "SUN".
       PROCEDURE DIVISION.
           MOVE 85 TO WS-SCORE
           IF GRADE-B DISPLAY "85 is a B" END-IF
           IF PASSED DISPLAY "85 passed" END-IF
           MOVE 120 TO WS-SCORE
           IF NOT VALID-SCORE DISPLAY "120 is not a valid score" END-IF
           MOVE "SUN" TO WS-DAY
           IF WEEKEND DISPLAY "Sunday is a weekend day" END-IF
           STOP RUN.
85 is a B
85 passed
120 is not a valid score
Sunday is a weekend day

Notice that the conditions overlap: GRADE-B and PASSED are both true for 85. That's allowed; each 88 is an independent test.

Flags and switches

Level 88s make yes/no flags self-explaining. This is the standard way to control loops, such as an end-of-file switch:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. FLAGS-88.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-EOF-FLAG        PIC X       VALUE "N".
           88  END-OF-DATA                VALUE "Y".
           88  MORE-DATA                  VALUE "N".
       01  WS-ROWS            PIC 9       VALUE 0.
       PROCEDURE DIVISION.
           PERFORM UNTIL END-OF-DATA
               ADD 1 TO WS-ROWS
               DISPLAY "Processing row " WS-ROWS
               IF WS-ROWS = 3
                   SET END-OF-DATA TO TRUE
               END-IF
           END-PERFORM
           SET MORE-DATA TO TRUE
           DISPLAY "Flag reset to " WS-EOF-FLAG
           STOP RUN.
Processing row 1
Processing row 2
Processing row 3
Flag reset to N

Tip: Write the code with condition names only. If the meaning of "Y" ever changes, you change one VALUE clause, and every IF and SET follows automatically.

Validating input codes

Condition names are perfect for validating a field against a list of allowed codes:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. VALIDATE-88.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-CURRENCY        PIC X(3).
           88  SUPPORTED-CURRENCY         VALUE "EUR" "USD" "GBP"
                                                "JPY".
       PROCEDURE DIVISION.
           MOVE "USD" TO WS-CURRENCY
           PERFORM CHECK-CURRENCY
           MOVE "XYZ" TO WS-CURRENCY
           PERFORM CHECK-CURRENCY
           STOP RUN.
       CHECK-CURRENCY.
           IF SUPPORTED-CURRENCY
               DISPLAY WS-CURRENCY ": accepted"
           ELSE
               DISPLAY WS-CURRENCY ": rejected"
           END-IF.
USD: accepted
XYZ: rejected

Test yourself

How much storage does an 88-level item use?

  1. One byte
  2. The same as its parent
  3. None; it is a named test on the item above it
  4. Two bytes

What does SET ACCOUNT-CLOSED TO TRUE do?

  1. Makes ACCOUNT-CLOSED a boolean variable
  2. Moves the condition's (first) value into its parent item
  3. Tests the condition
  4. Nothing at run time

Which entry makes IF TEEN true for ages 13 to 19?

  1. 88 TEEN VALUE 13 THRU 19.
  2. 88 TEEN VALUE 13 TO 19.
  3. 88 TEEN PIC 13-19.
  4. 77 TEEN VALUE 13, 19.