Exercises — Chapter 7: Iteration Patterns
Exercise 7.1: PERFORM TIMES vs. PERFORM UNTIL (Beginner)
Write a program that displays a multiplication table for a number entered by the user (via ACCEPT).
Part A: Use PERFORM ... TIMES to display the first 12 multiples (e.g., for input 7: "7 x 1 = 7", "7 x 2 = 14", ... "7 x 12 = 84").
Part B: Rewrite the same program using PERFORM ... VARYING.
Part C: Rewrite it again using inline PERFORM with END-PERFORM.
Compare the three versions. Which is most readable? Which gives you the most control?
Exercise 7.2: TEST BEFORE vs. TEST AFTER (Beginner)
Write two versions of a number-guessing game:
Version A (TEST BEFORE):
PERFORM 2100-GET-GUESS
UNTIL WS-GUESS = WS-TARGET
Version B (TEST AFTER):
PERFORM 2100-GET-GUESS
WITH TEST AFTER
UNTIL WS-GUESS = WS-TARGET
For each version: 1. What happens if the user guesses correctly on the first attempt? 2. How many times is the paragraph executed in each case? 3. Which version requires a "priming" ACCEPT?
Test both versions and document the behavior differences.
Exercise 7.3: Table Traversal (Intermediate)
Define a table of 20 student records with name (PIC X(20)) and grade (PIC 9(03)). Populate it with sample data using VALUE clauses or MOVE statements.
Write paragraphs using PERFORM VARYING to: 1. Display all students and their grades 2. Find the highest grade and the student who earned it 3. Count how many students have grades above 80 4. Calculate the class average 5. Find a specific student by name (linear search)
For task 5, demonstrate the proper pattern for checking whether the search succeeded or failed after the PERFORM VARYING loop exits.
Exercise 7.4: Two-Dimensional Table (Intermediate)
Create a program that manages a seating chart for a theater with 10 rows and 15 seats per row.
Data structure:
01 WS-THEATER.
05 WS-ROW OCCURS 10 TIMES.
10 WS-SEAT OCCURS 15 TIMES.
15 WS-SEAT-STATUS PIC X(01).
88 SEAT-EMPTY VALUE 'E'.
88 SEAT-SOLD VALUE 'S'.
88 SEAT-HELD VALUE 'H'.
Tasks using PERFORM VARYING ... AFTER: 1. Initialize all seats to EMPTY 2. Display the seating chart (show E, S, or H for each seat) 3. Count and display total empty, sold, and held seats 4. Find the first available seat (row, column) — exit the loop when found 5. Mark a specific seat as sold (accept row and seat number from user)
Exercise 7.5: Defensive Loop Programming (Intermediate)
Write a batch processing program that reads a sequential file of transaction records. Implement all three defensive strategies from Section 7.11:
- Maximum iteration counter: Limit processing to 1,000,000 records
- Progress verification: Detect if the same account number appears more than 100 times consecutively
- Checkpoint logging: Display a progress message every 5,000 records
Testing scenarios: - Create a test file with 50 records and verify normal processing - Create a test scenario where the end-of-file flag is never set (simulate by commenting out the AT END clause) and verify the safety counter stops the loop - Create a test file with 150 consecutive identical account numbers and verify the progress check catches it
Exercise 7.6: PERFORM THRU with Exit Pattern (Intermediate)
Rewrite the following nested IF logic using the PERFORM THRU pattern with guard clauses:
IF WS-INPUT-VALID = 'Y'
IF WS-CUSTOMER-EXISTS = 'Y'
IF WS-CREDIT-OK = 'Y'
IF WS-INVENTORY-AVAILABLE = 'Y'
PERFORM 3100-PLACE-ORDER
ELSE
MOVE 'OUT OF STOCK' TO WS-MSG
END-IF
ELSE
MOVE 'CREDIT DENIED' TO WS-MSG
END-IF
ELSE
MOVE 'CUSTOMER NOT FOUND' TO WS-MSG
END-IF
ELSE
MOVE 'INVALID INPUT' TO WS-MSG
END-IF
Your solution should use PERFORM THRU with an exit paragraph, and GO TO for guard clause exits. Maximum nesting depth should be 1.
Exercise 7.7: Simulating Recursion — Factorial (Advanced)
Implement a factorial calculator using the stack-based recursion pattern from Section 7.10.
Requirements: 1. Accept a number N from the user (1-20) 2. Use an explicit stack to simulate the recursive calls 3. Display each "push" and "pop" operation to trace the execution 4. Display the final result
Example output for N=5:
Push: N=5
Push: N=4
Push: N=3
Push: N=2
Push: N=1
Pop: N=1, result=1
Pop: N=2, result=2
Pop: N=3, result=6
Pop: N=4, result=24
Pop: N=5, result=120
Factorial of 5 = 120
Exercise 7.8: EXIT PERFORM Patterns (Advanced)
Note: Requires COBOL 2002+ compiler (IBM Enterprise COBOL V4.2+ or GnuCOBOL)
Write a program that demonstrates EXIT PERFORM and EXIT PERFORM CYCLE with a practical example:
Scenario: You have a table of 50 product records. Write inline PERFORM loops that:
-
Search with EXIT PERFORM: Find the first product with price > $100 and display it. Use EXIT PERFORM to stop searching once found.
-
Filter with EXIT PERFORM CYCLE: Display all products except those with status 'D' (discontinued). Use EXIT PERFORM CYCLE to skip discontinued products.
-
Bounded search with both: Search for a product by code, but skip products with status 'D', and stop if you encounter a product with code > search target (assuming sorted table). Use both EXIT PERFORM and EXIT PERFORM CYCLE.
For each task, also write the equivalent version using condition flags (for pre-2002 compilers) and compare readability.