Practice question · True or false
An invariant that holds before the loop and is preserved by each iteration proves the loop correct.
Hints
- Two of the three parts are named, what is missing?
- 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.
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.