Computer Science I / Loop Invariants and Correctness Ideas
Practice question · True or false

An invariant that holds before the loop and is preserved by each iteration proves the loop correct.

Hints
  1. Two of the three parts are named, what is missing?
  2. What does the loop's exit condition contribute?
Show the answer

False

Why

False. Those two parts give partial correctness at best: you also need the exit condition, which combined with the invariant yields the result, and a termination argument showing the loop ends at all. while true preserves any invariant you like and proves nothing.

Read the lesson: Loop Invariants and Correctness Ideas →

Practise Loop Invariants and Correctness Ideas

The app has 7 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.

More questions on Loop Invariants and Correctness Ideas