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

COBOL - Divide statement


The DIVIDE statement divides one numeric data item into or by others and sets the values of data items equal to the quotient and remainder.

The DIVIDE statement is used to divide numeric items together. Both formats work slightly differently and each is described below,

Format 1: DIVIDE statement

The first format is used to divide a field(s) or value(s) by another field or value. The field or value listed between DIVIDE and INTO is divided into the value of the field(s) following the INTO. The answer is stored if the individual field.

Syntax:

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

The value of identifier-1 or literal-1 is divided into the value of identifier-2, and the quotient is then stored in identifier-2. For each successive occurrence of identifier-2, the division takes place in the left-to-right order in which identifier-2 is specified.

Example 1:

DIVIDE A INTO B

- The value in A is divided into the value in B and the result is stored in B. The value in A is unchanged.

Example 2:

DIVIDE C INTO D E

- The value in C is divided into the value in D, storing the ans er in D; the value of C is also divided into E, storing the value in E. The value in C is unchanged.
Format 2: DIVIDE statement with INTO and GIVING phrases

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

Syntax:

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

The value of identifier-1 or literal-1 is divided into the value of identifier-2 or literal-2. The value of the quotient is stored in each data item referenced by identifier-3.

Example:

DIVIDE A INTO B GIVING C

- The value in A is divided into the value in B and the result is stored in C. The values in A and B are unchanged.
Format 3: DIVIDE statement with BY and GIVING phrases

The third format is also used to divide field(s) or value(s) by one another, storing the answer in a different field. The field(s) or value(s) listed between DIVIDE and GIVING are divided and stored in the field following the field(s) following the GIVING.

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

The value of identifier-1 or literal-1 is divided by the value of identifier-2 or literal-2. The value of the quotient is stored in each data item referenced by identifier-3.

Eample:

DIVIDE A BY B GIVING C 

- The value in A is divided by the value in B and the result is stored in C. The values in A and B are unchanged.
Format 4: DIVIDE statement with INTO and REMAINDER phrases

The fourth format is used to divide field(s) or value(s) by one another, storing the answer in a different field. The field(s) or value(s) listed between DIVIDE and GIVING are divided and stored in the field following the field(s) following the GIVING.

Syntax:

DIVIDE {indentifier-1  or literal-1} INTO {identifier-2 or literal-2} GIVING identifier-3 [ROUNDED] REMAINDER identifier-4
   [ON SIZE ERROR imperative-statement-1]
   [NOT ON SIZE ERROR imperative-statement-2]
[END-DIVIDE]

The value of identifier-1 or literal-1 is divided into identifier-2 or literal-2. The value of the quotient is stored in identifier-3, and the value of the remainder is stored in identifier-4.

Example:
DIVIDE A INTO B GIVING C REMAINDER D
- The value in A is divided into the value in B and the results stored in C with the remainder being stored in D. The values in A and B are unchanged.
Format 5: DIVIDE statement with BY and REMAINDER phrases

The fifth format is also used by divide field(s) or value(s) by one another, storing the answer in a different field. The field(s) or value(s) listed between DIVIDE and GIVING are divided and stored in the field following the field(s) following the GIVING.

Syntax:

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

The value of identifier-1 or literal-1 is divided by identifier-2 or literal-2. The value of the quotient is stored in identifier-3, and the value of the remainder is stored in identifier-4.

Example:

DIVIDE A BY B GIVING C REMAINDER D

- The value in A is divided by the value in B and the result is stored in C with the remainder being stored in D. The values in A and B are unchanged.
Syntax : Explanation

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

identifier-3, identifier-4 Must name an elementary numeric or numeric-edited item.

If identifier-3 or identifier-4 is a date field, see Storing arithmetic results that involve date fields for details on how the quotient or remainder is stored in identifier-3.

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

In formats 1, 2, and 3, floating-point data items and literals can be used anywhere that a numeric data item or literal can be specified.

In formats 4 and 5, floating-point data items or literals cannot be used.


Optional phrases in DIVIDE 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.

END-DIVIDE phrase
This explicit scope terminator serves to delimit the scope of the DIVIDE statement.

END-DIVIDE turns a conditional DIVIDE statement into an imperative statement that can be nested in another conditional statement. END-DIVIDE can also be used with an imperative DIVIDE statement.
Tips:
  • The fields to be divided 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 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