You are here: Home > Knowledge Refreshers

KR edition 175,178


KR-175 : COBOL Puzzle 

Hi everyone,

A COBOL puzzle to start the week...idea for puzzle triggered by Viswa

We have the following code snippet:

01 WS-NUM-TEST PIC +9(2).99. 
01 WS-NUM-TEST2 PIC -9(2).99.

MOVE 19.99 TO WS-NUM-TEST 
MOVE -9.99 TO WS-NUM-TEST2

IF WS-NUM-TEST > WS-NUM-TEST2 
   DISPLAY "+VE GREATER THAN -VE" 
ELSE 
   DISPLAY "+VE LESS THAN -VE" 
END-IF

What do you think the output will be? Can you justify the result?

Clue: Remember it is a numeric edited picture clause. The first question that arises is can we compare such values? Yes; comparing edited values is permitted; it won't yield a compile error.


KR-178 : COBOL Puzzle (answer)

Continuing with answer to the question ....

One might feel that the result is "+ve GREATER" but it isn't :-) -  Let's see why?

Our picture clauses define WS-NUM-TEST and TEST2 as numeric edited variables, i.e. they aren't really numeric. In fact these are considered like alphanumeric variables by COBOL; 

The + and - are stored as characters. When comparing characters, COBOL uses the EBCDIC values for comparison.

+ has value of 4E in hex
- has value of 60 in hex.

So since "-" character is greater than "+" character the result will be: +VE LESS THAN -VE

Remember: Never compare between numeric edited variables since they could lead to wrong results.


Go back to the main contents page