MODULE 4 · CONDITIONS · 2/8

Class and sign conditions

12 min30 XPExercise

Input data can't be trusted. A field that should hold an account number may contain spaces, letters or binary garbage from a bad upstream file, and doing arithmetic on it can abend the job. Class conditions let you test what kind of characters a field holds before you use it.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. CLASSES.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-ID       PIC X(6).
       01  WS-NAME     PIC X(8).
       01  WS-AMT      PIC S9(5)V99 VALUE -0.01.
       PROCEDURE DIVISION.
           MOVE "004512" TO WS-ID
           IF WS-ID IS NUMERIC DISPLAY "004512 IS NUMERIC" END-IF
           MOVE "4512" TO WS-ID
           IF WS-ID IS NOT NUMERIC
               DISPLAY "4512 + 2 SPACES IS NOT NUMERIC"
           END-IF
           MOVE "VAN DYKE" TO WS-NAME
           IF WS-NAME IS ALPHABETIC
               DISPLAY "VAN DYKE IS ALPHABETIC"
           END-IF
           MOVE "O'NEIL" TO WS-NAME
           IF WS-NAME IS NOT ALPHABETIC
               DISPLAY "O'NEIL IS NOT ALPHABETIC"
           END-IF
           MOVE SPACES TO WS-NAME
           IF WS-NAME IS ALPHABETIC
               DISPLAY "ALL SPACES IS ALPHABETIC"
           END-IF
           IF WS-AMT IS NEGATIVE DISPLAY "-0.01 IS NEGATIVE" END-IF
           STOP RUN.

Output:

004512 IS NUMERIC
4512 + 2 SPACES IS NOT NUMERIC
VAN DYKE IS ALPHABETIC
O'NEIL IS NOT ALPHABETIC
ALL SPACES IS ALPHABETIC
-0.01 IS NEGATIVE

Class conditions

Test True when every character is…
NUMERIC a digit 0–9 (for a signed numeric field, a valid sign is allowed too)
ALPHABETIC a letter A–Z, a–z, or a space
ALPHABETIC-UPPER A–Z or space
ALPHABETIC-LOWER a–z or space

Each can be negated with NOT. Watch out for the edge cases:

  • Spaces are not numeric. "4512 " fails NUMERIC. Short input typed into a PIC X(6) is padded with spaces, so it fails too.
  • Spaces are alphabetic. A completely blank name passes ALPHABETIC. If blank isn't acceptable, test = SPACES separately.
  • A minus sign or decimal point in a PIC X field makes it non-numeric. NUMERIC checks characters, not "does this look like a number".

Sign conditions

For numeric fields you can ask about the sign directly:

           IF WS-AMT IS POSITIVE ...
           IF WS-AMT IS NEGATIVE ...
           IF WS-AMT IS ZERO ...
           IF WS-AMT IS NOT POSITIVE ...

POSITIVE means greater than zero — zero is neither positive nor negative. NOT POSITIVE is therefore "zero or negative". These read better than > 0 / < 0 and are common in financial code (credits versus debits).

On the job

IF WS-AMOUNT NOT NUMERIC before arithmetic is the standard guard against the dreaded S0C7 data exception — the abend you get on z/OS when packed or zoned arithmetic meets invalid data. Validation programs that check every field of every input record with class tests are a normal part of batch systems.

Your task

A customer-maintenance job validates input from a data-entry screen. Read three lines:

  1. an account ID into WS-ACCT-ID (PIC X(6))
  2. a surname into WS-SURNAME (PIC X(12))
  3. an adjustment amount into WS-AMOUNT (PIC S9(7)V99)

Display three result lines:

  • ACCOUNT ID: VALID if the ID is all digits, otherwise ACCOUNT ID: INVALID
  • SURNAME: MISSING if the surname is blank; otherwise SURNAME: VALID if it's alphabetic, else SURNAME: INVALID
  • AMOUNT: CREDIT if positive, AMOUNT: DEBIT if negative, AMOUNT: ZERO if zero

Use class conditions (NUMERIC, ALPHABETIC) and sign conditions (POSITIVE, NEGATIVE), not > 0.

fixed format
Run your program to see its output here. The first visible test's input and datasets are used.
Submit to grade your program against every test.