SUBTRACT Statement
The SUBTRACT
Statement performs an arithmetic subtract operation on a number of operands and allows for the storage of the result in a number of data items.
Format 1
SUBTRACT {integer-1} ... FROM {integer-data-1 ROUNDED] } ...
[ ON SIZE ERROR statement-1 ]
[ NOT ON SIZE ERROR statement-2 ]
[ END-SUBTRACT ]
Format 2
SUBTRACT {integer-2} ... FROM integer-3
GIVING { integer-data-2 [ROUNDED] } ...
[ ON SIZE ERROR statement-1 ]
[ NOT ON SIZE ERROR statement-2 ]
[ END-SUBTRACT ]
Format 3
SUBTRACT {CORRESPONDING} group-1 FROM group-2 [ROUNDED]
{CORR }
[ ON SIZE ERROR statement-1 ]
[ NOT ON SIZE ERROR statement-2 ]
[ END-SUBTRACT ]
Syntax:
integer-n
is a data element, literal or data returned from a function call that is an integer.integer-data-n
is a data item that is an integer.group-n
is a group data item containing one or more elementary data items.statement-n
is an imperative stat ement.- The
SIZE ERROR
exception is triggered if the receiving field is not large enough to accommodate the result of theSUBTRACT
function. - In a
SUBTRACT CORRESPONDING
operation, elementary items in separate group items must have the same elementary data name for theSUBTRACT
function to be performed.
General Rules:
- In a
FORMAT 1 SUBTRACT
statement with noGIVING
clause, allinteger-n
data items are subtracted, in sequence, from each of the data items following theFROM
clause. - In a
FORMAT 2 SUBTRACT
statement containing aGIVING
clause, allinteger-n
data items are subtracted, in sequence, from the data item following theFROM
clause with the result stored in the integer-data item following theGIVING
clause - The
ROUNDED
clause is applied when an arithmetic operation produces a result that includes more decimal places than are included in the description of the data item given to hold the final result of the arithmetic operation. - For rules regarding the
ROUNDED
clause, see the entry for ROUNDED in the Common General Rules section. - The
SIZE ERROR
exception is triggered if theON SIZE ERROR
clause is present and if the receiving field is not large enough to accommodate the result of theSUBTRACT
function. - For rules regaring the
ON SIZE ERROR
clause, see the entry for ON SIZE ERROR in the Common General Rules section. - In a
SUBTRACT CORRESPONDING
operation, elementary items in separate group items must have the same elementary data name for theSUBTRACT
function to be performed. - The
SUBTRACT CORRESPONDING
operation causes multipleSUBTRACT
operations to be performed between elementary data items in separate group items, where the elementary items have the same elementary data name, and are not described asFILLER
. - The rules for identifying a
CORRESPONDING
data item for the purposes of theSUBTRACT CORRESPONDING
clause are the same as the rules used for the purposes of theMOVE CORRESPONDING
clause. For more detail, see the General Rules of the MOVE CORRESPONDING clause. - For details on how data items are identified as
CORRESPONDING
, see the entry Rules for identifying CORRESPONDING data in the Common General Rules section. - In a
SUBTRACT CORRESPONDING
operation, allSUBTRACT
operations will be completed before theON SIZE ERROR
condition will be triggered.
Code Sample:
IDENTIFICATION DIVISION.
PROGRAM-ID. SUBTRACT-1.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 FIELD-1 PIC 9V9.
77 FIELD-2 PIC 9V9.
77 FIELD-3 PIC9.9.
01 GROUP-1.
03 FLD-1 PIC 99 VALUE 5.
03 FLD-2 PIC 99 VALUE 15.
01 GROUP-2.
03 FLD-1 PIC 99 VALUE 10.
03 FLD-2 PIC 99 VALUE 20.
77 DUMMY PIC X.
PROCEDURE DIVISION.
MAIN.
*SUBTRACT {INTEGER-1} ... FROM {INTEGER-2 [ROUNDED] } ...
* [ ON SIZE ERROR STATEMENT-1 ]
* [ NOT ON SIZE ERROR STATEMENT-2 ]
* [ END-SUBTRACT ]
*
MOVE 2.5 TO FIELD-1.
MOVE 3.4 TO FIELD-2.
SUBTRACT FIELD-1 FROM FIELD-2
ON SIZE ERROR
DISPLAY "INVALID SUBTRACTION" LINE 10 COL 10
NOT ON SIZE ERROR
DISPLAY FIELD-2 LINE 10 COL 10
END-SUBTRACT.
*SUBTRACT {INTEGER-3} ... FROM INTEGER-4
* GIVING { INTEGER-5 [ROUNDED] } ...
* [ ON SIZE ERROR STATEMENT-1 ]
* [ NOT ON SIZE ERROR STATEMENT-2 ]
* [ END-SUBTRACT ]
*
MOVE 2.5 TO FIELD-1.
MOVE 3.4 TO FIELD-2.
SUBTRACT FIELD-1 FROM FIELD-2 GIVING FIELD-3
ON SIZE ERROR DISPLAY "INVALID SUBTRACTION" LINE 10 COL 10
NOT ON SIZE ERROR DISPLAY FIELD-3 LINE 10 COL 10
END-SUBTRACT.
*SUBTRACT {CORRESPONDING} GROUP-1 FROM GROUP-2 [ROUNDED]
* {CORR }
* [ ON SIZE ERROR STATEMENT-1 ]
* [ NOT ON SIZE ERROR STATEMENT-2 ]
* [ END-SUBTRACT ]
SUBTRACT CORRESPONDING GROUP-1 FROM GROUP-2
ON SIZE ERROR
DISPLAY "INVALID SUBTRACTION" LINE 10 COL 10
NOT ON SIZE ERROR
DISPLAY FLD-1 OF GROUP-2 LINE 12 COL 10
DISPLAY FLD-2 OF GROUP-2 LINE 13 COL 10
END-SUBTRACT.
DISPLAY "SUBTRACT-1 FINISHED!" LINE 15 COL 10.
ACCEPT DUMMY LINE 15 COL 30.
STOP RUN.