The ADD statement sums two or more numeric operands and stores the result stores into the target operand.

If only one operand is specified in the input operands, then the input operand value adds to target operand value and stores the result in target operand. For example -

ADD WS-A TO WS-B.

In the above case, WS-A value adds to the WS-B and stores the result in WS-B.

Syntax-1: Simple ADD statement -

All identifiers or literals that are before the keyword TO are added together and the summed result is stored in identifier-a.

ADD Syntax

identifier-1, identifier-2, ..... -

  • If identifier-a is a numeric item, these all should be an elementary numeric items.
  • If identifier-a is a date field, these are all should not be date fields and should be numeric items only.

literal-1, literal-2, ... -

  • Should be a numeric literal.
  • Floating-point variables and literals can use anywhere that a numeric variable or literal can be specified.

identifier-a, identifier-b, ... -

  • It can be an elementary numeric item or a date field.
  • If the target identifier have the value assigned, the input value adds to the identifier value and stores the result in the same identifier.
  • If the target identifier value is not set and only one input, the input value directly assigned to the target identifer.
  • If the target identifier value is not set and multiple inputs, the input values adds and the final result assigned to the target identifer.

ROUNDED Phrase -

  • The ROUNDED option is used to round the fraction result to the nearest integer value based on the faction value.
  • Suppose the ROUNDED option is specified and the fraction value is greater than ".5". In that case, the least significant digit of the result is increased by 1.
  • Suppose the ROUNDED option is specified and the fraction value is less than ".5". In that case, the least significant digit remains same.
  • Refer ROUNDED Phrase topic for complete information.

END-ADD phrase -

  • The END-ADD was used to end the scope of the ADD statement.
  • It is optional.
  • END-ADD is not required when ADD statement ended with a period.

Examples -

Scenario1 - Using single literal

Input-        WS-B not initialized
Declaration-  05 WS-B     PIC 9(03).
Code-         ADD 10 TO WS-B.
Result-       WS-B = 10

In the above case, 10 assigns to WS-B. Because WS-B not having any value.

Scenario2 - Using single literal

Input-        WS-B = 20
Declaration-  05 WS-B     PIC 9(03) VALUE 20.
Code-         ADD 10 TO WS-B.
Result-       WS-B = 30

In the above case, 10 adds to WS-B value and result stores in WS-B.

Scenario3 - Using multiple literals

Input-        WS-B = 30
Declaration-  05 WS-B     PIC 9(03) VALUE 30.
Code-         ADD 10, 20 TO WS-B.
Result-       WS-B = 60

In the above case, 10, 20 adds to WS-B value and result stores in WS-B.

Scenario4 -Using single input identifier

Input-        WS-A = 10, WS-B = 20
Declaration-  05 WS-A     PIC 9(03) VALUE 10.
              05 WS-B     PIC 9(03) VALUE 20.
Code-         ADD WS-A TO WS-B.
Result-       WS-B = 30

In the above case, WS-A value adds to WS-B value and result stores in WS-B.

Scenario5 -Using multiple input identifiers

Input-        WS-A = 10, WS-B = 20, WS-C = 40
Declaration-  05 WS-A     PIC 9(03) VALUE 10.
              05 WS-B     PIC 9(03) VALUE 20.
              05 WS-C     PIC 9(03) VALUE 40.
Code-         ADD WS-A, WS-B TO WS-C.
Result-       WS-C = 70

In the above case, WS-A and WS-B values adds to WS-C value and result stores in WS-C.

Scenario6 - Using single input literal and multiple output identifers

Input-        WS-B = 20, WS-C = 30 
Declaration-  05 WS-B     PIC 9(03) VALUE 20.
              05 WS-C     PIC 9(03) VALUE 30.
Code-         ADD 10 TO WS-B, WS-C.
Result-       WS-B = 30, WS-C = 40

In the above case, 10 adds to WS-B, WS-C values and corresponding results stores in WS-B and WS-C.

Scenario7 - Using multiple input literals and multiple output identifers

Input-        WS-B = 30, WS-C = 50 
Declaration-  05 WS-B     PIC 9(03) VALUE 30.
              05 WS-C     PIC 9(03) VALUE 50.
Code-         ADD 10, 20 TO WS-B.
Result-       WS-B = 60, WS-C = 80

In the above case, 10, 20 adds to WS-B, WS-C values and corresponding results stores in WS-B, WS-C.

Scenario8 -Using single input identifier and multiple output identifers

Input-        WS-A = 10, WS-B = 20, WS-C = 40
Declaration-  05 WS-A     PIC 9(03) VALUE 10.
              05 WS-B     PIC 9(03) VALUE 20.
              05 WS-C     PIC 9(03) VALUE 40.
Code-         ADD WS-A TO WS-B, WS-C.
Result-       WS-B = 30, WS-C = 50

In the above case, WS-A value adds to WS-B, WS-C values and corresponding results stores in WS-B, WS-C.

Scenario9 -Using multiple input identifiers and multiple output identifers

Input-        WS-A = 10, WS-B = 20, WS-C = 40, WS-D = 50
Declaration-  05 WS-A     PIC 9(03) VALUE 10.
              05 WS-B     PIC 9(03) VALUE 20.
              05 WS-C     PIC 9(03) VALUE 40.
              05 WS-D     PIC 9(03) VALUE 50.
Code-         ADD WS-A, WS-B TO WS-C.
Result-       WS-C = 70, WS-D = 80

In the above case, WS-A and WS-B values adds to WS-C, WS-D values and corresponding results stores in WS-C, WS-D.

Scenario10 -Using decimal inputs

Input-        WS-A = 10.2, WS-B = 20.5
Declaration-  05 WS-A     PIC 9(03)V9(02) VALUE 10.2
              05 WS-B     PIC 9(03)V9(02) VALUE 20.5.
              05 WS-C     PIC 9(03)V9(02).
Code-         ADD WS-A, WS-B TO WS-C.
Result-       WS-C = 30.7

In the above case, WS-A and WS-B values adds to WS-C and result stores in WS-C.

Scenario11 -ROUNDED

Input-        WS-A = 10.2, WS-B = 20.5
Declaration-  05 WS-A     PIC 9(03)V9(02) VALUE 10.2
              05 WS-B     PIC 9(03)V9(02) VALUE 20.5.
              05 WS-C     PIC 9(03).
Code-         ADD WS-A, WS-B TO WS-C ROUNDED.
Result-       WS-C = 31

In the above case, the result 30.7 have the fraction value > .5. So 1 added to the last significant digit and the result is 31.

Syntax-2: ADD statement with ON SIZE ERROR Phrase -

Suppose the computed result value is greater than the maximum value stores in identifier-a. In that case, the ON SIZE ERROR phrase is executed and routes the execution flow.

ADD Syntax

SIZE ERROR -

A size error condition occurs in below cases -

  • When the result value of an arithmetic expression exceeds the largest value of the target field.
  • When the year of the arithmetic statement result falls outside the century window.
  • Refer SIZE ERROR phrase topic for complete information.

Results of using date fields in addition -

Nondate second operandDate field second operand
Nondate first operand Nondate Date field
Date field first operand Date field Not allowed

Examples -

Scenario1 - No overflow

Input-        WS-A = 700, WS-B = 200
Declaration-  05 WS-A     PIC 9(03) VALUE 700.
              05 WS-B     PIC 9(03) VALUE 200.
              05 WS-C     PIC 9(03).
Code-         ADD WS-A, WS-B TO WS-C 
                ON SIZE ERROR DISPLAY "OVERFLOW"
                NOT ON SIZE ERROR DISPLAY "WS-C:  " WS-C.             
Result-      WS-C = 900

In the above case, the result is 900. So, the NOT ON SIZE ERROR phrase gets executed and displays the the result 900.

Scenario2 - Result overflow

Input-        WS-A = 900, WS-B = 200
Declaration-  05 WS-A     PIC 9(03) VALUE 900.
              05 WS-B     PIC 9(03) VALUE 200.
              05 WS-C     PIC 9(03).
Code-         ADD WS-A, WS-B TO WS-C 
                ON SIZE ERROR DISPLAY "OVERFLOW"
                NOT ON SIZE ERROR DISPLAY "WS-C:  " WS-C.             
Result-       OVERFLOW

In the above case, the result is 1100. However, WS-C is declared as 9(3) and the maximum value can save is 999. So, the SIZE ERROR phrase gets executed and displays the message "OVERFLOW".

Scenario3 - Date overflow

Input-        WS-B = 2020, WS-B = 0120
Declaration-  05 WS-A          DATE FORMAT YYYY  PIC 9999 VALUE IS 2020. 
              05 WS-B          PIC 9(04) VALUE 0120.
              05 WS-C          DATE FORMAT YYYY    PIC 9999.
Code-         ADD WS-A, WS-B TO WS-C
                ON SIZE ERROR DISPLAY "OVERFLOW"
                NOT ON SIZE ERROR DISPLAY "WS-C:  " WS-C.             
Result-       OVERFLOW

In the above case, the result is 2140 which is beyond the century limit(2020 - 2119). So, the SIZE ERROR phrase gets executed and displays the message "OVERFLOW".

Note! The DATEPROC(TRIG) compiler option should be in effect to get the SIZE ERROR triggered.

Syntax-3: ADD statement with GIVING phrase -

The GIVING phrase is used when the target operand is different from the second source operand.

All identifiers or literals before the keyword TO are added with the identifiers or literals after keyword TO, and the result stores in the identifiers with the GIVING phrase.

ADD Statement with GIVING Phrase
Warning! TO phrase should have only one idenfier (identifier-a) and multiple identifiers not allowed.

GIVING Phrase -

  • The GIVING keyword is used to identify the target field to store the calculated result.
  • The identifier can be a numeric-edited item.

Examples -

Scenario1 - With two inputs

Input-        WS-A = 20, WS-B = 60
Declaration-  05 WS-A          PIC 9(02) VALUE 20.
              05 WS-B          PIC 9(02) VALUE 60.
              05 WS-C          PIC 9(02).
Code-         ADD WS-A TO WS-B GIVING WS-C
                ON SIZE ERROR DISPLAY "OVERFLOW"
                NOT ON SIZE ERROR DISPLAY "WS-C:  " WS-C.
Result-       WS-C = 80

In the above case, WS-A and WS-B values added and stores in WS-C. WS-A and WS-B values remains same.

Scenario2 - With two inputs before keyword TO

Input-        WS-A = 20, WS-B = 60, WS-C = 10
Declaration-  05 WS-A          PIC 9(02) VALUE 20.
              05 WS-B          PIC 9(02) VALUE 60.
              05 WS-C          PIC 9(02) VALUE 10.
              05 WS-D          PIC 9(02).
Code-         ADD WS-A, WS-B TO WS-C GIVING WS-D
                ON SIZE ERROR DISPLAY "OVERFLOW"
                NOT ON SIZE ERROR DISPLAY "WS-D:  " WS-D.
Result-       WS-D = 90

In the above case, WS-A, WS-B and WS-C values added together and stores in WS-D. WS-A, WS-B and WS-C values remains same.

Scenario3 - Multiple outputs

Input-         WS-A = 20, WS-B = 60, WS-C = 10
Declaration-  05 WS-A          PIC 9(02) VALUE 20.
              05 WS-B          PIC 9(02) VALUE 60.
              05 WS-C          PIC 9(02) VALUE 10.
              05 WS-D          PIC 9(02).
              05 WS-E          PIC 9(02).
Code-         ADD WS-A, WS-B TO WS-C GIVING WS-D, WS-E
                ON SIZE ERROR DISPLAY "OVERFLOW"
                NOT ON SIZE ERROR DISPLAY "WS-D:  " WS-D.
Result-       WS-D = 90, WS-E = 90

In the above case, WS-A, WS-B and WS-C values added together and stores in WS-D. WS-A, WS-B and WS-C values remains same.

Syntax-4: ADD statement with CORRESPONDING phrase -

ADD CORRESPONDING is used to add the corresponding variables or fields in two identical group variables.

The results get stored in the elementary variables of group variable after the keyword TO.

ADD Statement with CORRESPONDING Phrase

group-variable1, group-variable2 -

  • group-variable1, group-variable2 are the group items.
  • These should have same set of elementary data items/variables.

CORRESPONDING/CORR Phrase -

  • The CORRESPONDING phase is used to perform the ADD operation on same-named elementary data items from two different group items.
  • Refer CORRESPONDING/CORR Phrase topic complete information.

Examples -

Scenario - CORRESPONDING

Input-        WS-VAR, WS-VAR1
Declaration-  01 WS-VAR.                             
                 05 WS-A          PIC 9(02) VALUE 20.
                 05 WS-B          PIC 9(02) VALUE 30.
              01 WS-VAR1.                            
                 05 WS-A          PIC 9(02) VALUE 10.
                 05 WS-B          PIC 9(02) VALUE 10.
Code-         ADD CORR WS-VAR   TO WS-VAR1
                ON SIZE ERROR DISPLAY "OVERFLOW"
                NOT ON SIZE ERROR DISPLAY "WS-VAR1:  " WS-VAR1.
Result-       WS-A = 30, WS-B = 40

In the above case, WS-A of WS-VAR added to WS-A of WS-VAR1 and stores the result in WS-A of WS-VAR1. Similarly, for WS-B.

Practical Example -

Scenario - Below example describes how the ADD statement used in COBOL programming.

Code -

ADD program Code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. ADDITION.                                            
       AUTHOR. MTH.                                                     
                                                                        
       DATA DIVISION.                                                   
       WORKING-STORAGE SECTION.                                         
                                                                        
       01 WS-VAR.                                                       
          05 WS-V1         PIC 9(02) VALUE 20.                          
          05 WS-V2         PIC 9(02) VALUE 30.                          
          05 WS-V3         PIC 9(02).                                   
       01 WS-VAR1.                                                      
          05 WS-A          PIC 9(02) VALUE 20.                          
          05 WS-B          PIC 9(02) VALUE 30.                          
       01 WS-VAR2.                                                      
          05 WS-A          PIC 9(02) VALUE 10.                          
          05 WS-B          PIC 9(02) VALUE 10.                          
                                                                        
       PROCEDURE DIVISION.                                              
                                                                        
      *    SIMPLE ADD STATEMENT                                         
           ADD WS-V1, 10        TO WS-V3.                               
           DISPLAY "SIMPLE ADD OUTPUT: " WS-V3.                         
                                                                        
      *    ADD STATEMENT WITH ON SIZE ERROR PHRASE                      
           MOVE ZEROES          TO WS-V3.                               
           ADD WS-V1, WS-V2, 50 TO WS-V3                                
            ON SIZE ERROR DISPLAY "RESULT OVERFLOW1"                    
            NOT ON SIZE ERROR                                           
                DISPLAY "ADD WITH SIZE ERROR OUTPUT: " WS-V3.           
                                                                        
      *    ADD STATEMENT WITH GIVING PHRASE                             
           ADD WS-V1, 30        TO WS-V2                                
            GIVING WS-V3                                                
            ON SIZE ERROR DISPLAY "RESULT OVERFLOW2"                    
            NOT ON SIZE ERROR                                           
                DISPLAY "ADD WITH GIVING OUTPUT: " WS-V3.               
                                                                        
      *    ADD STATEMENT WITH CORRESPONDING PHRASE                      
           ADD CORR WS-VAR1     TO WS-VAR2                              
            ON SIZE ERROR DISPLAY "RESULT OVERFLOW3"                    
            NOT ON SIZE ERROR                                           
                DISPLAY "ADD WITH CORR OUTPUT: " WS-VAR2.               
                                                                        
           STOP RUN.                                                    
**************************** Bottom of Data ****************************

Output -

ADD program Output