ADD Statement


The ADD statement is used to perform addition operations. The ADD statement sums two or more numeric values and stores the result in the output variable. If only one value or one variable is used as an input, then it adds to the output variable and places the result into the output variable.

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 -


In this format, all the input numeric values or values in variables are added to the output variable, and the result will be placed into 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, ... - From example, it is 10.
  • input-variable1, ... - From example, it is WS-A.
  • output-variable1, ... - It is also one of the input. From 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 for the ADD statement and is optional when the period is coded at the end of the ADD statement.

Example -

Scenario - Adding number to a variable.

 WORKING-STORAGE SECTION.
 01 WS-VAR.
    05 WS-A      PIC 9(03) VALUE 20.

 PROCEDURE DIVISION.
	 ADD 10 TO WS-A.
	 DISPLAY "Result: " WS-A.

Output -

Result: 30

ADD with ON SIZE ERROR (Error handling) -


ON SIZE ERROR is used to handle the program flow when the result value exceeds the maximum value that a result variable can hold. NOT ON SIZE ERROR phrase handles the program flow when the 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 the example, it is DISPLAY "OVERFLOW".
  • statements-block2 - specifies the set of statements that are executed when the ADDition is successful. From the example, it is DISPLAY "NOT OVERFLOW".

Example -

Scenario - Addition error handling.

 WORKING-STORAGE SECTION.
 01 WS-VAR.
    05 WS-A      PIC 9(03) VALUE 900.
    05 WS-B      PIC 9(03) VALUE 200.
    05 WS-B      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 "WS-C:  " WS-C.

Output -

OVERFLOW

ADD with GIVING -


The GIVING phrase is used when the output variables only used to store the result and not part of the inputs. All inputs before the keyword TO are added with inputs after the keyword TO, 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.

 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 the corresponding identical elementary variables in two groups. The results get stored in the elementary variables of the group variable 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 example, it is WS-VAR1.
  • output-group-variable - It is one of the inputs. From example, it is WS-VAR2.

Example -

Scenario - Adding corresponding variables from two groups.

 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