You are here: Home > Knowledge Refreshers KRC++

KR edition 158 to 159

KR-158 : COBOL Puzzle- idea contributed by Jeyakumar

Hi everyone,

We have a COBOL puzzle for this edition...

WS-EMP-ID is a PIC X(8) working storage variable and the following snippet is executed. What is the output?

MOVE SPACES TO WS-EMP-ID
IF WS-EMP-ID > 0
DISPLAY 'SPACE > 0!'
ELSE
DISPLAY 'SPACE <=0!'
END-IF

MOVE '11111111' TO WS-EMP-ID
IF WS-EMP-ID > 0
DISPLAY 'EMP ID > 0!'
ELSE
DISPLAY 'EMP ID <= 0!'
END-IF

It's something that generally confuses a programmer!

Another question is will WS-EMP-ID > 0 compile?

Yes it will and the answer is:
SPACE <=0!
EMP ID > 0!

Puzzled? Do you know why?

We'll ponder into the explanation in the next edition! Stay tuned..

KR-159 : COBOL puzzle explanation

We start off the week with the explanation for the previous COBOL puzzle contributed by Jeyakumar:

• A zero as a character is represented as hex F0 (based on EBCDIC coding).

• Any number stored as a character (i.e. in PIC X format), say n, is represnted as hex Fn in EBCDIC format.

• Thus when any number stored in character format is compared with character 0, the number will be greater. Which is why in the puzzle we got EMP ID > 0! for the following snippet:

MOVE '11111111' TO WS-EMP-ID
IF WS-EMP-ID > 0
DISPLAY 'EMP ID > 0!'
ELSE
DISPLAY 'EMP ID <= 0!'
END-IF

(character 1 in hex is F1 and character 0 is hex F0; so F1 > F0 and thus EMP ID > 0 is displayed).

• The character space is represented by hex 40 (which is the same value used in ASCII coding scheme as well). So the snippet:

MOVE SPACES TO WS-EMP-ID
IF WS-EMP-ID > 0
DISPLAY 'SPACE > 0!'
ELSE
DISPLAY 'SPACE <=0!'
END-IF

will yield the result SPACE <=0 because character space is hex 40 while 0 is represented as F0 (and hex 40 is < hex F0).