Relation conditions compare two values and return true/false. COBOL supports =,NOT =, >, <, >=, and<=. Understand numeric vs text comparison to avoid logic bugs.
Comparing numbers is like comparing weights on a scale (heavier vs lighter). Comparing strings is like dictionary order—words are ordered letter by letter.
1234501 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.
1234501 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.
123SPECIAL-NAMES. ALPHABET MY-COLLATION IS "A" THRU "Z" "a" THRU "z". ...
Define a custom collating sequence if your ordering needs differ from default.
123IF INPUT-VALUE IS NUMERIC AND INPUT-VALUE > 100 DISPLAY 'Valid and large' END-IF.
Validate textual numerics with CLASS NUMERIC before comparing numerically.
12345EVALUATE 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.
Mistake | Problem | Fix |
---|---|---|
Comparing text as numbers | '10' vs '2' yields unexpected order | Convert to numeric first |
Ignoring spaces | Trailing spaces affect results | TRIM or compare with edited formats |
Case-sensitive surprises | Different order for 'a' and 'A' | Normalize case as needed |
Operator | Meaning | Example |
---|---|---|
= | Equal to | IF A = B |
NOT = | Not equal to | IF X NOT = Y |
> | Greater than | IF AMT > 0 |
< | Less than | IF CNT < LIMIT |
>= | Greater or equal | IF AGE >= 18 |
<= | Less or equal | IF SCORE <= 100 |
1. Which of the following is NOT a relational operator in COBOL?
2. How are alphanumeric fields compared?
3. What should you check before comparing numeric text?
4. Which statement is true about comparing different lengths?
5. How to change text comparison rules globally?