ADD Statement


ADD statement sums two or more numeric values and stores the result in the output variable. It is used to perform addition operations.

ADD statement is divided into below types based on their usage -

  • Simple ADD statement.
  • ADD with ON SIZE ERROR (Error handling).
  • ADD with GIVING.
  • ADD with CORRESPONDING.

Simple ADD statement -


All the values in input variables are added to the value in the output variable, and the result is placed in the output variable.

Syntax -

---+----2----+----3----+----4----+----5
ADD  input-value1|input-variable1
   [......]
TO   output-variable1 [ROUNDED]
   [....]
[END-ADD].
Note! All statements coded in [ ] are optional.

Parameters -

  • input-value1, ... - Specifies the input numeric value. From below example, it is 10.
  • input-variable1, ... - Specifies the input numeric variable. From below example, it is WS-A.
  • output-variable1, ... - Specifies it is one of the input and places the result once the operation is successful. From below example, it is WS-B.
  • ROUNDED Phrase - Used to round the fraction result to the nearest integer value based on the faction value.
  • END-ADD - Specifies the scope terminator and is optional when the period is coded at the end of the ADD statement.

Example -

Scenario - Adding number to the value in variable.

----+----1----+----2----+----3----+----4----+----5----+
       ...
       WORKING-STORAGE SECTION. 
       01 WS-VAR.
          05 WS-A      PIC 9(03) VALUE 20.
          05 WS-B      PIC 9(03) VALUE 30.
       ...
       PROCEDURE DIVISION.
           ADD 10, WS-A TO WS-B. 
           DISPLAY "RESULT: " WS-B.
		   ...

Output -

Result: 60

ADD with ON SIZE ERROR (Error handling) -


ON SIZE ERROR phrase executes the statements coded with it when the result value exceeds the maximum value a result variable can hold. NOT ON SIZE ERROR phrase executes statements-block2 when the operation is successful and result value is within the range.

Syntax -

---+----2----+----3----+----4----+----5
ADD  input-value1|input-variable1
   [......]
TO   output-variable1 [ROUNDED]
   [....]
       [ON SIZE ERROR statements-block1]
   [NOT ON SIZE ERROR statements-block2]
[END-ADD].

Parameters -

  • SIZE ERROR - Refer to the SIZE ERROR phrase for full information.
  • statements-block1 - specifies the set of statements that are executed when SIZE ERROR occurs. From below example, it is DISPLAY "OVERFLOW".
  • statements-block2 - specifies the set of statements that are executed when the ADDition is successful. From below example, it is DISPLAY "NOT OVERFLOW".

Example -

Scenario - Addition error handling.

----+----1----+----2----+----3----+----4----+----5----+
       ...
	   WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-A      PIC 9(03) VALUE 900.
          05 WS-B      PIC 9(03) VALUE 200.
          05 WS-C      PIC 9(03) VALUE 100.
       ...
       PROCEDURE DIVISION.
           ADD WS-A, WS-B TO WS-C 
                ON SIZE ERROR DISPLAY "OVERFLOW"
            NOT ON SIZE ERROR DISPLAY "NOT OVERFLOW".  
		   ...

Output -

OVERFLOW

ADD with GIVING -


GIVING phrase specifies that the output variables are used to store the result and are not part of the inputs. All inputs before the keyword GIVING are added, and the result is stored in the variables with the GIVING phrase.

Syntax -

1----+----2----+----3----+----4----+----5
  ADD    input-value1|input-variable1
         [......]
   TO    input-variableA
  GIVING output-variable1 [ROUNDED]
	     [,.....]
         [ON SIZE ERROR Statements-block1]
    [NOT ON SIZE ERROR Statements-block2]
  [END-ADD].
Note! TO phrase should have only one variable, and multiple variables are not allowed.

Example -

Scenario - Adding two variables and stored the result into another variable.

----+----1----+----2----+----3----+----4----+----5----+
       ...
	   WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-A      PIC 9(03) VALUE 20.
          05 WS-B      PIC 9(03) VALUE 60.
          05 WS-C      PIC 9(03).
       ...
       PROCEDURE DIVISION.
           ADD WS-A TO WS-B GIVING WS-C.
           DISPLAY "WS-C:  " WS-C.
		   ...

Output -

WS-C:  80

ADD with CORRESPONDING -


ADD CORRESPONDING [CORR] adds identical elementary variables data from two groups. The results are stored in the elementary variables of the group coded after the keyword TO.

Syntax -

---+----2----+----3----+----4----+----5
ADD [CORRESPONDING|CORR]
	 input-group-variable
 TO  output-group-variable
        [ON SIZE ERROR Statements-block1]
    [NOT ON SIZE ERROR Statements-block2]
[END-ADD].

Parameters -

  • CORRESPONDING|CORR Phrase - This phrase performs the ADD on the same named elementary variables under two different group variables.
  • input-group-variable - From below example, it is WS-VAR1.
  • output-group-variable - It is one of the inputs. From below example, it is WS-VAR2.

Example -

Scenario - Adding corresponding variables from two groups.

----+----1----+----2----+----3----+----4----+----5----+
       ...
	   WORKING-STORAGE SECTION.
       01 WS-VAR1.                             
          05 WS-A          PIC 9(02) VALUE 10.
          05 WS-B          PIC 9(02) VALUE 10.
       01 WS-VAR2.                            
          05 WS-A          PIC 9(02) VALUE 20.
          05 WS-B          PIC 9(02) VALUE 30.
       ...
       PROCEDURE DIVISION.
           ADD CORR WS-VAR1   TO WS-VAR2.
           DISPLAY "WS-A: " WS-A OF WS-VAR2.
           DISPLAY "WS-B: " WS-B OF WS-VAR2.
		   ...

Output -

WS-A: 30
WS-B: 40