MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL Class Conditions

Class conditions in COBOL are tests that check whether a data item contains characters of a specific class (type). The main class conditions are IS NUMERIC (checks for digits), IS ALPHABETIC (checks for letters), and IS ALPHANUMERIC (checks for letters or digits). Class conditions are essential for data validation, input verification, and ensuring data meets expected format requirements before processing in mainframe COBOL programs.

What are Class Conditions?

Class conditions test the character class (type) of data:

  • IS NUMERIC: Checks if all characters are digits (0-9)
  • IS ALPHABETIC: Checks if all characters are letters (A-Z, a-z)
  • IS ALPHANUMERIC: Checks if all characters are letters or digits
  • IS NOT: Inverts any class condition

Class conditions return true or false, allowing conditional processing based on data type.

IS NUMERIC Condition

IS NUMERIC checks if all characters in a field are numeric digits:

IS NUMERIC Syntax

cobol
1
2
3
4
5
6
7
8
IF data-item IS NUMERIC imperative-statement END-IF *> Or with NOT IF data-item IS NOT NUMERIC imperative-statement END-IF

Example: Validating Numeric Input

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
WORKING-STORAGE SECTION. 01 ZIP-CODE PIC X(10). 01 PHONE-NUMBER PIC X(14). 01 AMOUNT-INPUT PIC X(15). PROCEDURE DIVISION. MAIN-PARA. *> Validate ZIP code ACCEPT ZIP-CODE IF ZIP-CODE IS NUMERIC DISPLAY "Valid ZIP code: " ZIP-CODE *> Process as numeric ELSE DISPLAY "ERROR: ZIP code must be numeric" DISPLAY "Invalid value: " ZIP-CODE END-IF *> Validate phone number (digits only, no formatting) ACCEPT PHONE-NUMBER IF PHONE-NUMBER IS NUMERIC DISPLAY "Valid phone number: " PHONE-NUMBER ELSE DISPLAY "ERROR: Phone number contains non-numeric characters" END-IF *> Validate amount ACCEPT AMOUNT-INPUT IF AMOUNT-INPUT IS NOT NUMERIC DISPLAY "ERROR: Amount must be numeric" DISPLAY "Invalid amount: " AMOUNT-INPUT END-IF STOP RUN.

IS NUMERIC returns true only if every character is a digit (0-9). Spaces, letters, or special characters cause it to return false.

Important Notes about IS NUMERIC

  • All characters must be digits: Even one non-digit character makes IS NUMERIC false
  • Spaces are not numeric: Fields with spaces return false
  • Leading zeros are valid: "000123" is numeric
  • No decimal points: "123.45" is NOT numeric (contains a period)
  • No signs: "+123" or "-123" are NOT numeric (contains + or -)

IS ALPHABETIC Condition

IS ALPHABETIC checks if all characters are letters:

IS ALPHABETIC Syntax

cobol
1
2
3
4
5
6
7
8
IF data-item IS ALPHABETIC imperative-statement END-IF *> Or with NOT IF data-item IS NOT ALPHABETIC imperative-statement END-IF

Example: Validating Names

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
WORKING-STORAGE SECTION. 01 FIRST-NAME PIC X(30). 01 LAST-NAME PIC X(30). 01 STATE-CODE PIC X(2). PROCEDURE DIVISION. MAIN-PARA. *> Validate first name ACCEPT FIRST-NAME IF FIRST-NAME IS ALPHABETIC DISPLAY "Valid first name: " FIRST-NAME ELSE DISPLAY "ERROR: First name must contain only letters" DISPLAY "Invalid name: " FIRST-NAME END-IF *> Validate last name ACCEPT LAST-NAME IF LAST-NAME IS NOT ALPHABETIC DISPLAY "ERROR: Last name contains invalid characters" END-IF *> Validate state code (letters only) ACCEPT STATE-CODE IF STATE-CODE IS ALPHABETIC DISPLAY "Valid state code: " STATE-CODE ELSE DISPLAY "ERROR: State code must be letters only" END-IF STOP RUN.

IS ALPHABETIC returns true if all characters are letters (A-Z, a-z). Behavior with spaces may vary by compiler - some treat spaces as valid, others do not.

IS ALPHANUMERIC Condition

IS ALPHANUMERIC checks if all characters are letters or digits:

IS ALPHANUMERIC Syntax

cobol
1
2
3
4
5
6
7
8
IF data-item IS ALPHANUMERIC imperative-statement END-IF *> Or with NOT IF data-item IS NOT ALPHANUMERIC imperative-statement END-IF

Example: Validating Codes and IDs

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
WORKING-STORAGE SECTION. 01 CUSTOMER-ID PIC X(10). 01 PRODUCT-CODE PIC X(8). 01 ACCOUNT-NUMBER PIC X(12). PROCEDURE DIVISION. MAIN-PARA. *> Validate customer ID (letters and numbers only) ACCEPT CUSTOMER-ID IF CUSTOMER-ID IS ALPHANUMERIC DISPLAY "Valid customer ID: " CUSTOMER-ID ELSE DISPLAY "ERROR: Customer ID must be alphanumeric" DISPLAY "No special characters allowed" END-IF *> Validate product code ACCEPT PRODUCT-CODE IF PRODUCT-CODE IS NOT ALPHANUMERIC DISPLAY "ERROR: Product code contains invalid characters" END-IF *> Validate account number ACCEPT ACCOUNT-NUMBER IF ACCOUNT-NUMBER IS ALPHANUMERIC DISPLAY "Valid account number: " ACCOUNT-NUMBER ELSE DISPLAY "ERROR: Account number must be alphanumeric" END-IF STOP RUN.

IS ALPHANUMERIC returns true if all characters are either letters (A-Z, a-z) or digits (0-9). Special characters, spaces, or punctuation cause it to return false.

Using Class Conditions in Conditional Statements

Class conditions can be used in various conditional statements:

With IF Statements

cobol
1
2
3
4
5
6
7
8
9
IF INPUT-FIELD IS NUMERIC *> Process as numeric MOVE INPUT-FIELD TO NUMERIC-FIELD PERFORM NUMERIC-PROCESSING ELSE *> Handle non-numeric input DISPLAY "ERROR: Expected numeric value" PERFORM ERROR-HANDLING END-IF

With EVALUATE Statements

cobol
1
2
3
4
5
6
7
8
9
10
11
EVALUATE TRUE WHEN INPUT-FIELD IS NUMERIC PERFORM PROCESS-NUMERIC WHEN INPUT-FIELD IS ALPHABETIC PERFORM PROCESS-TEXT WHEN INPUT-FIELD IS ALPHANUMERIC PERFORM PROCESS-CODE WHEN OTHER DISPLAY "ERROR: Invalid character type" PERFORM ERROR-HANDLING END-EVALUATE

With Logical Operators

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
*> Multiple conditions IF ZIP-CODE IS NUMERIC AND LENGTH OF ZIP-CODE = 5 DISPLAY "Valid 5-digit ZIP code" END-IF *> Complex validation IF CUSTOMER-ID IS ALPHANUMERIC AND CUSTOMER-ID NOT = SPACES AND LENGTH OF CUSTOMER-ID >= 5 PERFORM PROCESS-CUSTOMER ELSE DISPLAY "Invalid customer ID format" END-IF

Data Validation Patterns

Common patterns for using class conditions in validation:

Pattern 1: Input Validation

cobol
1
2
3
4
5
6
7
8
9
ACCEPT USER-INPUT IF USER-INPUT IS NUMERIC MOVE USER-INPUT TO NUMERIC-FIELD PERFORM PROCESS-NUMERIC ELSE DISPLAY "ERROR: Numeric input required" DISPLAY "You entered: " USER-INPUT PERFORM REQUEST-INPUT-AGAIN END-IF

Pattern 2: Format Validation

cobol
1
2
3
4
5
6
7
*> Validate format before processing IF ACCOUNT-CODE IS ALPHANUMERIC AND LENGTH OF ACCOUNT-CODE = 8 PERFORM PROCESS-ACCOUNT ELSE DISPLAY "ERROR: Account code must be 8 alphanumeric characters" END-IF

Pattern 3: Type-Specific Processing

cobol
1
2
3
4
5
6
7
8
9
10
11
*> Route to appropriate processor based on type IF DATA-FIELD IS NUMERIC PERFORM NUMERIC-PROCESSOR ELSE IF DATA-FIELD IS ALPHABETIC PERFORM TEXT-PROCESSOR ELSE IF DATA-FIELD IS ALPHANUMERIC PERFORM CODE-PROCESSOR ELSE DISPLAY "ERROR: Invalid data type" PERFORM ERROR-HANDLER END-IF

Class Condition Behavior with Spaces

Understanding how class conditions handle spaces:

Class Condition Behavior with Spaces
ConditionField with SpacesDescription
IS NUMERICReturns FALSESpaces are not numeric digits, so fields with spaces fail the numeric test
IS ALPHABETICVaries by compilerSome compilers allow spaces in alphabetic fields, others do not
IS ALPHANUMERICReturns FALSESpaces are not letters or digits, so fields with spaces fail the test

For strict validation, consider trimming spaces before testing, or use additional validation logic.

Best Practices for Class Conditions

Follow these best practices:

  • Validate before processing: Use class conditions to validate input before performing operations
  • Provide clear error messages: When validation fails, explain what was expected
  • Test edge cases: Test with empty fields, spaces, special characters, and boundary values
  • Combine with length checks: Often combine class conditions with length validation
  • Understand compiler behavior: Test space handling with your specific compiler
  • Use appropriate condition: Choose the right class condition for your validation needs
  • Handle NOT conditions: Use IS NOT for error checking and validation
  • Document expectations: Comment code to explain what format is expected
  • Validate early: Check class conditions as soon as data is received
  • Consider trimming: Trim spaces before validation if needed for your requirements

Common Mistakes with Class Conditions

Avoid these common mistakes:

Mistake 1: Assuming Spaces are Valid

cobol
1
2
3
4
5
6
7
8
9
*> WRONG: May fail if field has spaces IF ZIP-CODE IS NUMERIC *> Process ZIP code END-IF *> CORRECT: Trim or handle spaces IF FUNCTION TRIM(ZIP-CODE) IS NUMERIC *> Process ZIP code END-IF

Mistake 2: Not Validating Before Use

cobol
1
2
3
4
5
6
7
8
9
10
11
*> WRONG: No validation MOVE INPUT-FIELD TO NUMERIC-FIELD COMPUTE RESULT = NUMERIC-FIELD * 2 *> CORRECT: Validate first IF INPUT-FIELD IS NUMERIC MOVE INPUT-FIELD TO NUMERIC-FIELD COMPUTE RESULT = NUMERIC-FIELD * 2 ELSE DISPLAY "ERROR: Invalid numeric input" END-IF

Mistake 3: Confusing Class and Type

cobol
1
2
3
4
5
6
7
8
*> Class condition checks CHARACTERS, not data type *> A PIC X field can be numeric if it contains digits 01 NUMERIC-STRING PIC X(10) VALUE '12345'. *> This is TRUE even though field is PIC X IF NUMERIC-STRING IS NUMERIC DISPLAY "Field contains numeric characters" END-IF

Explain Like I'm 5: Class Conditions

Think of class conditions like sorting toys:

  • IS NUMERIC is like checking if a box contains only number blocks - if there's even one letter block, it's not all numbers
  • IS ALPHABETIC is like checking if a box contains only letter blocks - if there's even one number block, it's not all letters
  • IS ALPHANUMERIC is like checking if a box contains only letter and number blocks - if there's a special shape block, it's not alphanumeric
  • Validation is like checking your toy box before playing - you make sure you have the right kind of toys before you start

So class conditions are like checking what kind of characters are in your data - all numbers, all letters, or letters and numbers - before you use them!

Practice Exercises

Complete these exercises to reinforce your understanding:

Exercise 1: Numeric Validation

Create a program that accepts user input and validates it is numeric. If valid, display the number; if invalid, display an error message with the invalid input.

Exercise 2: Name Validation

Create a program that accepts a first name and last name, validates both are alphabetic, and displays appropriate error messages if validation fails.

Exercise 3: Code Validation

Create a program that accepts a product code, validates it is alphanumeric and exactly 8 characters, and processes it only if valid.

Exercise 4: Type Routing

Create a program that accepts input and routes to different processors based on whether the input is numeric, alphabetic, or alphanumeric. Use EVALUATE with class conditions.

Exercise 5: Comprehensive Validation

Create a program that validates multiple fields: a numeric account number, an alphabetic state code, and an alphanumeric customer ID. Display specific error messages for each validation failure.

Test Your Knowledge

1. What does IS NUMERIC check?

  • If a field contains any numbers
  • If all characters are digits (0-9)
  • If a field is a numeric data type
  • If a field can be converted to a number

2. What does IS ALPHABETIC check?

  • If a field contains any letters
  • If all characters are letters (A-Z, a-z)
  • If a field is uppercase
  • If a field is lowercase

3. What does IS ALPHANUMERIC check?

  • If a field contains letters or numbers
  • If all characters are letters or digits
  • If a field is mixed case
  • If a field has special characters

4. How do you check if a field is NOT numeric?

  • IF field NOT NUMERIC
  • IF field IS NOT NUMERIC
  • IF NOT field NUMERIC
  • IF field <> NUMERIC

5. What happens if a field with spaces is tested with IS NUMERIC?

  • Returns true
  • Returns false (spaces are not numeric)
  • Depends on the compiler
  • Causes an error

6. What is a primary use of class conditions?

  • Performance optimization
  • Data validation and input verification
  • Memory management
  • File operations

Related Concepts

Related Pages