Visual Basic Do ... Loops

Revision as of 19:35, 13 August 2007 by Neil (Talk | contribs)

Revision as of 19:35, 13 August 2007 by Neil (Talk | contribs)

PreviousTable of ContentsNext
Visual Basic For LoopsVisual Basic Arrays


Often when looping it is not possible to know in advance how many times the loop needs to be performed. For example, a loop may need to continue until some specific criteria are met. Under such circumstances the Visual Basic Do ... Loop structure is used.

Creating a Visual Basic Do ... Loop

The Visual Basic Do ... Loop structure can be used in a variety of different ways. The first, basic method involves a simple Do loop with an Exit statement called based on a If statement. The syntax for this type of Do Loop is as follows:

Do
[VB Statements]
If expression Then Exit Do
Loop

The following code excerpt causes the Do ... Loop to exit when the intTotal variable reaches 10:

        Dim intTotal As Integer

        Do
            intTotal = intTotal + 1
            If intTotal = 10 Then Exit Do

            MessageBox.Show("intTotal = " + CStr(intTotal))
        Loop

Visual Basic Do While Loops

Another way to use a Do Loop is to make use of the While keyword. A Do ... Loop which uses the While keyword will loop as long as the While expression evaluates to True.

It is important to understand that the While keyword can be used either at the start or end of the loop. When it is possible that the loop will not have to executed at all (i.e the criteria may already be met before the loop executes) then the While should be placed at the start of the loop. Alternatively, when the loop will need to be executed at least once before the expression will evaluate to True then the While should be placed at the end of the loop. The syntax for these two uses are as follows:

Do While expression
[VB Statements]
Loop

Do
[VB Statements]
Loop While expression

The following code samples demonstrate both uses of the While keyword. Note that the second loop will never execute because the first loop has already increased the value of intTotal to 10.

Dim intTotal As Integer

        Do
            intTotal = intTotal + 1

            MessageBox.Show("intTotal = " + CStr(intTotal))
        Loop While intTotal < 10

        Do While intTotal < 10
            intTotal = intTotal + 1

            MessageBox.Show("intTotal = " + CStr(intTotal))
        Loop

Visual Basic Do Until Loops

The While loop causes the loop to continue until an expression ceases to evaluate to True. In other words, as soon as the expression returns False the loop exists. The Until keyword does the reverse, in other words the loop continues until an expression evaluates to true. As long as the expression returns false, the loop continues.

As with the While loop the Until expression may be placed at the start or end of the loop:

Do Until expression
[VB Statement]
Loop

Do
[VB Statements]
Loop Until expression

The following code example shows the Do ... Until ... Loop in action:

Dim intTotal As Integer

        Do
            intTotal = intTotal + 1

            MessageBox.Show("intTotal = " + CStr(intTotal))
        Loop Until intTotal > 10

        Do Until intTotal > 10
            intTotal = intTotal + 1

            MessageBox.Show("intTotal = " + CStr(intTotal))
        Loop

As with the previous example, the second loop will not execute because the the intTotal variable already exceeds 10 as a result of the previous loop.

Summary

In summary, the Visual Basic Do ... Loop structure provides a way to loop a number of time when it is not possible to know in advance how many times the loop may need to iterate. When a loop is required to be performed a pre-determined number of times, then a For ... Loop may make more sense. For information on Visual Basic For loops see Visual Basic For Loops.