typestar

ADD and SUBTRACT in COBOL

Verb-first arithmetic that reads like an order to a stockroom clerk.

IDENTIFICATION DIVISION.
PROGRAM-ID. ADD-SUBTRACT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 BIN-A     PIC 9(3) VALUE 250.
01 BIN-B     PIC 9(3) VALUE 175.
01 SOLD-LB   PIC 9(3) VALUE 60.
01 TOTAL-LB  PIC 9(4).
PROCEDURE DIVISION.
*> Verb-first arithmetic reads like an order to a clerk.
    ADD BIN-A TO BIN-B GIVING TOTAL-LB
    SUBTRACT SOLD-LB FROM TOTAL-LB
    DISPLAY "Apples on hand: " TOTAL-LB " lb"
    STOP RUN.

How it works

  1. ADD BIN-A TO BIN-B GIVING TOTAL-LB leaves both bins untouched and fills the total.
  2. SUBTRACT SOLD-LB FROM TOTAL-LB updates the total in place.
  3. The result field 9(4) is a digit wider than the bins, so the sum always fits.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
406
Tokens
56
Three-star pace
45 tpm

At the three-star pace of 45 tokens a minute, this run takes about 75 seconds.

Type this snippet

Step 1 of 3 in Arithmetic, step 10 of 27 in Language basics.

← Previous Next →