AND, OR and NOT
Part of the Decisions and Loops section of Coddy's COBOL Programming journey. Lesson 4 of 20.
AND joins two conditions that must both be true:
IF AGE >= 13 AND AGE <= 19
DISPLAY "Teen"
END-IF.OR needs at least one of them, and NOT reverses a condition:
IF DAY-CODE = "SAT" OR DAY-CODE = "SUN"
IF NOT BALANCE > 0COBOL works out NOT first, then AND, then OR. Parentheses change the order and make it clear to a reader:
IF TOTAL >= 100
OR (TOTAL >= 50 AND MEMBER = "Y")When the same field starts each comparison, COBOL lets you write it once. These are abbreviated conditions:
IF DAY-CODE = "SAT" OR "SUN"
IF AGE >= 13 AND <= 19Challenge
EasyRead TEMP. Print Comfortable when it is from 18 to 24, both included, and Adjust otherwise.
Example input, one line per ACCEPT: 20.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. COMFORT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TEMP PIC 99.
PROCEDURE DIVISION.
ACCEPT TEMP.
*> Print Comfortable or Adjust
STOP RUN.
All lessons in Decisions and Loops
Practice on your own: Online COBOL compiler