5.2 Looping | 223
Phases of Loop Execution
The body of a loop is executed in several phases:
The moment that the flow of control reaches the first statement inside
the loop body is theloop entry.
Each time the body of a loop is executed, a pass is made through the
loop. This pass is called aniteration.
Before each iteration, control is transferred to the loop testat the begin-
ning of the loop.
When the last iteration is complete and the flow of control has passed
to the first statement following the loop, the code has exited the loop.The
condition that causes a loop to be exited is the termination condition.In
the case of a whileloop, the termination condition is that the while
expression becomes false.
Notice that the loop exit occurs at only one point: when the loop test is per-
formed. Even though the termination condition may become satisfied midway
through the execution of the loop, the current iteration is completed before the com-
puter checks thewhileexpression again.
The concept of looping is fundamental to programming. In this chapter, we will
spend some time looking at typical kinds of loops and ways of implementing them
with the whilestatement. These looping situations come up again and again when
you are analyzing problems and designing algorithms.
Loops Using the while Statement
In solving problems, you will encounter two major types of loops: count-controlled
loops, which repeat a specified number of times, and event-controlled loops, which
repeat until something happens within the loop. In the context of a loop, we use
the word eventto mean a specific condition that we expect to occur during some
iteration of the loop, and that can be tested by a Boolean expression.
If you are making an angel food cake and the recipe reads, “Beat the mixture 300 strokes,”
you are executing a count-controlled loop. If you are making a pie crust and the recipe reads,
“Cut with a pastry blender until the mixture resembles coarse meal,” you are executing an
event-controlled loop; you don’t know ahead of time the exact number of loop iterations.
Count-Controlled Loops
A count-controlled loop uses a variable called the loop control variablein the loop test. Before
we enter a count-controlled loop, we must initialize(set the initial value of ) the loop control
variable and then test it. Then, as part of each iteration of the loop, we must increment(in-
crease by 1) the loop control variable. Here’s an example:
Loop entry The point at which
the flow of control reaches the
first statement inside a loop
Iteration An individual pass
through, or repetition of, the
body of a loop
Loop test The point at which
the whileexpression is
evaluated and the decision is
made either to begin a new iter-
ation or to skip to the statement
immediately following the loop
Loop exit The point at which
the repetition of the loop body
ends and control passes to the
first statement following the
loop
Termination condition The
condition that causes a loop to
be exited
Count-controlled loop A loop
that executes a specified num-
ber of times
Event-controlled loop A loop
that terminates when
something happens inside the
loop body to signal that the
loop should be exited