The ADD statement performs arithmetic addition.
Format 1
ADD {num} ... TO { result [ROUNDED] } ...
[ ON SIZE ERROR statement-1 ]
[ NOT ON SIZE ERROR statement-2 ]
[ END-ADD ]
Format 2
ADD {num} ... TO num GIVING { result [ROUNDED] } ...
[ ON SIZE ERROR statement-1 ]
[ NOT ON SIZE ERROR statement-2 ]
[ END-ADD ]
Format 3
ADD {CORRESPONDING} group-item TO group-item [ROUNDED]
{CORR }
[ ON SIZE ERROR statement-1 ]
[ NOT ON SIZE ERROR statement-2 ]
[ END-ADD ]
Format 4
ADD TABLE src-table TO dest-table [ROUNDED]
[ FROM INDEX src-start TO src-end ]
[ DESTINATION INDEX dest-start ]
[ ON SIZE ERROR statement-1 ]
[ NOT ON SIZE ERROR statement-2 ]
[ END-ADD ]
Syntax Rules
- Num is a numeric literal or elementary numeric data item.
- Result is an elementary numeric data item or, in Format 2, an elementary numeric edited data item.
- group-item is a group item containing one or more elementary numeric data items.
- statement-1, and
statement-2 are imperative statements.
- CORR is an abbreviation of CORRESPONDING.
- src-table and
dest-table are numeric data items that are table elements. The low-order subscript of these items must be omitted. For example, if
SRC-1 was an element of a one-dimensional table, then you would just use
SRC-1 in the statement. If
SRC-2 was an element of a two-dimensional table, and you wanted to add all the elements in row
2, you would use
SRC-2( 2 ).
- src-start,
src-end and
dest-start are numeric literals or data items. These items may not be subscripted.
General Rules
- Note that pertinent additional information is located in the sections covering Arithmetic Operations
(6.4.1), Multiple Receiving Fields
(6.4.2), the ROUNDED Option
(6.4.3), the SIZE ERROR Option
(6.4.4), and the CORRESPONDING Option
(6.4.5).
- In Format 1, all nums are added together and their sum is then added to each result in turn.
- In Format 2, all nums are added together and their sum is moved to each result field.
- In Format 3, each pair of corresponding elementary numeric items in the two
group-items are added together. The results are moved to the corresponding items in the second
group-item.
- In Format 4, a range of
src-table elements is added to a range of
dest-table elements. The results are stored in
dest-table. The first element of the src-table range is added to the first element of the
dest-table range, the second element to the second, and so on.
- src-end specifies the first element of the source range. If omitted, the value defaults to
1.
src-end specifies the last element of the range (inclusive). If omitted, it is set to the current upper bound of the source table.
In a multidimensional table, the range of elements varies over the innermost OCCURS.
- dest-start indicates the first element of the destination range. If omitted, it defaults to
1. Note that the last element of the destination range is
dest-start +
src-end -1.
- If the SIZE ERROR phrase is used, elements for which the size error condition occurs are not updated; other elements are updated.
When an add results in a size error,
statement-1 executes, otherwise
statement-2 executes.
Note: A Format 4 ADD TABLE statement is usually substantially faster than an equivalent PERFORM loop. The degree of improvement
depends on the size of the range (larger ranges show better improvement). The SIZE ERROR and ROUNDED phrases typically add
significant overhead. The runtime always performs table boundary checking in ADD TABLE, even if the program is not compiled
with
-Za.
Code Example Format 4
The following definitions will be used in the examples:
01 SOURCE-TABLE OCCURS 20 TIMES PIC S9(9)V99.
01 DEST-TABLE OCCURS 20 TIMES PIC S9(9)V99.
01 ROLL-UP-TABLE.
03 TOTALS OCCURS 10 TIMES.
05 REPORT-SUM
OCCURS 20 TIMES PIC S9(9)V99.
77 CTR PIC 99.
To add all the elements of SOURCE-TABLE to DEST-TABLE:
ADD TABLE SOURCE-TABLE TO DEST-TABLE
To add the first five elements of SOURCE-TABLE to the last five elements of DEST-TABLE:
ADD TABLE SOURCE-TABLE TO DEST-TABLE
FROM INDEX 1 TO 5
DESTINATION INDEX 16
To add all the REPORT-SUM elements in the last TOTALS row to the row
above it (second to last row):
ADD TABLE REPORT-SUM(10) TO REPORT-SUM(9)
To perform the same operation using a PERFORM loop you would have to write the following code:
PERFORM VARYING CTR FROM 1 BY 1 UNTIL CTR > 20
ADD REPORT-SUM(10, CTR) TO REPORT-SUM(9, CTR)
END-PERFORM