Loop Design Methodology

There are several steps that we can follow when it comes to loop designs. To properly understand a loop, we must consider

  • pre-condition
  • post-condition
  • loop variant
  • loop invariant

We must properly define all four criteria to ensure a loop that is correct and terminates. Pre-condition must be properly defined and true before the loop executes. Post-condition must be true upon loop termination. Loop variant (exit condition) which controls the execution of a loop must be properly defined to ensure loop termination.

The most important aspect of a loop is the loop invariant. The loop invariant is a statement that is true before and after each iteration of the loop. While the values of variables may change, it is the truth of the loop invariant that does not vary. Many statements can be made that are true both before and after each iteration of the loop, but the loop invariant must relate the work that the loop does such that when the loop is finished the truth of the loop invariant implies the desired result.

Fook Yuen Sin - Team 1