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

COBOL - Multiply Statement


The MULTIPLY statement multiplies numeric items and sets the values of data items equal to the results.

The MULTIPLY statement is used to multiply numeric items together. Both formats work slightly differently and each is described in the correspondingly numbered area.

Format 1: MULTIPLY statement

The first format is used to multiply a field or value by another field or value. The product of the field(s) or value(s) listed between MULTIPLY and BY is computed and multiplied by the value of the field(s) following the BY. The answer is stored in the individual field.

Syntax:

MULTIPLY {indentifier-1  or literal-1}...BY {indentifier-2 [ROUNDED]}...
   [ON SIZE ERROR imperative-statement-1]
   [NOT ON SIZE ERROR imperative-statement-2]
[END-MULTIPLY]

identifier-1, identifier-2 Must name an elementary numeric item. identifier-1 and identifier-2 cannot be date fields.

literal-1, literal-2 Must be a numeric literal.

Explanation:
The value of identifier-1 or literal-1 is multiplied by the value of identifier-2; the product is then placed in identifier-2. For each successive occurrence of identifier-2, the multiplication takes place in the left-to-right order in which identifier-2 is specified.

Let see some example,

Example 1:

MULTIPLY A BY B

- The value in A is multiplied by the value in B and the result is stored in B. The value in A is unchange.

Example 2:

MULTIPLY C BY D E

- The value in C is multiplied by the value in D, storing the answer in D and the value of C is also multiplied by E, storing the value in F. The value in C is unchanged.
Format 2: MULTIPLY statement with GIVING phrase

The second format is used by multiply field(s) or value(s) by one another field or value, storing the answer in a different field. The field(s) or value(s) listed between MULTIPLY and GIVING are multiplied and stored in the field(s) following the GIVING.

Syntax:

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

identifier-1, identifier-2 Must name an elementary numeric item. identifier-1 and identifier-2 cannot be date fields.

literal-1, literal-2 Must be a numeric literal.

identifier-3 Must name an elementary numeric or numeric-edited item. identifier-3, the GIVING phrase identifier, is the only identifier in the MULTIPLY statement that can be a date field.

Explanation:
The value of identifier-1 or literal-1 is multiplied by the value of identifier-2 or literal-2. The product is then stored in the data items referenced by identifier-3.

Example:

MULTIPLY A BY B GIVING C

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

Optional phrases in MULTIPLY operation:

ROUNDED phrase:

With ROUNDED option, the computer will always round the result to the PICTURE clause specification of the receiving field. It is usually coded after the field to be rounded.

SIZE ERROR phrases:

If this is coded with any operation that ended with SIZE error will not be carried out but the statement follows ON SIZE ERROR will be executed.

NOT ON SIZE ERROR phrases:

This is opposite to SIZE ERROR. If this is coded with any operation that not ended with SIZE error, then the statement follows ON SIZE ERROR will be executed.

END-MULTIPLY phrase:

This explicit scope terminator serves to delimit the scope of the MULTIPLY statement. END-MULTIPLY permits a conditional MULTIPLY statement to be nested in another conditional statement. END-MULTIPLY can also be used with an imperative MULTIPLY statement.


Tips:

  • The fields to be multiplied 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 both 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