VBScript Tutorial six-looping statements

Source: Internet
Author: User
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.
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.

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.