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:
numeric-data-n
is a numeric data item.numeric-n
is a literal or data item, or the output of an intrinsic function that is numeric.group-n
is a group data item containing one or more elementary data items.statement-n
is an imperative statement.
General Rules:
- In a
FORMAT 1 ADD
statement containing aGIVING
clause, allnumeric-n
data items are added together, with the result stored innumeric-data-1
. - In a
FORMAT 1 ADD
statement with noGIVING
clause, allnumeric-n
data items are added, in sequence, to each of the data items following theTO
clause. - 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. - For rules regarding the
ROUNDED
clause, see the entry forROUNDED
in Common General Rules. - The
SIZE ERROR
exception is triggered if theON SIZE ERROR
clause is present and if the receiving field is not large enough to accommodate the result of theADD
function. - For rules regaring the
ON SIZE ERROR
clause, see the entry forON SIZE ERROR
in Common General Rules. - In an
ADD CORRESPONDING
operation, elementary items in separate group items must have the same elementary data name for theADD
function to be performed. - The
ADD CORRESPONDING
operation causes multipleADD
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 asFILLER
. - The rules for identifying a
CORRESPONDING
data item for the purposes of theADD CORRESPONDING
clause are the same as the rules used for the purposes of theMOVE CORRESPONDING
clause. - For details on how data items are identified as
CORRESPONDING
, see Common General Rules / Rules for identifying CORRESPONDING data. - In an
ADD CORRESPONDING
operation, allADD
operations will be completed before theON 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.