Looping
4
In this chapter, we will begin to explore the topic of repetition structures
(also called loops). Loops contain a block of statements that can be executed repeatedly. We will discuss different types of loops and more advanced loop applications. The discussion of loops continues in Chapter 5.
ISBN 1-256-14455-X
After reading this chapter, you will be able to do the following:
Distinguish between pre-test and post-test loops [Section 4.1]
Identify infinite loops and loops that never get executed [Section 4.1]
Create a flowchart using the loop structure [Section 4.1]
Use relational and logical operators in loop conditions [Section 4.1]
Construct counter-controlled loops [Section 4.2]
Use counter-controlled loops to increment or decrement the counter by any integer value [Section 4.2]
Construct For loops [Section 4.3]
Create test conditions to avoid infinite loops and loops that never get executed [Section 4.3]
Construct sentinel-controlled loops [Section 4.4]
Use the Int function [Section 4.4]
Apply loops to data input and validation problems [Section 4.4]
Apply loops to compute sums and averages [Section 4.4]
163
Prelude to Programming: Concepts and Design, Fifth Edition, by Stewart Venit and Elizabeth Drake. Published by Addison-Wesley. Copyright © 2011 by Pearson Education, Inc.
164
Chapter 4
In the
Everyday
World
Repetition Structures: Looping
Loops
You may not remember, but you probably learned to walk about the time you were a year old. As you took your first step you had to figure out how to execute the following process:
Put one foot in front of the other
At some point you did just that, and it was a major accomplishment. But this didn’t get you very far. If you wanted to walk across the room, you needed to extend this process to the following:
Put the left foot in front of the right foot
Put the right foot in front of the left foot
Put the left foot in front of the right foot
Put the right foot in front of the left