Ch 15. Controlling Loops

Selecting the kind of loop
When to use a while loop
When to use a loop-with-exit loop
When to use a for loop
Controlling the loop
Steps to creating a loop
  1. Selecting the kind of loop

    1. Types of loops:

      1. counted loop- performed an exact number of times
      2. continuously evaluated loop-performed an arbitrary number of times depending on exit condition test done on each iteration
      3. endless loop-loops forever

    2. Characteristics that determine what kind of loop to use:

      • flexibility-whether the loop is counted, or contiuously evaluated
      • location of test-can be beginning, middle, or end. This tells us whether the loop will be executed at least once, or perhaps not at all.

    * See table 15.1, page 324 Code Complete

  2. When to use a while loop:

  3. When to use a loop-with-exit loop:

    (exit condition is tested in the middle of the loop)
  4. When to use a for loop:



  5. Controlling the loop

    1. There are 2 factors to controlling loops:

      1. minimize the number of factors that influence the loop
      2. pretend the loop is a routine, keep control outside the loop whenever possible; think of it as a black box


    2. Guidelines for entering the loop:

      • enter from one location only, the top
      • keep initialization statements directly before the loop, this increases readability and modifiability
      • don't substitute a for loop when a while loop is more appropriate


    3. Guidelines for the body of the loop:

      • enclose the code in a code block
      • don't use empty loops, they are unclear
      • keep loop control expressions at the beginning or the end of the loop
      • a loop should perform only one task, think of it as a routine


    4. Guidelines for exiting the loop:

      • avoid endless loops, make sure that exit conditions can become true
      • make the loop-termination conditions obvious by keeping the control in one place
      • don't change the value of a for loop counter in the body of the loop, use a while loop instead
      • don't use the loop index in code that is outside the loop
      • use safety counters to prevent errors


    5. Guidelines for using break and continue statements

      • avoid break and continue statements wherever possible
      • a good loop has only one exit statement
      • use break statements instead of boolean flags to increase readability and avoid nested if statements
      • many break statements in one loop is a warning sign that perhaps the loop was not designed carefully
      • when you use a goto statement to simulate a break, go to the first statement after the loop body


    6. Checking endpoints

      • consider first, middle, and last cases to check for off by one errors
      • mentally run through your loop, and check calculations with a calculator


    7. Guidelines for using loop variables:

      • use integers whenever possible
      • use meaningful names to increase clarity in nested loops
      • use meaningful names to avoid mixing up loop-indexes in nested loops (cross talk)


    8. Length of loops:

      • should be short enough that the entire loop body can be viewed at once
      • don't nest deeper than three levels, to increase clarity
      • make sure long loops are especially clear


  6. Steps to creating a loop

    1. write the tasks the loop needs to perform in PDL
    2. convert the comments into code
    3. create the loop index
    4. write the exit condition
    5. write the initializations
* See checklist, page 345 Code Complete


A Good Example of a C++ loop
A Bad Example of a C++ loop
A Good Example of a C loop
A Bad Example of a C loop
A Good Example of a Java loop
A Bad Example of a Java loop


This page was modified by Tanya Douglas at: Sunday, 25-Mar-2012 18:06:43 CST.
Copyright: Department of Computer Science, University of Regina of Regina.


[CS Department]