Do...Loop

Top  Previous  Next

Do...Loop - Control Loops

Language: VBScript

Description
The Do...Loop function consists of several different loops types which are used when the number of iterations is unknown.

A Do...Loop allows you to continuously iterate through a loop until an exit condition is met. There are several ways do loops can be written, with similar results. See the following example syntaxes:

Do While Loop (Exit Condition at Top)

x = 1

Do While x <= 5

   SMEvent.Raise "Trace", x & " Loop"

   x = x + 1

Loop

Do While Loop (Exit Condition at End)

Do

   answer = MsgBox("Continue Looping?", vbYesNo)

Loop While answer = vbYes

Note: Do While loops can also be written as Do Until loops except that the condition and result would be the opposite.

Do Loop (Using Exit Do Statement)

Do

   answer = MsgBox("Continue Looping?", vbYesNo)

   If answer=vbNo Then Exit Do    

Loop

Note: Do...Loop's have no built-in exit trigger, therefore if written incorrectly they can cause infinite loops which will consume large amounts of system resources and usually hang the script. Carefully planning is always required to make sure that an exit condition will be reached at some point.