Do ... Loop


A Do...Loop structure is used when you want to repeat a set of statements until a condition is satisfied. If you want to repeat the statements a set number of times, the For...Next Statement is usually a better choice.

Do loops allow you to choose whether to end the loop when condition stops being True or when it first becomes True. You can also test the condition at either the beginning or end of the loop.

Do {While | Until} Condition

For Example:

x = 0
Do While x < 10
x = x + 1
Print x
Loop


x = 0
Do Until x > 10
x = x + 1
Print x
Loop

Loop {While | Until} Condition

For Example:

x = 0
Do
x = x + 1
Print x
Loop While x < 10


x = 0
Do
x = x + 1
Print x
Loop Until x > 10

Exit Do

Do loops should never be exited with a Goto statement. If you must exit a Do loop use the Exit Do command.

For Example:

x = 0
Do
If x = 5 Then Exit Do
x = x + 1
Print x
Loop While x < 10

You may execute a Return from within a Do Loop if you are in a subroutine

For Example:

Sub Test()

x = 0
Do
If x = 5 Then Return
x = x + 1
Print x
Loop While x < 10

End Sub






Become a Patron