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
ADD BIN-A TO BIN-B GIVING TOTAL-LBleaves both bins untouched and fills the total.SUBTRACT SOLD-LB FROM TOTAL-LBupdates the total in place.- The result field
9(4)is a digit wider than the bins, so the sum always fits.
Keywords and builtins used here
ADDDATADISPLAYDIVISIONIDENTIFICATIONPROCEDURESECTIONSTOPSUBTRACT
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.
Step 1 of 3 in Arithmetic, step 10 of 27 in Language basics.