Skip to content

ADD Statement

The ADD Statement performs an arithmetic add operation on a number of operands and stores the result.

Format 1:

       ADD { numeric-1 }  TO { numeric-2 }  
              [ GIVING numeric-data-1 [ROUNDED] ] 
              [ ON SIZE ERROR statement-1 ] 
              [ NOT ON SIZE ERROR statement-2 ] 
              [ END-ADD ]

Format 2:

       ADD { CORRESPONDING } group-1 TO group-2 [ ROUNDED ] 
           { CORR          }
           [ ON SIZE ERROR statement-1 ] 
           [ NOT ON SIZE ERROR statement-2 ] 
           [ END-ADD ]

Syntax:

  1. numeric-data-n is a numeric data item.
  2. numeric-n is a literal or data item, or the output of an intrinsic function that is numeric.
  3. group-n is a group data item containing one or more elementary data items.
  4. statement-n is an imperative statement.

General Rules:

  1. In a FORMAT 1 ADD statement containing a GIVING clause, all numeric-n data items are added together, with the result stored in numeric-data-1.
  2. In a FORMAT 1 ADD statement with no GIVING clause, all numeric-n data items are added, in sequence, to each of the data items following the TO clause.
  3. 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.
  4. For rules regarding the ROUNDED clause, see the entry for ROUNDED in Common General Rules.
  5. The SIZE ERROR exception is triggered if the ON SIZE ERROR clause is present and if the receiving field is not large enough to accommodate the result of the ADD function.
  6. For rules regaring the ON SIZE ERROR clause, see the entry for ON SIZE ERROR in Common General Rules.
  7. In an ADD CORRESPONDING operation, elementary items in separate group items must have the same elementary data name for the ADD function to be performed.
  8. The ADD CORRESPONDING operation causes multiple ADD operations to be performed between elementary data items in separate group items, where the elementary items have the same elementary data name, and are not described as FILLER.
  9. The rules for identifying a CORRESPONDING data item for the purposes of the ADD CORRESPONDING clause are the same as the rules used for the purposes of the MOVE CORRESPONDING clause.
  10. For details on how data items are identified as CORRESPONDING, see Common General Rules / Rules for identifying CORRESPONDING data.
  11. In an ADD CORRESPONDING operation, all ADD operations will be completed before the ON SIZE ERROR condition will be triggered.

Code Sample:

       IDENTIFICATION DIVISION. 
       PROGRAM-ID. ADD-1. 
       ENVIRONMENT DIVISION. 
       DATA DIVISION. 
       WORKING-STORAGE SECTION. 
       77 FIELD-1 PIC 9V9 VALUE 2.5. 
       77 FIELD-2 PIC 9V9 VALUE 3.4. 
       77 FIELD-3 PIC 9.9. 

       01 GROUP-1.
           03 FLD-1 PIC 99 VALUE 10. 
           03 FLD-1 PIC 99 VALUE 20. 
       01 GROUP-2. 
           03 FLD-1 PIC 99 VALUE 5. 
           03 FLD-2 PIC 99 VALUE 15. 

       77 DUMMY PIC X. 
       PROCEDURE DIVISION. 
       MAIN. 
      *    ADD { INTEGER } TO { INTEGER-LIST } 
      *        [ GIVING INTEGER [ROUNDED] ] 
      *        [ ON SIZE ERROR STATEMENT-1 ] 
      *        [ NOT ON SIZE ERROR STATEMENT-2 ] 
      *        [ END-ADD ] 

           ADD FIELD-1 TO FIELD-2 GIVING FIELD-3 
               ON SIZE ERROR DISPLAY "INVALID ADDITION" LINE 10 COL 10 
               NOT ON SIZE ERROR DISPLAY FIELD-3 LINE 10 COL 10 
           END-ADD. 

      *    ADD LENGTH OF { INTEGER } TO { INTEGER-LIST } 
      *        [ GIVING INTEGER [ROUNDED] ] 
      *        [ ON SIZE ERROR STATEMENT-1 ] 
      *        [ NOT ON SIZE ERROR STATEMENT-2 ] 
      *        [ END-ADD ] 

           ADD LENGTH OF FIELD-1 TO FIELD-2 GIVING FIELD-3 
               ON SIZE ERROR DISPLAY "INVALID ADDITION" LINE 11 COL 10 
               NOT ON SIZE ERROR DISPLAY FIELD-3 LINE 11 COL 10 
           END-ADD. 

      *ADD {CORRESPONDING} GROUP-ITEM-1 TO GROUP-ITEM-2 [ ROUNDED ] 
      *    { CORR } 
      *    [ ON SIZE ERROR STATEMENT-1 ] 
      *    [ NOT ON SIZE ERROR STATEMENT-2 ] 
      *    [ END-ADD ] 

           ADD CORRESPONDING GROUP-1 TO GROUP-2 
               ON SIZE ERROR DISPLAY "INVALID ADDITION" LINE 12 COL 10 
               NOT ON SIZE ERROR 
                   DISPLAY FLD-1 OF GROUP-2 LINE 12 COL 10 
                   DISPLAY FLD-2 OF GROUP-2 LINE 13 COL 10 
               END-ADD. 

               DISPLAY "ADD1 FINISHED!" LINE 15 COL 10. 
               ACCEPT DUMMY LINE 15 COL 30. 
               STOP RUN.
Back to top