- 4.1 Introduction
- 4.2 Algorithms
- 4.3 Pseudocode
- 4.4 Control Structures
- 4.5 If...Then Selection Statement
- 4.6 If...Then...Else Selection Statement
- 4.7 Nested If...Then...Else Statements
- 4.8 Repetition Statements
- 4.9 Compound Assignment Operators
- 4.10 Formulating Algorithms: Counter-Controlled Repetition
- 4.11 Formulating Algorithms: Nested Control Statements
- 4.12 Using the Debugger: Locating a Logic Error
- 4.13 Wrap-Up
- Self-Review Exercises
- Answers to Self-Review Exercises
- Quick Quiz
- Making a Difference Exercises
4.8 Repetition Statements
A repetition statement (also called a looping statement, an iteration statement or a loop) allows you to specify that an action should be repeated, depending on the value of a loop-continuation condition or a loop-termination condition. The pseudocode statements
While there are more items on my shopping list Put next item in cart Cross it off my list
describe the repetitive actions on a shopping trip. The loop-continuation condition "there are more items on my shopping list" can be true or false. If it's true, the actions "Put next item in cart" and "Cross it off my list" are performed. These actions execute repeatedly while the condition remains true. The statement(s) contained in the While repetition statement constitute the body of the While. Eventually, the condition becomes false (when the last remaining item on the shopping list has been purchased and crossed off the list). Then, the repetition terminates, and the first statement after the repetition statement executes.
Performing a Calculation in a Do While...Loop Repetition Statement
Consider a program segment designed to find the first power of 3larger than 100. Suppose that the Integer variable product is initialized to 3. When the following Do While...Loop statement finishes executing, product contains the result:
When the Do While...Loop statement begins execution, product is 3. The body statement repeatedly multiplies product by 3, so it takes on the values 3, 9, 27, 81 and 243, successively. When product becomes 243, the condition product <= 100 becomes false. This terminates the repetition with 243 as product's final value. Then, execution continues with the next statement after the keyword Loop. If the condition in a Do While...Loop is initially false, the body statement(s) do not execute.
UML Activity Diagram for the Do While ... Loop Repetition Statement
The activity diagram of Fig. 4.7 illustrates the preceding Do While...Loop statement's flow of control. Once again, the symbols in the diagram (besides the initial state, transition arrows, a final state and three notes) represent an action state and a decision. This diagram also introduces the UML's merge symbol, which joins two activity flows into one. The UML represents both the merge symbol and the decision symbol as diamonds (this can be confusing). In this diagram, the merge symbol joins the transitions from the initial state and the action state, so they both flow into the decision that determines whether the loop should begin (or continue) executing. The decision and merge symbols can be distinguished by the number of "incoming" and "outgoing" transition arrows. A decision symbol has one transition arrow pointing to the diamond and two or more transition arrows pointing out from the diamond to indicate possible transitions from that point. In addition, each transition arrow pointing out of a decision symbol has a guard condition next to it (exactly one of which must be true when a decision is made). A merge symbol has two or more transition arrows pointing to the diamond and only one transition arrow pointing from the diamond, to indicate multiple activity flows merging to continue the activity. The transition arrows associated with a merge symbol do not have guard conditions.
Fig. 4.7 Do While ... Loop repetition statement activity diagram.
Figure 4.7 clearly shows the repetition of the preceding Do While...Loop statement. The transition arrow emerging from the action state connects back to the merge, which transitions back to the decision that's tested each time through the loop until the guard condition product>100 becomes true. Then the Do While...Loop statement exits (reaches its final state) and control passes to the next statement in sequence.
While...End While Repetition Statement
The While...End While repetition statement behaves identically to the Do While...Loop repetition statement, so their activity diagrams are also identical. For consistency, you should choose one or the other. In this book, we use the Do While...Loop statement. The following While...End While statement reimplements the preceding Do While...Loop:
Do Until...Loop Repetition Statement
Unlike the preceding repetition statements, the Do Until ... Loop repetition statement executes its body statement(s) repeatedly as long as the condition (known as the loop-termination condition) evaluates to false. You can write the preceding Do While...Loop statement as a Do Until... Loop statement as follows:
The activity diagram of Fig. 4.7 also illustrates the flow of control in the preceding statement; however, we focus on the leftmost guard condition—product >100—the loop-termination condition for our control statement. Because we're using a Do Until...Loop statement, the statement's action is performed when the statement's loop-termination condition is false (that is, product is less than or equal to 100). When the loop-termination condition (product >100, the leftmost guard condition) is true, the statement exits. If the condition in a Do Until...Loop is initially true, the body statement(s) do not execute. In general, you can use a Do While...Loop statement with the condition expressed differently (in this case product<=100), rather than using a Do Until...Loop.