if-statementdivisioncobol

Cobol - checking for remainder in IF clause


I am wondering if there is any method to check the remainder of division in IF statement like: if (16 % 2 == 0) {...}

So far I have this:

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 number-in pic 9(3).
   01 number-out pic Z9.
   01 result pic 9.
   01 residue pic 9.
   PROCEDURE DIVISION.
   MAIN-PROCEDURE.

       MOVE 4 TO number-in.
       PERFORM L1-LOOP UNTIL number-in = 100.
       STOP RUN.

       L1-LOOP.
           DIVIDE number-in BY 2 GIVING result REMAINDER residue.
           IF residue = 0
               MOVE number-in to number-out
               DISPLAY number-out.
           ADD 1 TO number-in.

But here I have to use additional variable (result) to get remainder (residue) of the division. I would like to avoid this and check the remainder directly in the IF statement. Is it possible in Cobol? I would be grateful for some information or link.


Solution

  • Yes, this is doable in COBOL (you didn't specified which compiler you use, I'll assume it supports the intrinsic function module extension to COBOL85), as long as you don't need the actual result/remainder:

    Instead of

           DIVIDE number-in BY 2 GIVING result REMAINDER residue.
           IF residue = 0
    

    do

           IF FUNCTION MOD (number-in, 2) = 0
    

    For the link: You can (and should) have a look in your compiler's reference manual. Otherwise you may use a search engine for some details.

    Requested links: