Skip to content

COMPUTE Statement

The COMPUTE statement performs arithmetic computations on multiple operands and stores the results.

General Format:

       COMPUTE { numeric-data-1 [ROUNDED] } ... { =     } arithmetic-expr 
                                                { EQUAL } 
           [ ON SIZE ERROR statement-1 ] 
           [ NOT ON SIZE ERROR statement-2 ] 
           [ END-COMPUTE ]

Syntax:

  1. numeric-data-n is a numeric data item.
  2. statement-n is an imperative statement.

General Rules:

  1. EQUAL and “=” are synonyms.
  2. The ROUNDED clause is applied when an arithmetic operation produces a result that includes more decimal places than are included in the description of the data item given to hold the final result of the arithmetic operation.
  3. For rules regarding the ROUNDED clause, see the entry for ROUNDED in the Common General Rules section.
  4. The SIZE ERROR exception is trigg ered if the ON SIZE ERROR clause is present and if the receiving field is not large enough to accommodate the result of the arithmetic operation
  5. For rules regarsding the ON SIZE ERROR clause, see the entry for ON SIZE ERROR in the Common General Rules section.

Code Sample:

       IDENTIFICATION DIVISION. 
       PROGRAM-ID. COMPUTE-1. 
       ENVIRONMENT DIVISION. 
       DATA DIVISION. 
       WORKING-STORAGE SECTION. 

       77 FLD-1             PIC Z(9).9(9). 
       77 OP-1              PIC 9 VALUE 1. 
       77 OP-2              PIC 9 VALUE 7. 
       77 OP-3              PIC 9 VALUE 4. 
       77 TEMP-STRING       PIC X(40). 
       77 DUMMY             PIC X. 

       PROCEDURE DIVISION. 
       MAIN. 
      *COMPUTE { INT-LIST [ROUNDED] } ... { =     } ARITHMETIC-EXPR 
      *                                   { EQUAL } 
      *        [ ON SIZE ERROR STATEMENT ] 
      *        [ NOT ON SIZE ERROR STATEMENT ] 
      *        [ END-COMPUTE ] 
      * 
              COMPUTE FLD-1 ROUNDED = ( OP-1 / OP-2 ) * OP-3 
                     ON SIZE ERROR 
                            DISPLAY "SIZE ERROR!" LINE 12 COL 10 
                            PERFORM EXIT-COMPUTE 
                     NOT ON SIZE ERROR 
                            CONTINUE 
              END-COMPUTE. 

              PERFORM DISPLAY-RESULTS. 
              PERFORM EXIT-COMPUTE. 
              DISPLAY-RESULTS. 
              DISPLAY FLD-1 LINE 5, COL 10. 
              STRING"(", DELIMITED BY SIZE, 
                            OP-1,  DELIMITED BY SIZE, 
                            " / ", DELIMITED BY SIZE, 
                            OP-2,  DELIMITED BY SIZE, 
                            ")",   DELIMITED BY SIZE, 
                            " * ", DELIMITED BY SIZE, 
                            OP-3,  DELIMITED BY SIZE, 
                            " = ", DELIMITED BY SIZE, 
                            FLD-1, DELIMITED BY SIZE, 
                            INTO TEMP-STRING. 

              DISPLAY TEMP-STRING LINE10, COL10. 
       EXIT-COMPUTE. 
              DISPLAY "COMPUTE-1 COMPLETE!" LINE 12 COL 10. 
              ACCEPT DUMMY LINE 12 COL 30.
              STOP RUN.
Back to top