Adding Sequences of Fields

Besides basic addition described in Basic Arithmetic Operations, the ADD statement has some variants for adding sequences of fields. For example:

Syntax

ADD <n1> THEN <n 2> UNTIL <n z> GIVING <m>.

If <n 1 >, <n 2 >,..., <n z > is a sequence of equidistant fields of the same type and length in memory, they are summed and the result is assigned to <m>.

Syntax

ADD <n1> THEN <n 2> UNTIL <n z> TO <m>.

This statement works exactly like the previous one, but with the exception that the sum of the field values is added to the old contents of <m>.

For information about other similar variants, see the keyword documentation of the ADD statement.

DATA: BEGIN OF SERIES,
N1 TYPE I VALUE 10,
N2 TYPE I VALUE 20,
N3 TYPE I VALUE 30,
N4 TYPE I VALUE 40,
N5 TYPE I VALUE 50,
N6 TYPE I VALUE 60,
END OF SERIES.

DATA SUM TYPE I.

ADD SERIES-N1 THEN SERIES-N2 UNTIL SERIES-N5 GIVING SUM.
WRITE SUM.

ADD SERIES-N2 THEN SERIES-N3 UNTIL SERIES-N6 TO SUM.
WRITE / SUM.

Output

       150

       350

Here, the contents of components N1 to N5 are summed and assigned to the field SUM. Then, the contents of components N2 to N6 are summed and added to the value of SUM.