Menu
CoddyTech

IF and ELSE

Part of the Decisions and Loops section of Coddy's COBOL Programming journey. Lesson 1 of 20.

IF runs statements only when a condition is true. ELSE gives the statements for a false condition, and END-IF closes the decision:

IF TOTAL > 100
    DISPLAY "Free shipping"
ELSE
    DISPLAY "Shipping 5"
END-IF.

A condition compares two values. Each symbol has a word form too:

IF TOTAL > 100     *> GREATER THAN
IF TOTAL < 100     *> LESS THAN
IF TOTAL = 100     *> EQUAL TO
IF TOTAL NOT = 100 *> NOT EQUAL TO
IF TOTAL >= 100    *> GREATER THAN OR EQUAL TO

ELSE is optional. Without it, a false condition skips to the statement after END-IF:

IF BALANCE < 100
    DISPLAY "Low balance"
END-IF.
DISPLAY "Checked".

Text is compared character by character, and the shorter value is padded with spaces first. ANSWER = "Y" is true when a PIC X(3) field holds Y and two spaces.

challenge icon

Challenge

Easy

Read AGE, then print Adult when it is 18 or more and Minor otherwise.

Example input, one line per ACCEPT: 30.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. ADULT.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 AGE PIC 999.
       PROCEDURE DIVISION.
           ACCEPT AGE.
           *> Print Adult or Minor here
           STOP RUN.

All lessons in Decisions and Loops