|
|
|
|
|
|
|
Figure 6-2
A Comparison of If and While |
|
|
|
|
|
|
|
|
Although in some ways the If and While statements are alike, there are fundamental differences between them (see Figure 6-2). In the If structure, Statement1 is either skipped or executed exactly once. In the While structure, Statement1 can be skipped, executed once, or executed over and over. The If is used to choose a course of action; the While is used to repeat a course of action. |
|
|
|
|
|
|
|
|
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 the loop entry. |
|
|
|
|
|
|
|
|
Each time the body of a loop is executed, a pass is made through the loop. This pass is called an iteration. |
|
|
|
|
|
|
|
|
Before each iteration, control is transferred to the loop test at the beginning of the loop. |
|
|
|
|
|
|
|
|
When the last iteration is complete and the flow of control has passed to the first statement following the loop, the program has exited the loop. The condition that causes a loop to be exited is the termination condition. In the case of a While loop, the termination condition is that the While expression becomes zero (FALSE). |
|
|
|
|
|