Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
← Back to Debugging
Debugging | Intermediate | COBOL

Fix Infinite Loop

Problem Description

This code runs forever. Find and fix the bug:
PERFORM UNTIL WS-I > 10
DISPLAY WS-I
END-PERFORM.

Expected Output

Should display 1 to 10 and stop

Hints

The loop variable must be modified inside the loop.

Solution

IDENTIFICATION DIVISION. PROGRAM-ID. FIXLOOP. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-I PIC 9(2) VALUE 1. PROCEDURE DIVISION. * BUG: WS-I never changes, so loop never ends * FIX: Add 1 to WS-I inside the loop PERFORM UNTIL WS-I > 10 DISPLAY WS-I ADD 1 TO WS-I END-PERFORM. DISPLAY "LOOP COMPLETED". STOP RUN.

Explanation:

Loop variable must change to eventually satisfy UNTIL condition.