Class Tests
Part of the Decisions and Loops section of Coddy's COBOL Programming journey. Lesson 5 of 20.
A class test checks what kind of characters a field holds. NUMERIC is true when every character is a digit:
IF ACCT-IN IS NUMERIC
DISPLAY "Valid account"
END-IF.ACCEPT into a PIC 9 field does not reject letters: GnuCOBOL stores a number anyway, and abc becomes 000. So programs accept into PIC X, test it, then MOVE it to a number:
ACCEPT QTY-IN
IF QTY-IN IS NUMERIC
MOVE QTY-IN TO QTY
END-IF.Spaces are characters too. 123 typed into a PIC X(6) field is 123 and three spaces, so it is not NUMERIC.
ALPHABETIC is true for letters and spaces; ALPHABETIC-UPPER and ALPHABETIC-LOWER allow one case of letters, and spaces. A blank field passes all three. NOT reverses a test:
IF NAME-IN IS NOT ALPHABETICChallenge
EasyRead ACCT-IN. Print Valid account when all 6 characters are digits and Invalid account otherwise.
Example input, one line per ACCEPT: 123456.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. ACCTNO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ACCT-IN PIC X(6).
PROCEDURE DIVISION.
ACCEPT ACCT-IN.
*> Print Valid account or Invalid account
STOP RUN.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.