Using Do Loops
You can use Do...Loop statements to run a block of statements an indefinite number of times. The statements are repeated either while a condition is True or until a condition becomes True.
Repeating Statements While a Condition is True
Use the While keyword to check a condition in a Do...Loop statement. You can check
the condition before you enter the loop (as shown in the following
ChkFirstWhile
example), or you can check it after the loop has
run at least once (as shown in the ChkLastWhile
example). In the
ChkFirstWhile
procedure, if myNum is set to 9 instead of 20,
the statements inside the loop will never run. In the ChkLastWhile
procedure, the statements inside the loop run only once because the condition is
already False.
The following examples illustrates the use of the while
:
Example 1:
Sub ChkFirstWhile()
Dim counter, myNum
counter = 0
myNum = 20
Do While myNum > 10
myNum = myNum - 1
counter = counter + 1
Loop
'This loop made 10 repetitions."
End Sub
Example 2:
Sub ChkFirstWhile()
Dim counter, myNum
counter = 0
myNum = 9
Do While myNum > 10
myNum = myNum - 1
counter = counter + 1
Loop
'This loop made 0 repetitions.
End Sub
Example 3:
Sub ChkLastWhile()
Dim counter, myNum
counter = 0
myNum = 9
Do
myNum = myNum - 1
counter = counter + 1
Loop While myNum > 10
'This loop made 1 repetition.
End Sub
Example 4:
Sub ExampleWithDoWhile()
Dim counter, myNum
counter = 0
myNum = 15
Do While myNum > 10
myNum = myNum - 2
counter = counter + 1
Loop
' This loop reduced myNum to 9 and made 3 repetitions
End Sub
Example 5:
Sub ExampleWithDoLoopWhile()
Dim counter, myNum
counter = 0
myNum = 5
Do
myNum = myNum + 1
counter = counter + 1
Loop While myNum < 10
' This loop increased myNum to 10 and made 5 repetitions.
End Sub
Example 6:
Sub ChkLastWhile()
Dim counter, myNum
counter = 0
myNum = 9
Do
myNum = myNum - 1
counter = counter + 1
Loop While myNum > 10
'This loop made 1 repetition.
End Sub
Repeating a Statement Until a Condition Becomes True
There are two ways to use the Until
keyword to check a condition in
a Do...Loop statement. You can check the condition before you enter the loop (as
shown in the following ChkFirstUntil
example), or you can check it
after the loop has run at least once (as shown in the ChkLastUntil
example). As long as the condition is False, the looping occurs.
The following examples illustrates the use of the until
:
Example 1:
Sub ChkFirstUntil()
Dim counter, myNum
counter = 0
myNum = 20
Do Until myNum = 10
myNum = myNum - 1
counter = counter + 1
Loop
'This loop made 10 repetitions.
End Sub
Example 2:
Sub ChkLastUntil()
Dim counter, myNum
counter = 0
myNum = 1
Do
myNum = myNum + 1
counter = counter + 1
Loop Until myNum = 10
'This loop made 9 repetitions.
End Sub
Example 3:
Sub ExampleWithDoUntil()
Dim counter, myNum
counter = 0
myNum = 20
Do Until myNum < 15
myNum = myNum - 2
counter = counter + 1
Loop
'This loop reduced myNum to 14 and made 3 repetitions.
End Sub
Example 4:
Sub ExampleWithDoLoopUntil()
Dim counter, myNum
counter = 0
myNum = 5
Do
myNum = myNum + 2
counter = counter + 1
Loop Until myNum >= 15
'This loop increased myNum to 15 and made 5 repetitions.
End Sub
Exiting a Do...Loop Statement from Inside the Loop
You can exit a Do...Loop by using the Exit Do statement. Because you usually want to exit only in certain situations, such as to avoid an endless loop, you should use the Exit Do statement in the True statement block of an If...Then...Else statement. If the condition is False, the loop runs as usual.
In the following example, myNum
is assigned a value that creates an
endless loop. The If...Then...Else statement checks for this condition, preventing
the endless repetition.
The following examples illustrates the use of the do loop statement:
Example 1:
Sub ExitExample()
Dim counter, myNum
counter = 0
myNum = 9
Do Until myNum = 10
myNum = myNum - 1
counter = counter + 1
If myNum < 10 Then Exit Do
Loop
'This loop made 1 repetition.
End Sub
Example 2:
Sub PreventEndlessLoop()
Dim counter, myNum
counter = 1
myNum = 0
Do
myNum = myNum + 1
counter = counter + 1
If counter > 10 Then
Exit Do ' Avoids endless loop by exiting after 10 iterations.
End If
Loop
'This loop made 11 repetitions.
End Sub
Example 3:
Sub ExampleWithDoWhile()
Dim counter, myNum
counter = 0
myNum = 20
Do While myNum > 10
myNum = myNum - 2
counter = counter + 1
If myNum < 15 Then Exit Do ' Exit the loop early if myNum is less than 15.
Loop
'This loop reduced myNum to 14 and made 3 repetitions.
End Sub
Example 4:
Sub NestedLoopsExample()
Dim i, j
For i = 1 To 3
j = 0
Do
j = j + 1
If j > 2 Then Exit Do ' Exit inner loop after 2 iterations.
Loop
Next
End Sub