TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

COBOL - Add Statement


The ADD statement sums two or more numeric operands and stores the result.

ADD statement is used to add numeric items together. There are three formats which works slightly differently and is described below,

Format 1: ADD statement

The first format is used to add field(s) or value(s) to another field. The field(s) or value(s) listed between ADD and TO are summed and added to the value of the field(s) following the TO, where the answer is stored.

Syntax:

ADD {identifier-1 or literal-1} ...TO {indentifier-2 [ROUNDED]}...
   [ON SIZE ERROR imperative-statement-1]
   [NOT ON SIZE ERROR imperative-statement-2]
[END-ADD]

identifier-1, identifier-2 must name an elementary numeric item.

literal Must be a numeric literal. Floating-point data items and literals can be used anywhere that a numeric data item or literal can be specified.

Example:

ADD A TO B

- The value in A is added to the value in B and the result is stored in B. The value in A is unchanged.

Furthermore,

ADD C D TO E F

- The sum of the values in C and D is added to the value in E, storing the answer in E and the sum of C and D is also added to F storing the value in F. The values in C and D are unchanged.
Format 2: ADD statement with GIVING phrase

The second format is used to add field(s) or value(s) to one another , storing the answer in a different field. The field(s) or value(s) listed between ADD and GIVING are added and stored in the field following the field(s) following the GIVING.

Syntax:

ADD {identifier-1 or literal-1} ...TO {indentifier-2 or literal-2} GIVING {indentifier-3 [ROUNDED]}...
   [ON SIZE ERROR imperative-statement-1]
   [NOT ON SIZE ERROR imperative-statement-2]
[END-ADD]

identifier-1, identifier-2 must name an elementary numeric item except when following the word GIVING. Each identifier that follows the word GIVING must name an elementary numeric or numeric-edited item.

literal Must be a numeric literal. Floating-point data items and literals can be used anywhere that a numeric data item or literal can be specified.

Example:

ADD A TO B GIVING C

- The value in A is added to the value in B and the result is stored in C. The values in A and B are unchanged.

Format 3: ADD statement with CORRESPONDING phrase

The third format is used to add subordinate field(s) of one group to subordinate field(s) in another, storing the answer in those fields. Those subordinate field(s) of the group item identifier-1 are added to and stored in those with exactly the same name in the group item identifier-2. The names of the subordinate items must be spelled exactly the same way in both groups to qualify to participate in the addition.

Syntax:

ADD {CORRESPONDING or CORR} indentifier-1 TO indentifier-2 [ROUNDED]
   [ON SIZE ERROR imperative-statement-1]
   [NOT ON SIZE ERROR imperative-statement-2]
[END-ADD]

identifier-1, identifier-2 must name an alphanumeric group item or national group item.

literal Must be a numeric literal. Floating-point data items and literals can be used anywhere that a numeric data item or literal can be specified.

Example: if two data hierarchies are defined as follows:

05 ITEM-1 OCCURS 6.
   10 ITEM-A PIC S9(3).
   10 ITEM-B PIC +99.9.
   10 ITEM-C PIC X(4).
   10 ITEM-D PIC REDEFINES ITEM-C PIC 9(4).
   10 ITEM-E USAGE COMP-1.
   10 ITEM-F USAGE INDEX.

05 ITEM-2.
   10 ITEM-A PIC S9(3).
   10 ITEM-B PIC +9V9.
   10 ITEM-C PIC A(4).
   10 ITEM-D PIC 9(4).
   10 ITEM-E PIC 9(9) USAGE COMP.
   10 ITEM-F USAGE INDEX.

Then, if

ADD CORR ITEM-2 TO ITEM-1(X)

is specified,

ITEM-A and ITEM-A(X), ITEM-B and ITEM-B(X), and ITEM-E and ITEM-E(X) are considered to be corresponding and are added together.

ITEM-C and ITEM-C(X) are not included because they are not numeric.

ITEM-D and ITEM-D(X) are not included because ITEM-D(X) includes a REDEFINES clause in its data description.

ITEM-F and ITEM-F(X) are not included because they are defined as USAGE IS INDEX.

Note that ITEM-1 is valid as either identifier-1 or identifier-2.

If any of the individual operations in the ADD CORRESPONDING statement produces a size error condition, imperative-statement-1 in the ON SIZE ERROR phrase is not executed until all of the individual additions are completed.


ON SIZE Error Occurs:

  • When the absolute value of the result of an arithmetic evaluation, after decimal point alignment, exceeds the largest value that can be contained in the result field.

  • When division by zero occurs.

  • If the ON SIZE ERROR phrase is specified and a size error condition occurs, the value of the resultant identifier affected by the size error is not altered--that is, the error results are not placed in the receiving identifier. After completion of the execution of the arithmetic operation, the imperative statement in the ON SIZE ERROR phrase is executed, control is transferred to the end of the arithmetic statement, and the NOT ON SIZE ERROR phrase, if specified, is ignored.

  • If the NOT ON SIZE ERROR phrase has been specified and, after execution of an arithmetic operation, a size error condition does not exist, the NOT ON SIZE ERROR phrase is executed.

  • For ADD CORRESPONDING and SUBTRACT CORRESPONDING statements, if an individual arithmetic operation causes a size error condition, the ON SIZE ERROR imperative statement is not executed until all the individual additions or subtractions have been completed.

  • If the ROUNDED phrase is specified, rounding takes place before size error checking.

ROUNDED Phrase:

  • After decimal point alignment, the number of places in the fraction of the result of an arithmetic operation is compared with the number of places provided for the fraction of the resultant identifier.

  • When the size of the fractional result exceeds the number of places provided for its storage, truncation occurs unless ROUNDED is specified. When ROUNDED is specified, the least significant digit of the resultant identifier is increased by 1 whenever the most significant digit of the excess is greater than or equal to 5.

END-ADD phrase:

This explicit scope terminator serves to delimit the scope of the ADD statement.

END-ADD permits a conditional ADD statement to be nested in another conditional statement. END-ADD can also be used with an imperative ADD statement.


Tips:

  • The fields to be added must have numeric pictures, that is, they can only have the characters 9, S, and V in their PIC clauses.

  • Receiving fields may be either numeric or numeric edited fields.

  • Use the SIZE ERROR clause to detect field overflow on the receiving field.

  • In all formats, the mathematically correct results are computed, but if the receiving field is too short in either the integer or decimal portion, the result will be truncated, the integer on the left and the decimal on the right. Including the ROUNDED phrase will result the answer field being rounded instead of truncating. Rounding is always done in the least significant portion of the answer.

If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal