SUBTRACT Statement

The SUBTRACT statement performs arithmetic subtraction.

Format 1

SUBTRACT {number} ... FROM { result [ROUNDED] } ...

   [ ON SIZE ERROR statement-1 ]

   [ NOT ON SIZE ERROR statement-2 ]

   [ END-SUBTRACT ]

Format 2

SUBTRACT {number} ... FROM number 

     GIVING { result [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 ]

Format 4

SUBTRACT TABLE src-table FROM dest-table [ROUNDED]

   [ FROM INDEX src-start TO src-end ]

   [ DESTINATION INDEX dest-start ]

   [ ON SIZE ERROR statement-1 ]

   [ NOT ON SIZE ERROR statement-2 ]

   [ END-SUBTRACT ]

Syntax Rules

  1. number is a numeric literal or elementary numeric data item.
  2. result is an elementary numeric data item or, in Format 2, an elementary numeric edited data item.
  3. group-1 and group-2 are group items containing one or more elementary numeric data items.
  4. statement-1 and statement-2 are imperative statements.
  5. CORR is an abbreviation of CORRESPONDING.
  6. src-table and dest-table are numeric data items that are table elements. The low-order subscript of these items must be omitted. For example, if SRC-1 was an element of a one-dimensional table, you would use SRC-1 in the statement. If SRC-2 was an element of a two-dimensional table, and you wanted to add all the elements in row 2, you would use SRC-2( 2 ).
  7. src-start, src-end and dest-start are numeric literals or data items. These items may not be subscripted.

General Rules

  1. In Format 1, the numbers are added together and the result is then subtracted from each result in turn.
  2. In Format 2, the numbers before the word FROM are added together and the result is subtracted from the number following the word FROM. The results are then moved to each result item.
  3. In Format 3, elementary numeric items in group-1 are subtracted from the corresponding items in group-2. The values are then stored in group-2.
  4. Additional information can be found in Arithmetic Operations, Multiple Receiving Fields, the ROUNDED Option, the SIZE ERROR Option, and the CORRESPONDING Option.
  5. A Format 4 SUBTRACT statement subtracts a range of src-table elements from a range of dest-table elements. The results are stored in dest-table. The first element of the src-table range is subtracted from the first element of the dest-table range, the second element from the second, and so on.
  6. src-start indicates the first element of the source range. If omitted, it defaults to 1. src-end indicates the last element of the range (inclusive). If omitted, it is set to the current upper bound of the source table. In a multidimensional table, the range of elements varies over the innermost OCCURS.
  7. dest-start indicates the first element of the destination range. If omitted, it defaults to 1. Note that the last element of the destination range is dest-start + src-end - 1.
  8. If the SIZE ERROR phrase is used, elements for which the size error condition exists are not updated, while the remaining elements are. If any element gets a size error, then statement-1 executes, otherwise statement-2 executes.
Note: SUBTRACT TABLE is usually substantially faster than the equivalent PERFORM loop. The degree of improvement depends on the size of the range (larger ranges show better improvement). The runtime always performs table boundary checking in SUBTRACT TABLE, even if you do not compile with -Za. The boundaries are tested only at the end points (this is a fast test and there is nothing to be gained from not performing it).