This document covers the following topics:
Related Statements: COMPRESS |
COMPUTE |
DIVIDE |
EXAMINE |
MOVE |
MOVE ALL |
MULTIPLY |
RESET |
SEPARATE |
SUBTRACT
Belongs to Function Group: Arithmetic and Data Movement Operations
The ADD statement is used to add two or more operands.
This statements has two different syntax structures.
Notes:
ADD statement is
executed, each operand used in the arithmetic operation must contain a valid
value.
ADD [ROUNDED]
operand1TO
operand2 |
For an explanation of the symbols used in the syntax diagram, see Syntax Symbols.
Operand Definition Table (Syntax 1):
| Operand | Possible Structure | Possible Formats | Referencing Permitted | Dynamic Definition | |||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
operand1 |
C | S | A | N | N | P | I | F | D | T | yes | no | |||||||||
operand2 |
S | A | M | N | P | I | F | D | T | yes | yes | ||||||||||
Syntax Element Description:
| Syntax Element | Description: |
|---|---|
operand1 |
Operand(s): operand1
is a summand
|
ROUNDED |
ROUNDED Option:
If the keyword For information on rounding, see Rules for Arithmetic Assignment, Field Truncation and Field Rounding in the Programming Guide. |
TO operand2
|
Summand and Result of Summation:
|
Example:
The statement
ADD #A(*) TO #B(*)is equivalent toCOMPUTE #B(*) := #A(*) + #B(*)ADD #S TO #Ris equivalent toCOMPUTE #R := #S + #RADD #S #T TO #Ris equivalent toCOMPUTE #R := #S + #T + #RADD #A(*) TO #Ris equivalent toCOMPUTE #R := #A(*) + #R
ADD [ROUNDED]
operand1GIVING
operand2
|
For an explanation of the symbols used in the syntax diagram, see Syntax Symbols.
Operand Definition Table (Syntax 2):
| Operand | Possible Structure | Possible Formats | Referencing Permitted | Dynamic Definition | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
operand1 |
C | S | A | N | N | P | I | F | D | T | yes | no | ||||||||
operand2 |
S | A | M | A | U | N | P | I | F | B* | D | T | yes | yes | ||||||
* Format B of operand3 may be
used only with a length of less than or equal to 4.
Syntax Element Description:
| Syntax Element | Description: |
|---|---|
operand1 |
Operands: operand1
is a summand.
|
ROUNDED |
ROUNDED Option:
If the keyword For information on rounding, see Rules for Arithmetic Assignment, Field Truncation and Field Rounding in the Programming Guide. |
GIVING
operand2
|
Result of
Summation: operand2 is
only used to receive the result of the operation. It is not included
in the addition.
Note: |
Note:
If Syntax 2 is used, the following applies: Only the
(operand1) field(s) left of the keyword
GIVING are the terms of the addition, the field right of the
keyword GIVING (operand2)
is just used to receive the result value. If just a single
(operand1) field is supplied, the
ADD operation turns into an assignment.
Example:
The statement
ADD #S GIVING #Ris equivalent toCOMPUTE #R := #SADD #S #T GIVING #Ris equivalent toCOMPUTE #R := #S + #TADD #A(*) 0 GIVING #Ris equivalent toCOMPUTE #R := #A(*) + 0which is a legal operation, due to the rules defined in Arithmetic Operations with ArraysADD #A(*) GIVING #Ris equivalent toCOMPUTE #R := #A(*)which is an illegal operation, due to the rules defined in Assignment Operations with Arrays
** Example 'ADDEX1': ADD
************************************************************************
DEFINE DATA LOCAL
1 #A (P2)
1 #B (P1.1)
1 #C (P1)
1 #DATE (D)
1 #ARRAY1 (P5/1:4,1:4) INIT (2,*) <5>
1 #ARRAY2 (P5/1:4,1:4) INIT (4,*) <10>
END-DEFINE
*
ADD +5 -2 -1 GIVING #A
WRITE NOTITLE 'ADD +5 -2 -1 GIVING #A' 15X '=' #A
*
ADD .231 3.6 GIVING #B
WRITE / 'ADD .231 3.6 GIVING #B' 15X '=' #B
*
ADD ROUNDED 2.9 3.8 GIVING #C
WRITE / 'ADD ROUNDED 2.9 3.8 GIVING #C' 8X '=' #C
*
MOVE *DATX TO #DATE
ADD 7 TO #DATE
WRITE / 'CURRENT DATE:' *DATX (DF=L) 13X
'CURRENT DATE + 7:' #DATE (DF=L)
*
WRITE / '#ARRAY1 AND #ARRAY2 BEFORE ADDITION'
/ '=' #ARRAY1 (2,*) '=' #ARRAY2 (4,*)
ADD #ARRAY1 (2,*) TO #ARRAY2 (4,*)
WRITE / '#ARRAY1 AND #ARRAY2 AFTER ADDITION'
/ '=' #ARRAY1 (2,*) '=' #ARRAY2 (4,*)
*
END
ADD +5 -2 -1 GIVING #A #A: 2
ADD .231 3.6 GIVING #B #B: 3.8
ADD ROUNDED 2.9 3.8 GIVING #C #C: 7
CURRENT DATE: 2005-01-10 CURRENT DATE + 7: 2005-01-17
#ARRAY1 AND #ARRAY2 BEFORE ADDITION
#ARRAY1: 5 5 5 5 #ARRAY2: 10 10 10 10
#ARRAY1 AND #ARRAY2 AFTER ADDITION
#ARRAY1: 5 5 5 5 #ARRAY2: 15 15 15 15