Visual Basic 2005 for Programmers (2nd Edition)
5.5. While Repetition Statement
A repetition statement (also called a looping statement or a loop) allows you to specify that an action should be repeated, depending on the value of a condition (called the loop-continuation condition). As an example of a While statement (sometimes more formally referred to as the While...End While statement), consider a program designed to find the first power of 3 larger than 100 (Fig. 5.4). In line 5, we take advantage of a Visual Basic feature that allows variable initialization to be incorporated into a declaration. When the While statement is entered (line 9), product is 3. Variable product is repeatedly multiplied by 3 (line 11), taking on the values 3, 9, 27, 81 and 243, successively. When product becomes 243, the condition product <= 100 in the While statement becomes false. This terminates the repetition with 243 as product's final value. Execution continues with the next statement after the keywords End While in line 12. [Note: If a While statement's condition is initially false, the body statement(s) are not performed.] Figure 5.4. While repetition statement used to print powers of 3.
The UML activity diagram of Fig. 5.5 illustrates the flow of control that corresponds to the While statement shown in Fig. 5.4 (we have omitted the display of product each time through the loop at line 10). 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 flows of activity into one flow of activity. 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. Note that, unlike the decision symbol, the merge symbol does not have a counterpart in Visual Basic code. None of the transition arrows associated with a merge symbol have guard conditions. Figure 5.5. While repetition statement activity diagram.
The activity diagram of Fig. 5.5 clearly shows the repetition of the While statement discussed earlier in this section. The transition arrow emerging from the action state connects back to the merge, which transitions back to the decision that is tested each time through the loop until the guard condition product > 100 becomes true. Then the While statement exits (reaches its final state) and control passes to the next statement in sequence in the program. Common Programming Error 5.1
|
Категории