arithmetic

Command:

ADD

Syntax:

Variant 1:

ADD <number or variable> … TO <target-variable> [ROUNDED]

Variant 2:

ADD <number or variable> … TO <number or variable> GIVING <target-variable> [ROUNDED]

Description:

Variant 1 of the ADD statement is used to add one or more numeric literals and/or numeric variables together, storing the result in the numeric target-variable. All literals and variables are added together to produce the result, including the value of target-variable prior to the addition.

Variant 2 of ADD is used to add one or more numeric literals and/or variables together, with the result stored in a target-variable whose original contents are not considered in the addition. Thus, if var has an initial value of 1, performing the operation:

ADD 1 TO 1 GIVING var.

will place a value of 2, not 3, into var.

Both forms of ADD permit the use of the ROUNDED keyword, which rounds the target variable, after computation, to the nearest integer.

Example Usage:

Variant 1:

ADD 1 TO num_variable.

ADD 1 2 3 TO num_variable.

ADD var TO total.

ADD 1.11 2 var TO total ROUNDED.

Variant 2:

ADD value TO subtotal GIVING total.

ADD 9.99 value TO subtotal GIVING total ROUNDED

See Also:

COMPUTE

SUBTRACT

MULTIPLY

DIVIDE

Sample Program:

ADD.CBL




Command:

MULTIPLY

Syntax:

Variant 1:

MULTIPLY <number or variable> … BY <target-variable> [ROUNDED]

Variant 2:

MULTIPLY <number or variable> … BY <number or variable> GIVING <target-variable> [ROUNDED]

Description:

Variant 1 of MULTIPLY is used to multiply one or more numeric literals and/or numeric variables together, storing the result in the numeric target-variable. All literals and variables are multiplied together to produce the result, including the value in target-variable prior to the multiplication.

Variant 2 of MULTIPLY is used to multiply one or more numeric literals and/or variables together, with the result stored in target-variable, whose original contents are not considered in the multiplication. Thus, if VAR has an initial value of 3, performing the operation MULTIPLY 2 BY 2 GIVING VAR will place a value of 4, not 12, into VAR.

Both forms of MULTIPLY permit the use of the ROUNDED keyword, which rounds the target variable (after computation) to the nearest integer.

Example Usage:

Variant 1:

MULTIPLY 2 BY num.

MULTIPLY 2 3 BY num.

MULTIPLY value BY total.

MULTIPLY 1.11 2 value BY total ROUNDED.

Variant 2:

MULTIPLY value BY subtotal GIVING total.

MULTIPLY 2 BY 3 GIVING total ROUNDED.

See Also:

COMPUTE

ADD

SUBTRACT

DIVIDE

Sample Program:

MULTIPLY.CBL





Command:

SUBTRACT

Syntax:

Variant 1:

SUBTRACT <number or variable> … FROM <target-variable> [ROUNDED]

Variant 2:

SUBTRACT <number or variable> … FROM <number or variable> GIVING <target-variable> [ROUNDED

Description:

Variant 1 of SUBTRACT is used to subtract one or more numeric literals and/or numeric variables from a numeric target-variable, with the result stored in the target-variable. All literals and variables to the left of the FROM keyword are subtracted from the target-variable in order to determine its new value.

Variant 2 of SUBTRACT is used to subtract one or more numeric literals and/or numeric variables from a numeric literal or variable, with the result stored in the target-variable. All literals and variables to the left of the FROM keyword are subtracted from the literal or variable that is between the FROM and GIVING keywords in order to arrive at the new value for target-variable. Thus, if num_var has an initial value of 1, performing the operation:

SUBTRACT 1 FROM 3 GIVING num_var

will place a value of 2 into num_var.

Both forms of SUBTRACT permit the use of the ROUNDED keyword, which rounds the target variable (after computation) to the nearest integer.

Example Usage:

Variant 1:

SUBTRACT 1 FROM num.

SUBTRACT 1 2 3 FROM num.

SUBTRACT value FROM total.

SUBTRACT 1.11 2 value FROM total ROUNDED.

Variant 2:

SUBTRACT value FROM subtotal GIVING total.

SUBTRACT 9.99 value FROM subtotal GIVING total ROUNDED.

See Also:

COMPUTE

ADD

MULTIPLY

DIVIDE

Sample Program:

SUBTRACT.CBL





Command:

DIVIDE

Syntax:

Variant 1:

DIVIDE <number or divisor-variable1> … INTO <dividend-variable> [ROUNDED]

Variant 2:

DIVIDE <number or divisor-variable1> … INTO <number or dividend-variable> GIVING <result-variable> [ROUNDED] [REMAINDER <remainder-variable>]

Variant 3:

DIVIDE <number or dividend-variable> BY <number or divisor-variable> GIVING <result-variable> [ROUNDED] [REMAINDER <remainder-variable>]

If a REMAINDER clause is specified in Variant 2 of the DIVIDE statement, only a single divisor may be specified. Only one divisor and one dividend may be specified in Variant 3 of the DIVIDE statement, regardless of whether the REMAINDER clause is used.

Description:

Variant 1 of the DIVIDE statement is used to divide one or more numbers and/or numeric divisor-variables into a target numeric dividend-variable. The result is stored in the dividend-variable, and its previous value is overwritten. This form of DIVIDE is equivalent to the COMPUTE statement:

COMPUTE dividend-variable =

dividend-variable/divisor-variable1/divisor-variable2/… .

Variant 2 of the DIVIDE statement is used to divide one or more numbers and/or divisor-variables into a number or dividend-variable, and the result is stored in a separate result-variable, thereby preserving the value in the dividend-variable. This form of DIVIDE is equivalent to the COMPUTE statement:

COMPUTE result-variable =

dividend-variable/divisor-variable1/divisor-variable2/… .

Variant 3 of the DIVIDE statement is used to divide a number or dividend-variable by a single number and/or divisor-variable. The result is stored in a separate result-variable. This form of DIVIDE is equivalent to the COMPUTE statement:

COMPUTE result-variable = dividend-variable/divisor-variable.

Variants 2 and 3 of DIVIDE permit the usage of the REMAINDER keyword, which stores the remainder from the division operation in a separate remainder-variable. The remainder is the portion of the dividend that would be left over if the result were forced to be an integer value. Using the REMAINDER keyword in a DIVIDE statement is equivalent to executing two separate COMPUTE statements, the first the actual division, and the second the remainder calculation using the modulus (%) operator:

COMPUTE result-variable = dividend-variable/divisor-variable.

COMPUTE remainder-variable = dividend-variable % divisor-variable.

All variants of DIVIDE permit the use of the ROUNDED keyword, which rounds the target variable, after computation, to the nearest integer.

Example Usage:

Variant 1:

DIVIDE 1 INTO num_variable.

DIVIDE 1 2 3 INTO num_variable.

DIVIDE value_var INTO total.

DIVIDE 1.11 2 value_var INTO total ROUNDED.

Variant 2:

DIVIDE value_var INTO subtotal GIVING total.

DIVIDE 9.99 value_var INTO subtotal

GIVING result ROUNDED.

DIVIDE value_var INTO subtotal

GIVING result ROUNDED

REMAINDER remainder.

Variant 3:

DIVIDE subtotal BY value_var GIVING result.

DIVIDE subtotal BY value_var GIVING result ROUNDED.

DIVIDE subtotal BY value_var GIVING result ROUNDED

REMAINDER remainder.

See Also:

COMPUTE

ADD

SUBTRACT

MULTIPLY

Sample Program:

DIVIDE.CBL