Exercise 1: Why COBOL Needs a Priming Read — Possible Solution ==================================================================== WHAT PERFORM UNTIL ACTUALLY NEEDS TO EVALUATE This chapter's own material, building on Chapter 5's real PERFORM UNTIL, explains that a loop's condition has to be a real, already- existing value it can test - something like WS-EOF-FLAG = "Y". A loop condition genuinely can't be written as an instruction to "go do something and see what happens," because a condition is checked, not executed as a real statement of its own. WHY READ CAN'T BE PLACED DIRECTLY INTO THAT CONDITION READ is a real COBOL statement, not an expression that produces a usable true/false result on the spot. This chapter's own finding-box states this directly: there's no real way to write something like PERFORM UNTIL THE NEXT READ REACHES END-OF-FILE, because READ isn't a question a condition can ask - it's an action that has to actually run first, with its own separate AT END phrase reacting afterward by setting a real flag field. WHAT THE PRIMING READ ACTUALLY SOLVES This chapter's own worked example runs one real READ before the loop even begins, so that by the time PERFORM UNTIL first checks its condition, WS-EOF-FLAG genuinely already reflects whether that very first record read successfully or the file turned out to be empty. Without that first, "priming" read, the loop's condition would have to be checked against a flag field that had never actually been set by a real read attempt at all - an unreliable, essentially meaningless starting value. WHY THE SECOND READ INSIDE THE LOOP BODY IS EQUALLY NECESSARY This chapter's own example places a second, matching READ as the very last statement inside the loop body, right after processing the current record. This ensures WS-EOF-FLAG is refreshed with the result of reading the next record before PERFORM UNTIL rechecks its condition at the top of the next pass - keeping the flag genuinely in sync with the real, current state of the file on every single iteration. ANSWER: COBOL needs a priming read because PERFORM UNTIL's condition has to test an already-existing value, and READ is a real statement that has to actually execute first before its AT END phrase can set that value - there's no way to fold "attempt a READ" directly into a loop's own condition. The priming read runs one READ before the loop starts so WS-EOF-FLAG genuinely reflects a real read result the very first time the condition is checked, and a second, matching READ at the end of the loop body keeps that same flag refreshed and accurate for every subsequent pass through the loop. WHY THIS WORKS AS AN ANSWER ------------------------------ This correctly identifies the structural reason a statement can't double as a loop condition, and explains both halves of the priming- read pattern (the read before the loop and the read at the end of the loop body) rather than describing only one of the two required reads.