Enterprise COBOL for z/OS, V4.2, Language Reference

ADD statement

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

Read syntax diagramSkip visual syntax diagram
Format 1: ADD statement

        .------------------.       
        V                  |       
>>-ADD----+-identifier-1-+-+--TO-------------------------------->
          '-literal-1----'         

   .---------------------------.   
   V                           |   
>----identifier-2--+---------+-+-------------------------------->
                   '-ROUNDED-'     

>--+--------------------------------------------+--------------->
   '-+----+--SIZE ERROR--imperative-statement-1-'   
     '-ON-'                                         

>--+-------------------------------------------------+---------->
   '-NOT--+----+--SIZE ERROR--imperative-statement-2-'   
          '-ON-'                                         

>--+---------+-------------------------------------------------><
   '-END-ADD-'   

All identifiers or literals that precede the keyword TO are added together, and this sum is added to and stored in identifier-2. This process is repeated for each successive occurrence of identifier-2 in the left-to-right order in which identifier-2 is specified.

Read syntax diagramSkip visual syntax diagram
Format 2: ADD statement with GIVING phrase

        .------------------.                             
        V                  |                             
>>-ADD----+-identifier-1-+-+--+----+--+-identifier-2-+---------->
          '-literal-1----'    '-TO-'  '-literal-2----'   

           .---------------------------.   
           V                           |   
>--GIVING----identifier-3--+---------+-+------------------------>
                           '-ROUNDED-'     

>--+--------------------------------------------+--------------->
   '-+----+--SIZE ERROR--imperative-statement-1-'   
     '-ON-'                                         

>--+-------------------------------------------------+---------->
   '-NOT--+----+--SIZE ERROR--imperative-statement-2-'   
          '-ON-'                                         

>--+---------+-------------------------------------------------><
   '-END-ADD-'   

The values of the operands that precede the word GIVING are added together, and the sum is stored as the new value of each data item referenced by identifier-3.

Read syntax diagramSkip visual syntax diagram
Format 3: ADD statement with CORRESPONDING phrase

>>-ADD--+-CORRESPONDING-+--identifier-1--TO--identifier-2------->
        '-CORR----------'                                   

>--+---------+-------------------------------------------------->
   '-ROUNDED-'   

>--+--------------------------------------------+--------------->
   '-+----+--SIZE ERROR--imperative-statement-1-'   
     '-ON-'                                         

>--+-------------------------------------------------+---------->
   '-NOT--+----+--SIZE ERROR--imperative-statement-2-'   
          '-ON-'                                         

>--+---------+-------------------------------------------------><
   '-END-ADD-'   

Elementary data items within identifier-1 are added to and stored in the corresponding elementary items within identifier-2.

For all formats:

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

In format 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.

In format 3, must name an alphanumeric group item or national group item.

The following restrictions apply to date fields:

  • In format 1, identifier-2 can specify one or more date fields. identifier-1 must not specify a date field.
  • In format 2, either identifier-1 or identifier-2 (but not both) can specify at most one date field. If identifier-1 or identifier-2 specifies a date field, then every instance of identifier-3 must specify a date field that is compatible with the date field specified by identifier-1 or identifier-2. That is, they must have the same date format, except for the year part, which can be windowed or expanded.

    If neither identifier-1 nor identifier-2 specifies a date field, identifier-3 can specify one or more date fields without any restriction on the date formats.

  • In format 3, only corresponding elementary items within identifier-2 can be date fields. There is no restriction on the format of these date fields.
  • A year-last date field is allowed in an ADD statement only as identifier-1 and when the result of the addition is a nondate.

There are two steps to determining the result of an ADD statement that involves one or more date fields:

  1. Addition: determine the result of the addition operation, as described under Addition that involves date fields.
  2. Storage: determine how the result is stored in the receiving field. (In formats 1 and 3, the receiving field is identifier-2; in Format 3, the receiving field is the GIVING identifier-3.) For details, see Storing arithmetic results that involve date fields.
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.

When the ARITH(COMPAT) compiler option is in effect, the composite of operands can contain a maximum of 30 digits. When the ARITH(EXTEND) compiler option is in effect, the composite of operands can contain a maximum of 31 digits. For more information, see Arithmetic statement operands and the details on arithmetic intermediate results in Intermediate results and arithmetic precision in the Enterprise COBOL Programming Guide.

ROUNDED phrase

For formats 1, 2, and 3, see ROUNDED phrase.

SIZE ERROR phrases

For formats 1, 2, and 3, see SIZE ERROR phrases.

CORRESPONDING phrase (format 3)

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.

For more information, see Delimited scope statements.



Rate this page

[ Index | Table of contents ]