END-IF and the Period
Part of the Decisions and Loops section of Coddy's COBOL Programming journey. Lesson 2 of 20.
A period ends a sentence in COBOL, and it also ends every IF that is still open. Indentation means nothing to the compiler:
IF AMOUNT > 500
DISPLAY "Large amount".
DISPLAY "Sent for review".The period after "Large amount" closes the IF, so Sent for review prints for every amount.
END-IF closes an IF without a period. Write the statements inside with no periods:
IF AMOUNT > 500
DISPLAY "Large amount"
DISPLAY "Sent for review"
END-IF.Dropping the period without adding END-IF is the other trap. The IF then runs until the next period and takes the statements after it along:
IF AMOUNT > 500
DISPLAY "Large amount"
DISPLAY "Done". *> inside the IFA period before ELSE stops the compiler with a syntax error: the IF is already closed, so the ELSE has no IF to belong to. Close every IF with END-IF.
Challenge
EasyThis program prints Sent for review for every amount. Fix it so both review lines print only for amounts over 500, and Done prints every time.
Example input, one line per ACCEPT: 800.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. REVIEW.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AMOUNT PIC 9(5).
PROCEDURE DIVISION.
ACCEPT AMOUNT.
IF AMOUNT > 500
DISPLAY "Large amount".
DISPLAY "Sent for review".
DISPLAY "Done".
STOP RUN.
All lessons in Decisions and Loops
Practice on your own: Online COBOL compiler