Section 6.7. Do...Loop Until Repetition Statement


6.7. Do...Loop Until Repetition Statement

The Do...Loop Until repetition statement is similar to the Do Until...Loop statement, except that the loop-termination condition is tested after the loop body is performed; therefore, the loop body executes at least once. When a Do...Loop Until terminates, execution continues with the statement after the Loop Until clause. Figure 6.13 uses a Do...Loop Until statement to print the numbers from 1 to 5.

Figure 6.13. Do...Loop Until repetition statement.

  1  ' Fig. 6.13: DoLoopUntil.vb  2  ' Using Do...Loop Until repetition statement.  3  Module DoLoopUntil  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 Until counter >5            12 13        Console.WriteLine() 14     End Sub ' Main 15  End Module ' DoLoopWhile 

 1 2 3 4 5 



The Do...Loop Until statement activity diagram is the same as the one in Fig. 6.14. Note that the loop-termination condition (counter > 5) is not evaluated until after the body is executed at least once. If this condition is true, the statement exits. If the condition is false (i.e., the condition counter <= 5 is true), the loop continues executing.

Figure 6.14. Do...Loop While repetition statement activity diagram.


Error-Prevention Tip 6.5

In a counter-controlled loop, ensure that the control variable is incremented (or decremented) appropriately in the body of the loop to avoid an infinite 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