|
|
|
|
|
|
|
In Chapter 5, we said that the flow of control in a program can differ from the physical order of the statements. The physical order is the order in which the statements appear in a program; the order in which we want the statements to be executed is called the logical order. |
|
|
|
|
|
|
|
|
The If statement is one way of making the logical order different from the physical order. Looping control structures are another. A loop executes the same statement (simple or compound) over and over, as long as a condition or set of conditions is met. |
|
|
|
 |
|
 |
|
|
Loop A control structure that causes a sequence of statements to be executed repeatedly. |
|
|
|
|
|
|
|
|
In this chapter, we discuss different types of loops and how they are constructed using the While statement. We also discuss nested loops (loops that contain other loops) and introduce a notation for comparing the amount of work done by different algorithms. |
|
|
|
|
|
|
|
|
The While statement, like the If statement, tests a condition. Here is the syntax template for the While statement: |
|
|
|
|
|
|
|
|
and this is an example of one: |
|
|
|
|
|
|
|
|
while (inputVal != 25)
cin >> inputVal; |
|
|
|
|
|
|
|
|
The While statement is a looping control structure. The statement to be executed each time through the loop is called the body of the loop. In the example above, the body of the loop is the input statement that reads in a value for inputVal. |
|
|
|
|
|