Section 5.5. While Repetition Statement


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.

  1  ' Fig. 5.4: PowersOfThree.vb  2  ' Demonstration of While statement.  3  Module PowersOfThree  4     Sub Main()  5        Dim product As Integer = 3  6  7        ' statement multiplies and displays product  8        ' while product is less than or equal to 100  9        While product <= 100                                10           Console.Write(product & " " )                    11           product = product * 3 ' compute next power of 3  12        End While                                           13 14        Console.WriteLine() ' write blank line 15 16        ' print result 17        Console.WriteLine("First power of 3 " & _ 18            "larger than 100 is " & product) 19     End Sub 'Main 20  End Module 'PowersOfThree 

3  9  27  81 First power of 3 larger than 100 is 243 



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

Failure to provide the body of a While statement with an action that eventually causes the loop-continuation condition to become false is a logic error. Normally, such a repetition statement never terminates, resulting in an linfinite loop.





Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net