|
|
 |
|
 |
|
|
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 While expression is evaluated and the decision is made either to begin a new iteration or 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. |
|
|
|
|
|
|
|
|
Notice that the loop exit occurs only at one point: when the loop test is performed. Even though the termination condition may become satisfied midway through the execution of the loop, the current iteration is completed before the computer checks the While expression again. |
|
|
|
|
|
|
|
|
The concept of looping is fundamental to programming. In this chapter, we spend some time looking at typical types of loops and ways of implementing them with the While statement. These looping situations come up again and again when you are analyzing problems and doing top-down designs. |
|
|
|
|
|
|
|
|
Loops Using the While Statement |
|
|
|
|
|
|
|
|
In solving problems, you will come across two major types of loops: countcontrolled loops, which repeat a specified number of times, and eventcontrolled loops, which repeat until something happens within the loop. |
|
|
|
 |
|
 |
|
|
Count-Controlled Loop A loop that executes a specified number of times. |
|
|
|
 |
|
 |
|
|
Event-Controlled Loop A loop that terminates when something happens inside the loop body to signal that the loop should be exited. |
|
|
|
|
|
|
|
|
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. |
|
|
|
|
|