ADD and SUBTRACT
Part of the First Programs section of Coddy's COBOL Programming journey. Lesson 13 of 19.
ADD adds to a field and keeps the result in that field:
ADD 5 TO SCORE.
ADD BONUS TO SCORE.SUBTRACT takes away: SUBTRACT 3 FROM SCORE.
A field used in arithmetic needs a number from the start, so give it VALUE ZERO or a value of its own. A PIC 9 field with no VALUE holds no digits at all, and adding to it stops the program with a data exception.
A result too big for the field loses digits on the left, without a warning.
Challenge
EasyRead BONUS, add it to SCORE, subtract 4 from SCORE, and display SCORE.
Example input, one line per ACCEPT: 5.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. SCORES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SCORE PIC 9(3) VALUE 10.
01 BONUS PIC 9(3).
PROCEDURE DIVISION.
*> Read BONUS, add it, subtract 4
DISPLAY SCORE.
STOP RUN.