Skip to main content
eLearner.app
Module 2 · Lesson 2 of 24/10 in the course~12 min
Module lessons (2/2)

Decisions with IF and EVALUATE

Decision making in COBOL is performed primarily using the IF conditional statement and the multi-branch selection structure EVALUATE (analogous to switch or match in modern languages).

The IF Conditional Statement

The fundamental syntax of the IF statement is:

Code
IF condition
    statements-if-true
ELSE
    statements-if-false
END-IF.

In modern COBOL versions (COBOL-85 and later), the statement must explicitly end with END-IF. instead of relying solely on the final period ., significantly reducing parsing and logic errors.

Common Conditions

  • Greater than: > or GREATER THAN
  • Less than: < or LESS THAN
  • Equal to: = or EQUAL TO
  • Greater than or equal to: >= or GREATER THAN OR EQUAL TO
  • Less than or equal to: <= or LESS THAN OR EQUAL TO
Code
IF WS-BALANCE < 0
    DISPLAY "OVERDRAWN"
ELSE
    DISPLAY "ACCOUNT ACTIVE"
END-IF.

The EVALUATE Statement

When we need to compare a variable against multiple discrete values, nested IF statements can become complex. The EVALUATE statement resolves this by structuring the choices in a clean way:

Code
EVALUATE WS-USER-ROLE
    WHEN "ADMIN"
        DISPLAY "FULL ACCESS"
    WHEN "EDITOR"
        DISPLAY "EDIT ACCESS"
    WHEN OTHER
        DISPLAY "LIMITED ACCESS"
END-EVALUATE.
  • WHEN OTHER: Identifies the fallback branch (equivalent to default or the final else in modern languages).
  • END-EVALUATE.: Closes the structure.

Try it yourself

Exercise#cobol.m2.l2.e1
Attempts: 0Loading…

Write an IF-ELSE-END-IF block that checks if the variable WS-USER-AGE is greater than or equal to 18. If true, print 'ADULT', otherwise print 'MINOR' using DISPLAY.

Loading editor…
Show hint

Use >= for the comparison, DISPLAY for output, and END-IF. (with the dot at the end of the block).

Solution available after 3 attempts

Exercise#cobol.m2.l2.e2
Attempts: 0Loading…

Complete the EVALUATE structure to check the WS-STATUS variable. If the value is 'A' print 'ACTIVE', if it is 'I' print 'INACTIVE', otherwise for any other value print 'UNKNOWN'.

Loading editor…
Show hint

Write each WHEN branch followed by its DISPLAY, and use WHEN OTHER to handle the fallback.

Solution available after 3 attempts