Loops are used to repeat an action. There are various variations of
the Do loops.
The formula of the Do While loop is:
Do While Condition
Statement(s)
Loop
This expression will execute the Statement or
statements AS LONG AS the Condition is true, as many times as the
Condition will be visited and found true. The program will first
test the Condition. If the Condition is true, the program will
execute the Statement or Statements and go back to the
Do While
statement and test the condition again. If the Condition is false,
the program will skip the Do While statement and not execute any.
Since the Do While statement tests the Condition first before
executing the Statement, sometimes you will want the program to
execute the Statement first, then go back and test the Condition.
VBScript offers a reverse to the formula, which is:
Do
Statement(s)
Loop While Condition
In this case, VBScript will execute the Statement or Statements first, then it will test the
Condition. If the
Condition is true, the program will execute the Statement again.
The program will continue this examination-execution as long as
the Condition is true. The big difference here is that even if the
Condition is false, the program will have executed the Condition
at least once.
An alternative to the Do While loop is the
Do
Until loop. Its formula is:
Do Until Condition
Statement(s)
Loop
This loop will first examine the Condition,
instead of examining whether the Condition is true, it will test
whether the Condition is false.
The other side of the Do Until loop will execute
the Statement first, then it would examine the Condition. The
formula is:
Do
Statement(s)
Loop Until Condition
|