VBScript loop
Repeat the code using a loop
Repeat the cycle for a set of statements. Cycle can be divided into three categories: one before the condition becomes False repeat the statement, the condition becomes True for a class before the repeat statement, and the other in accordance with the specified number of times repeat the statement.
VBScript can be used in the following loop:
Do … Loop: while (or until) when the loop condition is True.
While … Wend: When the condition is True when the loop.
For … Next: the specified number of cycles, use run counter to repeat statements.
For Each … Next: For each collection or array each element, repeat a statement.
Use the Do loop
You can use Do … Loop statement many times (frequency variable) to run block. When the condition becomes True True or conditions prior to the time, repeat the block.
When the condition is True when the repeat statement
While keyword to check the Do … Loop statement conditions. There are two ways to check the conditions: check before entering the loop conditions (such as the following ChkFirstWhile example); or run at least once in the loop after the inspection conditions (such as the following ChkLastWhile example). In ChkFirstWhile process, if myNum the initial value is set to 9 instead of 20, you will never be the implementation of the statements in the loop. In ChkLastWhile process, the statements in the loop will be executed once, because the conditions are already in the check False.
Sub ChkFirstWhile ()
Dim counter, myNum
counter = 0
myNum = 20
Do While myNum> 10
myNum = myNum - 1
counter = counter + 1
Loop
MsgBox “loop repeats” & counter & “times. “
End Sub Sub ChkLastWhile ()
Dim counter, myNum
counter = 0
myNum = 9
Do
myNum = myNum - 1
counter = counter + 1
Loop While myNum> 10
MsgBox “loop repeats” & counter & “times. “
End Sub
Repeat the statements until the condition becomes True
Until keyword to check the Do … Loop statement conditions. There are two ways to check the conditions: Check the conditions before entering the loop (such as the following ChkFirstUntil example); or run at least once in the loop, after checking the conditions (such as the following ChkLastUntil example). As long as conditions are False, it will loop.
Sub ChkFirstUntil ()
Dim counter, myNum
counter = 0
myNum = 20
Do Until myNum = 10
myNum = myNum - 1
counter = counter + 1
Loop
MsgBox “loop repeats” & counter & “times. “
End SubSub ChkLastUntil ()
Dim counter, myNum
counter = 0
myNum = 1
Do
myNum = myNum + 1
counter = counter + 1
Loop Until myNum = 10
MsgBox “loop repeats” & counter & “times. “
End Sub
Exit loop
Exit Do statement to exit the Do … Loop loop. Because usually only in certain special circumstances to withdraw from circulation (for example, to avoid endless loop), so in If … Then … Else statement block using True Exit Do statement. If the condition is False, loop will run as usual.
In the following example, myNum the initial value will cause an infinite loop. If … Then … Else statement checks for this condition to prevent infinite loop.
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
MsgBox “loop repeats” & counter & “times. “
End Sub
Using While … Wend
While … Wend statement is for those who are familiar with its use of user-supplied. However, due to the lack of flexibility in While … Wend, it would be best to use Do … Loop statement.
Use For … Next
For … Next statement is used to run the specified number of times a statement block. Used in the loop counter variable, the variable value with each cycle to increase or decrease.
For example, the following example will process MyProc Repeat 50 times. For statement specifies the counter variable x and its initial value and termination value. Next statement to add a counter variable each time.
Sub DoMyProc50Times ()
Dim x
For x = 1 To 50
MyProc
Next
End Sub
Step counter variable keyword used to specify the value of each increase or decrease. In the following example, each time the counter variable j + 2. After cycling, total value of 2,4,6,8, and 10 combined. Sub TwosTotal ()
Dim j, total
For j = 2 To 10 Step 2
total = total + j
Next
MsgBox “Total is” & total & “. “
End Sub
For counter variable decrease, negative values can be set to Step. At this point the counter variable value must be less than the initial value of termination. In the following example, the counter variable by 2 each time myNum. After cycling, total value of 16,14,12,10,8,6,4 and 2 combined.
Sub NewTotal ()
Dim myNum, total
For myNum = 16 To 2 Step -2
total = total + myNum
Next
MsgBox “Total is” & total & “. “
End Sub
Exit For statement is used in the counter value reaches its end before the exit For … Next statement. Because usually only in certain special circumstances (for example, when an error occurs) to exit the loop, so you can If … Then … Else statement block using True Exit For statement. If the condition is False, loop will run as usual.
Use For Each … Next
For Each … Next loop and the For … Next loop similar. For Each … Next statement to run than a specified number of times, but each element of the array or object in the collection of a repeat of each set of statements. This did not know the number of elements in the collection is very useful.
In the following example, Dictionary object's content for the text were placed in a number of text box:
