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-nis a numeric data item.numeric-nis a literal or data item, or the output of an intrinsic function that is numeric.group-nis a group data item containing one or more elementary data items.statement-nis an imperative statement.
General Rules:
- In a
FORMAT 1 ADDstatement containing aGIVINGclause, allnumeric-ndata items are added together, with the result stored innumeric-data-1. - In a
FORMAT 1 ADDstatement with noGIVINGclause, allnumeric-ndata items are added, in sequence, to each of the data items following theTOclause. - The
ROUNDEDclause 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
ROUNDEDclause, see the entry forROUNDEDin Common General Rules. - The
SIZE ERRORexception is triggered if theON SIZE ERRORclause is present and if the receiving field is not large enough to accommodate the result of theADDfunction. - For rules regaring the
ON SIZE ERRORclause, see the entry forON SIZE ERRORin Common General Rules. - In an
ADD CORRESPONDINGoperation, elementary items in separate group items must have the same elementary data name for theADDfunction to be performed. - The
ADD CORRESPONDINGoperation causes multipleADDoperations 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
CORRESPONDINGdata item for the purposes of theADD CORRESPONDINGclause are the same as the rules used for the purposes of theMOVE CORRESPONDINGclause. - For details on how data items are identified as
CORRESPONDING, see Common General Rules / Rules for identifying CORRESPONDING data. - In an
ADD CORRESPONDINGoperation, allADDoperations will be completed before theON SIZE ERRORcondition 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.