GATE / CS ARCHIVE

GATE CSE 1991 / QUESTION 4(ii)

Converting repeat-until to while

Programming in C · Control Flow & Loop Tracing · Full GATE CSE 1991 paper

GATE 1991, Q4(ii) 2 marks · Descriptive
Report issue

Control Flow & Loop Tracing

Question 4(ii), GATE 1991: Rewrite Pascal repeat S until B using a while construct with equivalent behavior.
Question 4(ii), GATE 1991: Rewrite Pascal repeat S until B using a while construct with equivalent behavior.
Tap to enlarge
Answer & solution

Answer: begin S; while not B do S end (no official key was published)

A repeat loop executes S before checking B, so S runs at least once. Execute S once explicitly, then test not B in the while guard. Whenever B remains false, execute S again; stop immediately after an iteration makes B true. Merely writing while not B do S would incorrectly permit zero executions when B starts true. This transformation preserves the sequence of evaluations of B after executions of S, including ordinary side effects in the condition. If S is a multi-statement sequence, it must be grouped as a Pascal compound statement.

Original paper, page 5