Similar questions 1 more →
Control Flow & Loop Tracing

Answer & solution
Official answer: B
Prove the invariant x=qy+r by initialization and preservation: (q+1)y+(r−y)=qy+r. The guard ensures subtracting y never makes r negative. Since positive y decreases r at every step, the loop terminates and the false guard gives r<y. Therefore q is the integer quotient and r the remainder with 0≤r<y. A remainder may be zero, so a strict lower bound such as that in C would be wrong.
Solution: Initially q=0, r=x, so x=qy+r. Each iteration replaces r by r−y and q by q+1, preserving that invariant. Positive y decreases nonnegative r on every iteration; termination gives 0≤r<y. Hence the listed postcondition x=qy+r and r<y is B; the full invariant also supplies the nonnegative lower bound.