CS FundamentalsHardGATE CS

A loop `while i < n: i = i + 1` (integers) has the invariant i ≤ n. To prove TOTAL correctness rather than only partial correctness, what else must be shown?

  1. A. That the invariant i ≤ n still holds at the moment the loop exits
  2. B. A variant like n − i, bounded below and falling every iteration
  3. C. That the postcondition implies the precondition of the whole loop
  4. D. That every statement in the loop body is executed at least once

Correct answer: B. A variant like n − i, bounded below and falling every iteration

Partial correctness says the result is right if the loop ends; termination needs a well-founded measure. n − i stays ≥ 0 while the guard holds and drops by 1 each pass, so the loop must stop.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic