Section 6.6. Do...Loop While Repetition Statement


6.6. Do...Loop While Repetition Statement

The Do...Loop While repetition statement is similar to the While statement and Do While...Loop statement. In the While and Do While...Loop statements, the loop-continuation condition is tested at the beginning of the loop, before the body of the loop is performed. The Do...Loop While statement tests the loop-continuation condition after the loop body is performed; therefore, in a Do...Loop While statement, the loop body is always executed at least once. When a Do...Loop While statement terminates, execution continues with the statement after the Loop While clause. The program in Fig. 6.12 uses a Do...Loop While statement to output the values 15.

Figure 6.12. Do...Loop While repetition statement.

  1  ' Fig. 6.12: DoLoopWhile.vb  2  ' Demonstrating the Do...Loop While repetition statement.  3  Module DoLoopWhile  4     Sub Main()  5        Dim counter As Integer = 1  6  7        ' print values 1 to 5             8        Do                                9           Console.Write(counter & " " ) 10           counter += 1                  11        Loop While counter <= 5          12 13        Console.WriteLine() 14     End Sub ' Main 15  End Module ' DoLoopWhile 

 1 2 3 4 5 



Lines 811 demonstrate the Do...Loop While statement. The first time that the statement is encountered, lines 910 are executed, displaying the value of counter (at this point, 1) then incrementing counter by 1. Then the condition in line 11 is evaluated. Variable counter is 2, which is less than or equal to 5; because the loop-continuation condition is met, the Do...Loop While statement executes again. In the fifth iteration of the statement, line 9 outputs the value 5, and line 10 increments counter to 6. At this point, the loop-continuation condition in line 11 evaluates to false, and the program exits the Do...Loop While statement.

Do...Loop While Statement UML Activity Diagram

The Do...Loop While UML activity diagram (Fig. 6.14) illustrates the fact that the loop-continuation condition is not evaluated until after the statement body is executed at least once. Compare this activity diagram with that of the While statement (Fig. 5.5). Again, note that (besides an initial state, transition arrows, a merge, a final state and several notes) the diagram contains only action states and a decision.



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