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
Selecting the kind of loop
- Types of loops:
- counted loop- performed an exact number of times
- continuously evaluated loop-performed an arbitrary number of times depending on exit condition test done on each iteration
- endless loop-loops forever
- 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
When to use a while loop:
- use a while loop if you're not sure how many times the code should be executed
- a while loop that is tested at the beginning will increase readability
- use a while loop that is tested at the end when the loop needs to execute at least one time
When to use a loop-with-exit loop:
(exit condition is tested in the middle of the loop)
- use this kind of loop to replace code that must be repeated inside and before the loop. This will increase maintainability because all of the code is in one place.
- this kind of loop is easiest to understand
guidelines for using a loop-with-exit loop:
- keep the exit conditions in a group
- add comments to increase clarity
When to use a for loop:
- a for loop is good for loops that itereate a set number of times
- use a for loop for simple loops that don't use internal controls
- if you need to modify the loop index to terminate the loop, use a different looping structure
Controlling the loop
- There are 2 factors to controlling loops:
- minimize the number of factors that influence the loop
- pretend the loop is a routine, keep control outside the loop whenever possible; think of it as a black box
- 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
- 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
- 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
- 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
- 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
- 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)
- 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
Steps to creating a loop
- write the tasks the loop needs to perform in PDL
- convert the comments into code
- create the loop index
- write the exit condition
- 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: Friday, 21-Aug-2020 15:28:16 CST.
Copyright: Department of Computer Science, University of Regina of Regina.
[CS Department]