MainframeMaster

COBOL Data Comparison

Data comparison evaluates relationships between values using relational operators, class conditions, and sign conditions. Learn to compare numeric and alphanumeric data effectively in COBOL programs.

Relational Operators

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
*> Numeric comparisons IF CUSTOMER-BALANCE > 1000 DISPLAY 'High balance customer' END-IF. IF TRANSACTION-AMOUNT < 0 DISPLAY 'Negative transaction' END-IF. IF CUSTOMER-AGE >= 18 DISPLAY 'Adult customer' ELSE DISPLAY 'Minor customer' END-IF. IF ACCOUNT-STATUS = 'ACTIVE' DISPLAY 'Account is active' END-IF. IF CUSTOMER-ID NOT = 0 DISPLAY 'Valid customer ID' END-IF.

Use relational operators to compare values: > (greater than), < (less than), = (equal), >= (greater than or equal), <= (less than or equal), and NOT = (not equal). These work with both numeric and alphanumeric data.

String Comparison

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
*> Alphanumeric string comparison IF CUSTOMER-NAME = 'JOHN SMITH' DISPLAY 'Found John Smith' END-IF. IF CUSTOMER-CITY > 'CHICAGO' DISPLAY 'City comes after Chicago alphabetically' END-IF. *> Case-insensitive comparison IF FUNCTION UPPER-CASE(CUSTOMER-NAME) = 'JOHN SMITH' DISPLAY 'Found John Smith (case-insensitive)' END-IF. *> Partial string comparison IF CUSTOMER-NAME(1:4) = 'JOHN' DISPLAY 'Name starts with JOHN' END-IF.

Compare alphanumeric strings using relational operators. COBOL compares strings character by character using the collating sequence. Use FUNCTION UPPER-CASE for case-insensitive comparison and reference modification for partial comparisons.

Class Conditions

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
*> Test data types IF CUSTOMER-ID IS NUMERIC DISPLAY 'Customer ID is numeric' ELSE DISPLAY 'Customer ID contains non-numeric characters' END-IF. IF CUSTOMER-NAME IS ALPHABETIC DISPLAY 'Customer name contains only letters' ELSE DISPLAY 'Customer name contains non-alphabetic characters' END-IF. *> Test for specific values IF CUSTOMER-STATUS IS ALPHABETIC-UPPER DISPLAY 'Status is uppercase alphabetic' END-IF.

Use class conditions to test data types: NUMERIC tests for digits only, ALPHABETIC tests for letters only, ALPHABETIC-UPPER tests for uppercase letters only, and ALPHABETIC-LOWER tests for lowercase letters only.

Sign Conditions

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
*> Test numeric signs IF TRANSACTION-AMOUNT IS POSITIVE DISPLAY 'Credit transaction' PERFORM PROCESS-CREDIT ELSE IF TRANSACTION-AMOUNT IS NEGATIVE DISPLAY 'Debit transaction' PERFORM PROCESS-DEBIT ELSE IF TRANSACTION-AMOUNT IS ZERO DISPLAY 'Zero amount transaction' PERFORM PROCESS-ZERO-AMOUNT END-IF. *> Combined with other conditions IF BALANCE IS POSITIVE AND BALANCE > 1000 DISPLAY 'High positive balance' END-IF.

Use sign conditions to test numeric values: POSITIVE (greater than zero), NEGATIVE (less than zero), and ZERO (exactly zero). These are useful for financial calculations and business logic.

Complex Comparisons

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*> Multiple conditions with AND/OR IF CUSTOMER-AGE >= 18 AND CUSTOMER-INCOME > 30000 DISPLAY 'Eligible for premium account' END-IF. IF CUSTOMER-TYPE = 'INDIVIDUAL' OR CUSTOMER-TYPE = 'CORPORATE' DISPLAY 'Valid customer type' END-IF. *> Complex logical expressions IF (CUSTOMER-BALANCE > 1000 AND CUSTOMER-STATUS = 'ACTIVE') OR (CUSTOMER-TYPE = 'PREMIUM' AND CUSTOMER-BALANCE > 500) DISPLAY 'Eligible for special service' END-IF. *> Using NOT operator IF NOT (CUSTOMER-STATUS = 'CLOSED' OR CUSTOMER-STATUS = 'SUSPENDED') DISPLAY 'Account is active' END-IF.

Combine conditions using AND, OR, and NOT operators. Use parentheses to control evaluation order. Complex comparisons allow sophisticated business logic while maintaining readability.

Range Comparisons

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*> Numeric range checking IF CUSTOMER-AGE >= 18 AND CUSTOMER-AGE <= 65 DISPLAY 'Working age customer' END-IF. IF TRANSACTION-AMOUNT > 0 AND TRANSACTION-AMOUNT <= 10000 DISPLAY 'Standard transaction amount' END-IF. *> Using THRU for ranges IF CUSTOMER-GRADE >= 'A' AND CUSTOMER-GRADE <= 'C' DISPLAY 'Good customer grade' END-IF. *> Multiple range conditions IF (CUSTOMER-AGE >= 18 AND CUSTOMER-AGE <= 30) OR (CUSTOMER-AGE >= 50 AND CUSTOMER-AGE <= 65) DISPLAY 'Target age group' END-IF.

Check if values fall within specific ranges using AND operators. Use >= and <= for inclusive ranges. Multiple range conditions can be combined with OR for complex business rules.

Comparison with Variables

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
WORKING-STORAGE SECTION. 01 COMPARISON-VARIABLES. 05 MIN-BALANCE PIC 9(9)V99 VALUE 1000. 05 MAX-BALANCE PIC 9(9)V99 VALUE 100000. 05 TARGET-STATUS PIC X VALUE 'A'. PROCEDURE DIVISION. *> Compare with variables IF CUSTOMER-BALANCE >= MIN-BALANCE AND CUSTOMER-BALANCE <= MAX-BALANCE DISPLAY 'Balance within acceptable range' END-IF. IF CUSTOMER-STATUS = TARGET-STATUS DISPLAY 'Customer has target status' END-IF. *> Dynamic comparison values IF CURRENT-DATE > CUTOFF-DATE MOVE 'NEW-RULES' TO RULE-VERSION ELSE MOVE 'OLD-RULES' TO RULE-VERSION END-IF.

Compare data with variables for flexible business rules. Use working storage variables for comparison values that may change. This makes programs more maintainable and configurable.

Comparison Best Practices

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*> Clear, readable comparisons IF CUSTOMER-BALANCE > HIGH-BALANCE-THRESHOLD PERFORM PROCESS-HIGH-BALANCE-CUSTOMER ELSE IF CUSTOMER-BALANCE > MEDIUM-BALANCE-THRESHOLD PERFORM PROCESS-MEDIUM-BALANCE-CUSTOMER ELSE PERFORM PROCESS-LOW-BALANCE-CUSTOMER END-IF. *> Use meaningful variable names IF ACCOUNT-STATUS = ACTIVE-STATUS PERFORM PROCESS-ACTIVE-ACCOUNT END-IF. *> Document complex logic *> Check if customer is eligible for premium service IF CUSTOMER-TYPE = PREMIUM-TYPE AND CUSTOMER-BALANCE >= PREMIUM-THRESHOLD PERFORM PROCESS-PREMIUM-CUSTOMER END-IF.

Follow best practices: use clear, readable comparisons; choose meaningful variable names; document complex logic with comments; and structure comparisons for easy understanding and maintenance.