Using Loop statements

Source: Internet
Author: User
Tags exit
Loop | statement uses loops to execute code repeatedly
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.
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 False at 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 "Loop Repeats" & Counter & "times. "
End Sub

Sub Chklastwhile ()
Dim counter, MyNum
Counter = 0
MyNum = 9
Todo
MyNum = myNum-1
Counter = counter + 1
Loop while MyNum > 10
MsgBox "Loop Repeats" & Counter & "times. "
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 "Loop Repeats" & Counter & "times. "
End Sub

Sub Chklastuntil ()
Dim counter, MyNum
Counter = 0
MyNum = 1
Todo
MyNum = MyNum + 1
Counter = counter + 1
Loop Until MyNum = 10
MsgBox "Loop Repeats" & Counter & "times. "
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 < Then Exit do
Loop
MsgBox "Loop Repeats" & Counter & "times. "
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 ten step 2
Total = Total + j
Next
MsgBox "sum is & 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 = 2 Step-2
Total = Total + MyNum
Next
MsgBox "sum is & 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 the Dictionary object are used to place the text in multiple text boxes, respectively:

<HTML>
<HEAD><TITLE> Forms and elements

[1] [2] Next page



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.