MainframeMaster

COBOL Tutorial

RELATION - Relational Conditions and Operators

Progress0 of 0 lessons

Overview

Relation conditions compare two values and return true/false. COBOL supports =,NOT =, >, <, >=, and<=. Understand numeric vs text comparison to avoid logic bugs.

📏 Analogy

Comparing numbers is like comparing weights on a scale (heavier vs lighter). Comparing strings is like dictionary order—words are ordered letter by letter.

Numeric vs Alphanumeric

Numeric Comparison

cobol
1
2
3
4
5
01 A PIC 9(3) VALUE 10. 01 B PIC 9(3) VALUE 2. IF A > B DISPLAY '10 is greater than 2' END-IF.

Numeric fields compare by value.

Alphanumeric Comparison

cobol
1
2
3
4
5
01 X PIC X(2) VALUE '10'. 01 Y PIC X(1) VALUE '2'. IF X > Y DISPLAY '"10" is greater than "2" as text' END-IF.

Text compares by collating sequence, position by position.

Collating Sequence

cobol
1
2
3
SPECIAL-NAMES. ALPHABET MY-COLLATION IS "A" THRU "Z" "a" THRU "z". ...

Define a custom collating sequence if your ordering needs differ from default.

Common Patterns

Class Test Before Compare

cobol
1
2
3
IF INPUT-VALUE IS NUMERIC AND INPUT-VALUE > 100 DISPLAY 'Valid and large' END-IF.

Validate textual numerics with CLASS NUMERIC before comparing numerically.

EVALUATE with Relations

cobol
1
2
3
4
5
EVALUATE TRUE WHEN AMOUNT < 0 DISPLAY 'Negative' WHEN AMOUNT = 0 DISPLAY 'Zero' WHEN AMOUNT > 0 DISPLAY 'Positive' END-EVALUATE.

EVALUATE TRUE is a clean way to branch by ranges.

Non-Code Examples

  • Sorting names alphabetically (alphanumeric compare)
  • Filtering transactions greater than a threshold (numeric compare)
  • Checking dates ordered as YYYYMMDD strings (lexicographic works)

Best Practices and Pitfalls

Best Practices

  • Normalize case for text comparisons if case-insensitive logic is intended
  • Trim trailing spaces for user inputs before compare
  • Convert text numbers to numeric fields before arithmetic/compare
  • Use SPECIAL-NAMES collating sequence if default order is unsuitable

Common Mistakes

MistakeProblemFix
Comparing text as numbers'10' vs '2' yields unexpected orderConvert to numeric first
Ignoring spacesTrailing spaces affect resultsTRIM or compare with edited formats
Case-sensitive surprisesDifferent order for 'a' and 'A'Normalize case as needed

Quick Reference

OperatorMeaningExample
=Equal toIF A = B
NOT =Not equal toIF X NOT = Y
>Greater thanIF AMT > 0
<Less thanIF CNT < LIMIT
>=Greater or equalIF AGE >= 18
<=Less or equalIF SCORE <= 100

Test Your Knowledge

1. Which of the following is NOT a relational operator in COBOL?

  • =
  • >
  • <=
  • ++

2. How are alphanumeric fields compared?

  • Numerically
  • Lexicographically using the collating sequence
  • Randomly
  • By length only

3. What should you check before comparing numeric text?

  • Its PICTURE has an S
  • It contains only digits and optional sign
  • Its value is 0
  • It is COMP-3

4. Which statement is true about comparing different lengths?

  • COBOL automatically pads shorter operands for alphanumeric comparison
  • COBOL truncates operands always
  • COBOL converts to floating point
  • COBOL refuses to compare

5. How to change text comparison rules globally?

  • LC_TIME
  • SPECIAL-NAMES, COLLATING SEQUENCE
  • FILE-CONTROL
  • OBJECT-COMPUTER

Frequently Asked Questions