Using Do…Loops Statement
Description Description
Repeatedly executes a block of code while or until a condition becomes
Repeatedly executes a block of code while or until a condition becomes TrueTrue..
Syntax Syntax
Do
Do [{[{ While While || UntilUntil}} conditioncondition]] [
[statementsstatements]] [
[ExitExit DoDo]] [
[statementsstatements]] Loop
Loop Do Do
[
[statementsstatements]] [
[ExitExit DoDo]] [
[statementsstatements]] Loop
Loop [{[{ While While || UntilUntil}} conditioncondition]]
Arguments Arguments
Argument Description Argument Description condition
condition Numeric or string expression that isNumeric or string expression that is TrueTrue oror FalseFalse. If condition is. If condition is NullNull,, condition is treated as
condition is treated as FalseFalse..
statements
statements One or more statements that are repeated while or until condition isOne or more statements that are repeated while or until condition is True
True..
Notes Notes
On its own,
On its own, Do...LoopDo...Loop repeatedly executes the code that is containedrepeatedly executes the code that is contained within its boundaries indefinitely. You
within its boundaries indefinitely. You therefore need to specify under whattherefore need to specify under what conditions the loop is to
conditions the loop is to stop repeating. Sometimes, this requires modifyingstop repeating. Sometimes, this requires modifying the variable that controls loop execution within the loop. For example:
the variable that controls loop execution within the loop. For example:
Do Do
nCtr = nCtr +
nCtr = nCtr + 11 ' Modify loop control variable' Modify loop control variable MsgBox
MsgBox ""IterationIteration " & nCtr & "" & nCtr & " of the Do loop...of the Do loop..." &" & vbCrLf vbCrLf ' Compare to upper limit
' Compare to upper limit If
If nCtr =nCtr = 1010 ThenThen ExitExit DoDo Loop
Loop
Failure to do this results in the creation of an endless loop.
Failure to do this results in the creation of an endless loop.
Adding the
Adding the UntilUntil keyword afterkeyword after DoDo instructs your program toinstructs your program to DoDo somethingsomething Until
Until the condition isthe condition is TrueTrue. Its syntax is:. Its syntax is:
Do
Do UntilUntil conditioncondition code to execute code to execute Loop
Loop
If condition is
If condition is TrueTrue before your code gets to thebefore your code gets to the DoDo statement, the codestatement, the code within the
within the Do...LoopDo...Loop is ignored.is ignored.
Adding the
Adding the WhileWhile keyword afterkeyword after DoDo repeats the code while a particularrepeats the code while a particular condition is
condition is TrueTrue. When the condition . When the condition becomesbecomes FalseFalse, the loop is, the loop is automatically exited. The syntax of the
automatically exited. The syntax of the Do WhileDo While statement is:statement is:
Do While
Do While conditioncondition code to execute code to execute Loop
Loop
Again, the code within the
Again, the code within the Do...LoopDo...Loop construct is ignored if condition isconstruct is ignored if condition is False
False when the program arrives at the loop.when the program arrives at the loop.
In some cases, you may need to execute the loop at least once. You might, In some cases, you may need to execute the loop at least once. You might, for example, evaluate the values held within an array and terminate the for example, evaluate the values held within an array and terminate the loop if a particular value is found. In that case, you'd need to execute the loop if a particular value is found. In that case, you'd need to execute the loop at least once. To do this, place the
loop at least once. To do this, place the UntilUntil oror WhileWhile keyword along withkeyword along with the condition after the
the condition after the LoopLoop statement.statement. Do...Loop UntilDo...Loop Until always executesalways executes the code in the loop at least once and continues to loop until the condition is the code in the loop at least once and continues to loop until the condition is True
True. Likewise,. Likewise, Do...LoopDo...Loop WhileWhile always executes the code at least once,always executes the code at least once, and continues to loop while the condition is
and continues to loop while the condition is TrueTrue. The syntax of these two. The syntax of these two statements is as follows:
statements is as follows:
Do Do
code to execute code to execute Loop
Loop UntilUntil conditioncondition
Do Do
code to execute code to execute Loop
Loop While While conditioncondition
A
A NullNull condition is treated ascondition is treated as FalseFalse..
Your code can exit the loop at any point by executing the
Your code can exit the loop at any point by executing the Exit DoExit Do statement.
statement.
Using While. . .Wend Statement Using While. . .Wend Statement
Description Description
The
The While…WendWhile…Wend statement executes a series of statements as long as astatement executes a series of statements as long as a given condition is
given condition is TrueTrue..
Syntax Syntax
While
While conditioncondition
Version [statements]
Version [statements]
Wend Wend
Arguments Arguments
Argument Description Argument Description condition
condition Numeric variable used as a loop coNumeric variable used as a loop co unter. The variable can't be an arrayunter. The variable can't be an array element or an element of a user-defined type.
element or an element of a user-defined type.
statements
statements One or more statements between For and Next tOne or more statements between For and Next t hat are executed thehat are executed the specified number of times.
specified number of times.
Notes Notes
A
A NullNull condition evaluated ascondition evaluated as FalseFalse..
If
If conditioncondition evaluates toevaluates to TrueTrue, the program code between the, the program code between the WhileWhile andand Wend
Wend statements executed. After thestatements executed. After the WendWend statement is executed, controlstatement is executed, control is passed back up to the
is passed back up to the WhileWhile statement, where condition is evaluatedstatement, where condition is evaluated again.
again. WhenWhen conditioncondition evaluates toevaluates to FalseFalse, program execution skips to the, program execution skips to the
first statement following the
first statement following the WendWend statement.statement.
You can nest
You can nest While...WendWhile...Wend loops within each other.loops within each other.
Tips Tips
The
The While...WendWhile...Wend statement remains instatement remains in VBScriptVBScript for backwardfor backward compatibility only. It has been superseded by the much more flexible compatibility only. It has been superseded by the much more flexible Do...Loop
Do...Loop statement.statement.