Using Loop statements
Loops are used to repeatedly execute a set of statements. Loops can be grouped into three categories: a class that repeats a statement before the condition becomes False , a class that repeats the statement before the condition becomes True , and another class repeats the statement at a specified number of times.
The following looping statements can be used in VBScript:
- Do ... Loop: loops when (or until) the condition is True .
- While ... Wend: Loops When the condition is True .
- For ..... Next: Specify the number of loops and repeat the statement using the counter.
- For Each ... Next: Repeats a set of statements for each item in the collection or for each element in the array.
(The above statements are described in detail in the following sections)
Use Do Loop
You can use do ... The Loop statement runs a block of statements multiple times (indeterminate). Repeats the statement block when the condition is true or the condition becomes true .
Repeat the statement when the condition is True
while keyword is used to check do ... the condition in the Loop statement. There are two ways to check the condition: Check the condition before entering the loop, such as the following Chkfirstwhile example, or check the condition after the loop has run at least once (see chklastwhile example below). In the chkfirstwhile process, if the MyNum's initial value is set to 9 instead of 20, the statement in the loop body will never be executed. During the chklastwhile process, the statements in the loop body are executed only once, because the condition is Falseat the time of the check.
Sub ChkFirstWhile() Dim counter, myNum counter = 0 myNum = 20 Do While myNum > 10 myNum = myNum - 1 counter = counter + 1 Loop MsgBox "
The loop repeats
once. "
End Sub
Sub ChkLastWhile()
Dim counter, myNum
counter = 0
myNum = 9
Do
myNum = myNum - 1
counter = counter + 1
Loop While myNum > 10
MsgBox "
The loop repeats
once."
End Sub
Repeat the statement until the condition changes to True
The Until keyword is used to check the Do ... the condition in the Loop statement. There are two ways to check the condition: Check the condition before entering the loop, such as the following Chkfirstuntil example, or check the condition after the loop has run at least once (see chklastuntil example below). The loop occurs whenever the condition is False.
Sub ChkFirstUntil() Dim counter, myNum counter = 0 myNum = 20 Do Until myNum = 10 myNum = myNum - 1 counter = counter + 1 Loop MsgBox "
The loop repeats
once. "
End Sub
Sub ChkLastUntil()
Dim counter, myNum
counter = 0
myNum = 1
Do
myNum = myNum + 1
counter = counter + 1
Loop Until myNum = 10
MsgBox "
The loop repeats
once."
End Sub
Exit loop
The exit do statement is used to exit the Do ... Loop Loop. Because it is usually only in some special cases to exit the loop (for example, to avoid a dead loop), you can do so in the If ... Then ... the Exit do statement is used in the True statement block of the Else statement. If the condition is False, the loop will run as usual.
In the following example, the initial value of MyNum will cause a dead loop. If ... Then ... Else statement to check this condition to prevent the death loop from appearing.
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 "
The loop repeats
once."
End Sub
Use while ... Wend
While ... Wend statements are provided to users who are familiar with their usage. But because while ... Wend lacks flexibility, so it is advisable to use do ... The Loop statement.
Use for ... Next
For ..... The Next statement is used to run the statement block for a specified number of times. A counter variable is used in a loop, and the value of the variable increases or decreases with each loop.
For example, the following example repeats a procedure MyProc 50 times. The For statement specifies the counter variable x and its starting and ending values. The Next statement causes the counter variable to add 1 at a time.
Sub DoMyProc50Times() Dim x For x = 1 To 50 MyProc Next End Sub
The keyword step is used to specify a value for each increment or decrease in the counter variable. In the following example, the counter variable J increases by 2 each time. After the loop ends, the total value is the sum of 2, 4, 6, 8, and 10.
Sub TwosTotal() Dim j, total For j = 2 To 10 Step 2 total = total + j Next MsgBox "
Sum for " & total & "
."
End Sub
To decrement the counter variable, you can set the step to a negative value. The end value of the counter variable must be less than the starting value at this time. In the following example, the counter variable myNum is reduced by 2 each time. After the loop ends, the total value is the sum of 16, 14, 12, 10, 8, 6, 4, and 2.
Sub NewTotal() Dim myNum, total For myNum = 16 To 2 Step -2 total = total + myNum Next MsgBox "
Sum for " & total & "
."
End Sub
The exit for statement is used to exit for ... before the counter reaches its termination value. Next statement. Because it is usually only possible to exit the loop under certain special circumstances (for example, when an error occurs), you can do so in the If ... Then ... the Exit for statement is used in the True statement block of the Else statement. If the condition is False, the loop will run as usual.
Use for Each ... Next
For each ... Next loop with for ... Next loop is similar. For each ... Next is not to run the statement the specified number of times, but to repeat a set of statements for each element in the array or for each item in the collection of objects. This is useful when you do not know the number of elements in the collection.
In the following example, the contents of theDictionary object are used to place the text in multiple text boxes, respectively:
<HTML> <HEAD><TITLE>
Forms and elements</TITLE></HEAD>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub cmdChange_onClick
Dim d '
Create a variable Set d = CreateObject("Scripting.Dictionary")
d.Add "0", "Athens" '
Add Keys and Items d.Add "1", "Belgrade"
d.Add "2", "Cairo"
For Each I in d
Document.frmForm.Elements(I).Value = D.Item(I)
Next
End Sub
-->
</SCRIPT>
<BODY>
<CENTER>
<FORM NAME="frmForm"
<Input Type = "Text"><p>
<Input Type = "Text"><p>
<Input Type = "Text"><p>
<Input Type = "Text"><p>
<Input Type = "Button" NAME="cmdChange" VALUE="
Click here"><p>
</FORM>
</CENTER>
</BODY>
</HTML>
-->