Pre- versus Post-Test

A pre-test loop is one in which the condition is tested before the code block is executed.

This means that the code block may never be executed.

The steps that take place in a pre-test loop are:

  1. Initialize the looping variable
  2. Test the looping variable
  3. If the test is false, exit the loop
  4. If the test is true
  5. execute the code
  6. increment/decrement the looping variable
  7. return to step 2

A post-test loop is one in which the condition is tested after the code block is executed.

This means that the code block will always execute at least once.

The steps that take place in a post-test loop are:

  1. Initialize the looping variable
  2. Execute the code
  3. Test the looping variable
  4. If the test is false, exit the loop
  5. If the test is true
  6. increment/decrement the looping variable
  7. return to step 2