Here??™s the basic Do syntax:
Do While | Until condition
statements
Exit Do
Loop
The Do While executes the statements while the defined condition is True. On the other
hand, Do Until executes the statements until the defined condition is True. Do loops are useful
when you don??™t know the number of times you??™ll need to execute a statement. In this example,
the code loops through the array of integers while the value of the array element is less than or
equal to 5:
Dim MyArray() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim rnd As Random = New Random
Dim i As Integer = rnd.Next(0, 9)
Do While MyArray(i) <= 9
Console.WriteLine("{0}", MyArray(i).ToString)
i += 1
Loop
CHAPTER 2 n VB 2008 SYNTAX 32
The result will be the numbers between the random number and 10 appearing on the console.
If you want to accomplish the same thing using the Do Until construct, it would perform
the statements until the evaluated expression was true, and it would look like this:
Do Until MyArray(i) > 9
Console.WriteLine("{0}", MyArray(i).ToString)
i += 1
Loop
Continue
The Continue statement allows you to skip to the next iteration of a loop without processing
the rest of the loop body. For example, in this code, if Counter is 6 or 7, execution goes to the
Next line.
Pages:
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86