COBOL Compute Statement

COBOL Call Statement COBOL Display Statement

Advertisements


COMPUTE used to calculate the result of arithmetic expressions and stores the same in another variable/data-item.

COMPUTE can use the below arithmetic expressions.

	ADD			-		+
	SUBTRACT		-		-
	MULTIPLY		-		*
	DIVIDE			-		/
	EXPONENT		-		**

COMPUTE is very simple and efficient when compared to the above operations.

COMPUTE statement can be used to write more than one arithmetic statement in a single expression where other arithmetic operation can’t be.

END-COMPUTE is optional with COMPUTE.

END-COMPUTE will not be required when COMPUTE statement ended with period.

Syntax:
	COMPUTE identifier-1 [ROUNDED] =
		Identifier-2  identifier-3	
		[ON SIZE ERROR ]
		[NOT ON SIZE ERROR ]
	END-COMPUTE.

In the above syntax, ROUNDED, ON SIZE ERROR and NOT ON SIZE ERROR are optional.

Let’s discuss the options one by one.

ROUNDED:

ROUNDED option used to round the fraction result of the compute statement exceeds the length of the target data item fractional places.

If the result of the COMPUTE statement exceeds the length of the target data item fractional places, those will not be rounded until or unless the ROUNDED option specifies. The result will be truncated.

If the ROUNDED option specifies, the last digit of fraction place will be increased by 1 if the next fraction value greater than 5.

SIZE ERROR:

SIZE ERROR is optional in COMPUTE statement.

SIZE ERROR mostly occurs for the cases like

1. When the result of arithmetic expression exceeds the length of declaration for target item.

2. When divided by zero occurs.

3. When exponential errors occurred.


If any questions on the above, post your queries on Cobol Discussion forum


COBOL Call Statement COBOL Display Statement

Advertisements