Mainframe Path Start learning free
Core7 min readLesson 4 of 5

Logic, files and calling other programs

COBOL's control flow is small: conditionals, PERFORM loops, EVALUATE for multi-way choices, and CALL to run another program. File I/O is verb-based and always tells you what happened through a status code.

EVALUATE: the readable multi-way branch

EVALUATE replaces nested IFs
           EVALUATE TRUE
               WHEN CUST-BALANCE > 100000
                   MOVE 'PREMIUM' TO WS-TIER
               WHEN CUST-BALANCE > 10000
                   MOVE 'STANDARD' TO WS-TIER
               WHEN CUST-CLOSED
                   MOVE 'CLOSED' TO WS-TIER
               WHEN OTHER
                   MOVE 'BASIC' TO WS-TIER
           END-EVALUATE

File verbs and FILE STATUS

Always declare FILE STATUS IS WS-STATUS on the SELECT and check it after every operation. Without it, an I/O failure gives you an abend with no context; with it, you know exactly which file and which condition.

StatusMeans
00Successful
02Successful, duplicate key on an alternate index
10End of file
22Duplicate key — the record already exists
23Record not found
35File not found at OPEN — usually a missing DD
37Open failed on an incompatible device or format
39Attribute mismatch between program and file
9xVendor-specific; check the compiler's list

Tables and subscripts

OCCURS, indexes and a search
       01  WS-RATE-TABLE.
           05  WS-RATE-ENTRY OCCURS 50 TIMES
                             INDEXED BY WS-RATE-IX.
               10  WS-RATE-CODE   PIC X(03).
               10  WS-RATE-VALUE  PIC S9(3)V9(4) COMP-3.

           SET WS-RATE-IX TO 1
           SEARCH WS-RATE-ENTRY
               AT END
                   DISPLAY 'RATE NOT FOUND: ' WS-IN-CODE
                   MOVE 16 TO RETURN-CODE
               WHEN WS-RATE-CODE(WS-RATE-IX) = WS-IN-CODE
                   MOVE WS-RATE-VALUE(WS-RATE-IX) TO WS-RATE
           END-SEARCH

Calling another program

CALL with parameters
* in the caller
       01  WS-PARM-AREA.
           05  WS-IN-CODE     PIC X(03).
           05  WS-OUT-RATE    PIC S9(3)V9(4) COMP-3.
           05  WS-RETURN      PIC S9(4) COMP.

           CALL 'RATELKUP' USING WS-PARM-AREA

* in the called program RATELKUP
       LINKAGE SECTION.
       01  LK-PARM-AREA.
           05  LK-IN-CODE     PIC X(03).
           05  LK-OUT-RATE    PIC S9(3)V9(4) COMP-3.
           05  LK-RETURN      PIC S9(4) COMP.
       PROCEDURE DIVISION USING LK-PARM-AREA.

Example, line by line

COBOLWhat it means
PERFORM UNTIL WS-EOF
Loop until the end-of-file condition (an 88-level) becomes true.
READ INFILE
Read the next record from the file the program calls INFILE.
AT END SET WS-EOF TO TRUE
No more records: set the flag so the loop stops.
NOT AT END PERFORM 2000-PROCESS
A record was read: process it in its own paragraph.
END-READ
Scope terminator: safer than relying on a full stop.
END-PERFORM
End of the loop body.

Try it yourself

TRY IT YOURSELF

Which statement should a called subprogram use to return control to its caller?

Show a hint

STOP RUN would end the whole run unit, not just the subprogram.

Show the solution

GOBACK. It returns to the caller; in a main program it also ends the run.

Common mistakes

Not checking FILE STATUS

Every I/O failure becomes a mysterious abend instead of a clear message naming the file and the condition.

Mismatched parameter layouts between caller and callee

There is no compile-time check across programs. A layout change on one side and not the other produces corrupted data, not an error.

Testing without SSRANGE

Subscript errors that quietly corrupt storage in test become dramatic failures in production with more data.

Leaving a called program's WORKING-STORAGE dirty

Values persist between calls. Initialise at entry unless persistence is intended.

What you will see at work

Key terms

Check your understanding.
Take this lesson's quiz and save your progress. Free.

Take the lesson quiz
← PICTURE clauses and how data is storedCompile, link, run — and debugging S0C7 →