
COBOL VERBS  COMPUTE
COMPUTE Verb
All the arthemetic, that we can done using verb ADD, SUBTRACT, MULTIPLY,
DIVIDE verbs can be done using COMPUTE statement. In COMPUTE statement,
we need to use following operators to do arthemetic.
operator 
Meaning 
+ 
Add 
 
Subtract 
** 
Exponentiation 
/ 
Divide 
* 
Multiplication 
Format.
COMPUTE < dataitem1> [ROUNDED] ... = arithmeticexpression
[ ON SIZE ERROR < imperative statement1 ] ..
[ NOT ON SIZE ERROR < imperativestatement2> ] ..
Example 1  COMPUTE WSA = WSB + WSC  WSD.
Values in WSB , WSC will be added , WSD value subtracted
from that value and store the final result in WSA.
dataitems values values
before exectuion after execution
WSA 300 850 < changed
WSB 800 800
WSC 100 100
WSD 050 050
It is very important to know the order of evaluation, i.e.., In which order
operations are performed in COMPUT statement. Below table , explains us,
the order of evalution.
Priority 1 **
Priority 2 * or / ( If both appeared , first appeared operator
executed first, from left to right )
Priority 3 + or  ( If both appeared , first appeared operator
executed first, from left to right )
If parentheses appear in the COMPUTE statement, it will override above sequence
priority. Operations within parentheses are performed first.
Statement Order of evaluation
WSA + 10 ** 2 Exponentiation ( 10 ** 2) executed first and the
result 100 will be added to WSA
(WSA + 10 ) ** 2 Addition executed first and exponentiation follows.
ROUNDED 
This option is available with all arithmetic VERBs and it is optional.
COMPUTE WSA = 23.456 + 20.034
When we sum values 23.456 and 20.034 we get the result 40.490. But in this case
WSA picture clause is 99v9 means it can store only 1 decimal position, after
executing above statement WSA contains the value of 40.4 value 90 will get trun
cated. More desirable value is 40.5 in this case. rounded to nearest value.
This can be acheived thru the use of ROUNDED option.
COMPUTE WSA ROUNDED = 23.456 + 20.034
after execution of above statement WSA contains value of 40.5
ON SIZE ERROR
Let take some example to understand this option.
01 WSA PIC 9(3) VALUE 400.
01 WSB PIC 9(3) VALUE 800.
01 WSC PIC 9(3) VALUE 300.
COMPUTE WSA = WSB + WSC.
Above statement, add the value in WSB and WSC and store that value in WSA.
After execution of above statement WSA contains the value of 100, instead of
1100. Because WSA can hold only upto 3 bytes.
Please note that, program wont get abended because this overflow/truncation
condition. But value moved to WSA is not correct one. To aviod these kind
of size errors, best procedure is make sure receiving field has defined with
large enough size to accommodate the result. But sometimes, programmer may
not know the input max number/forget to define the receiving fields with
enough sizes. It is a good practice to use ON SIZE ERROR to catch such
errors.
ON SIZE ERROR option can be used with following arithmetic statements.
 ADD ... ON SIZE ERROR ... .
 SUBTRACT ... ON SIZE ERROR ... .
 MULTIPLY ... ON SIZE ERROR ... .
 DIVIDE ... ON SIZE ERROR ... .
 COMPUTE ... ON SIZE ERROR ... .
COMPUTE WSA = WSB + WSC
ON SIZE ERROR MOVE ZEROES TO WSA.
In above example, If WSA cannot accomodate the result then ZEROES will be
moved to WSA.
A size error can occurs in the following ways.
 When receiving field is not large enough to accommodate the result.
 When division by zero occurs
If ON SIZE ERROR option becomes true, statements after this option will get executed.
These statement needs to be ended either by period OR scope terminator i.e, in case of
ADD, scope terminator is ENDADD.
TIP : Always make sure receiving field has large enough to accommodate the result.

