In a VB.NET method, you will find many situations in which you want to do the same thing again and again, perhaps slightly changing a value each time you repeat the action. This is called iteration or looping. Typically, you'll iterate (or loop) over a set of items, taking the same action on each. This is the programming equivalent to an assembly line: take a hundred car bodies and put a windshield on each one as it comes by.
VB.NET provides an extensive suite of iteration statements
The semantics of a
Do loop are "Do this work while a condition is true" or "Do this work until a condition becomes true." You can test the condition either at the top or at the bottom of the loop. If you test at the bottom of the loop, the loop will execute at least once.
Do loop can even be written with no conditions; in which case, it will execute indefinitely, until it encounters an
Exit Do statement.
Do loops come in the following flavors:
Do While BooleanExpression statements Loop Do Until BooleanExpression statements Loop Do statements Loop while BooleanExpression Do statements Loop until BooleanExpression Do statements Loop
In each case, the
BooleanExpression can be any expression that evaluates to a
Boolean value of
The first kind of
Do While, executes only while the
True, as shown in Example 16-9.
Example 16-9. Using Do While
Option Strict On Module Module1 Sub Main() Dim counterVariable ...