Search code examples
db2cobolzos

COBOL add 0 to a Variable in COMPUTE


I ran into a strange statement when working on a COBOL program from $WORK.

We have a paragraph that is opening a cursor (from DB2), and the looping over it until it hits an EOT (in pseudo code):

... working storage ...
01  I                       PIC S9(9) COMP VALUE ZEROS.
01  WS-SUB                  PIC S9(4) COMP VALUE 0.

... code area ...
PARA-ONE.                                                
    PERFORM OPEN-CURSOR
    PERFORM FETCH-CURSOR

    PERFORM VARYING I FROM 1 BY 1 UNTIL SQLCODE = DB2EOT                        
        do stuff here...
    END-PERFORM                                           

    COMPUTE WS-SUB = I + 0                            
    PERFORM CLOSE-CURSOR

    ... do another loop using WS-SUB ...

I'm wondering why that COMPUTE WS-SUB = I + 0 line is there. My understanding is that I will always at least be 1, because of the perform block above it (i.e., even if there is an EOT to start with, I will be set to one on that initial iteration).

Is that COMPUTE line even needed? Is it doing some implicit casting that I'm not aware of? Why would it be there? Why wouldn't you just MOVE I TO WS-SUB?


Solution

  • Call it stupid, but with some compilers (with the correct options in effect), given

     01  SIGNED-NUMBER   PIC S99 COMP-5 VALUE -1.
     01  UNSIGNED-NUMBER PIC  99 COMP-5.
          ... 
          MOVE SIGNED-NUMBER TO UNSIGNED-NUMBER
          DISPLAY UNSIGNED-NUMBER
    

    results in: 255. But...

    COMPUTE UNSIGNED-NUMBER = SIGNED-NUMBER + ZERO
    

    results in: 1 (unsigned)

    So to answer your question, this could be classified as a technique used cast signed numbers into unsigned numbers. However, in the code example you gave it makes no sense at all.