--------------------------------------------------------------------------
Since numeric computations need valid numbers, each operand on that contributes its value to the calculation must contain a valid number.
-------------------------------------------------------------------------
ADD A TO B
A and B must have valid numeric data because both A and B are used for the calculation.
The results of A + B is recorded in B. In this case B is an operand in the calculation and the field which receives the result of the calculation.
-------------------------------------------------------------------------
ADD A, B GIVING C
Also requires that A and B have a valid numeric data.
C does not require valid numeric data prior to the calculation because the value of C is not part of the formula.
C is the receiving field of the calculation rather than a contributing operand.
-------------------------------------------------------------------------
The field receiving the outcome of the calculation
should have enough digits to contain the answer.
ON SIZE ERROR can be used with any arithmetic
operation.
01 E PIC 9 VALUE 9.
ADD 1 TO E.
The result in E is zero.
ADD 1 TO E
ON SIZE ERROR
DISPLAY ' E needs more integers to hold the result'
NOT ON SIZE ERROR
DISPLAY ' E has enough integers to hold the result'
END-ADD
-------------------------------------------------------------------------
Arithmetic operations can be performed on numeric
data of different types.
Since each numeric type has a different format, a number is valid if it fits the format of its USAGE type.
PIC 999 USAGE DISPLAY.
PIC 99V9 USAGE DISPLAY.
Uses 1 byte per digit of readable EBCDIC values '0' through '9'.
-----------------------------------------------------------------------
PIC S99V9 USAGE DISPLAY
Same readable values as above, except
last digit may contain
for values ending in
'A' through 'I'
+1 through +9
'J' through 'R'
-1 through -9
'{'
+0
'}'
-0
----------------------------------------------------------------------
PIC S999 USAGE COMPUTATIONAL.
PIC 9999 USAGE COMP.
PIC 99 USAGE BINARY.
Not readable. Any bit configuration is a
valid binary number.
the high order bit is often used to indicate negative
numbers.
Do not use for numbers containing decimals.
Any number of digits can be pictured with the comp/binary field.
The number of digits used for the picture determines
the amount of
storage used.
Signed Pictures of storage
size
Value max
9 to 9(4)
2 bytes
32768
9(5) to 9(9)
4 bytes (full word) 2147483648
9(10) to 9(18)
8 bytes (2 full words) 9223372036854775808
-----------------------------------------------------------------------
PIC S999 USAGE COMPUTATIONAL-3.
PIC S999 USAGE COMP-3.
PIC S999 USAGE PACKED-DECIMAL.
Not readable.
Stored 2 digits per byte, except for the last byte
which contains a digit and the sign + as C, - as D, unsigned as F.
------------------------------------------------------------------------
USAGE COMP-1. Single precision floating
point requiring 4 bytes
USAGE COMP-2. Double precision floating
point requiring 8 bytes
Does not need a picture clause.
Both of these forms are used for engineering computations.
The data is stored in scientific notation.
This form is rarely used in COBOL.
-----------------------------------------------------------------------
TEST of CLASS
If the input data cannot be trusted, test whether
it is numeric
before doing calculations,
A PIC 999.
B PIC 999.
C PIC 999.
IF A NUMERIC AND B NUMERIC
ADD A, B GIVING C
ON SIZE ERROR
DISPLAY ' C should be larger to hold the result'
MOVE ZEROS TO C
END-ADD
ELSE
DISPLAY 'The input was not valid'
END-IF
----------------------------------------------------------------------------------------------------------
TEST OF SIGN
If the fields used are signed, a negative number is valid.
However a negative number may not be a valid number for certain entries--such as salary.
Test the field for positive values if that is necessary for a correct result.
SALARY PIC S9(6).
NEW-SALARY PIC S9(6).
IF SALARY NUMERIC AND SALARY POSITIVE
COMPUTE NEW-SALARY ROUNDED
= SALARY * 1.15
ON
SIZE ERROR
DISPLAY ' SALARY does not fit into storage
NOT
ON SIZE ERROR
DISPLAY ' The new SALARY is ' NEW-SALARY
END-COMPUTE
ELSE
DISPLAY 'The input SALARY
was not valid'
END-IF
-------------------------------------------------------------------------
CONDITION-NAMES
Can be used to add meaning to the program
and simplify understanding of what the code is
doing.
Hard to read and/or understand code.
01 CODE-IN PIC X.
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - -
IF CODE-IN = 'A'
PERFORM 100-PROCESS
ELSE
IF CODE-IN = 'B'
PERFORM 200--PROCESS
etc
------------------------------------------------------------------------
More meaningful code by using Condition names.
01 CODE-IN PIC X.
88 POLICY-100 VALUE 'A'.
88 POLICY-200 VALUE 'B'.
etc
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - -
IF POLICY-100
PERFORM 100-PROCESS
ELSE
IF POLICY-200
PERFORM 200--PROCESS
etc
- - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
Maybe the EVALUATE verb is even clearer.
EVALUATE TRUE
WHEN POLICY-100 PERFORM 100-PROCESS
WHEN POLICY-200 PERFORM 200-PROCESS
Etc
END-EVALUATE
-----------------------------------------------------------------------
Condition names can be used for numeric values.
01 TEST-SCORE PIC 999.
88 A-GRADE VALUES ARE 90 THRU
100.
88 B-GRADE VALUES ARE 80 THRU
89.
88 C-GRADE VALUES ARE 70 THRU
79
88 D-GRADE VALUES ARE 60 THRU
69.
88 F-GRADE VALUES ARE 00 THRU
59.
01 LETTER GRADE PIC X.
EVALUATE TRUE
WHEN A-GRADE MOVE 'A' TO LETTER-GRADE.
WHEN B-GRADE MOVE 'B' TO LETTER-GRADE.
WHEN C-GRADE MOVE 'C' TO LETTER-GRADE.
WHEN D-GRADE MOVE 'D' TO LETTER-GRADE.
WHEN F-GRADE MOVE 'F' TO LETTER-GRADE.
WHEN OTHER MOVE '?'
TO LETTER-GRADE.
END-EVALUATE
-----------------------------------------------------------------------