VBScript tutorial Lesson 8 use circular statements

Source: Internet
Author: User

Repeatedly execute code using Loops
Loop is used to repeatedly execute a set of statements. Loops can be divided into three types: one class executes statements repeatedly before the condition changes to False, the other class executes statements repeatedly before the condition changes to True, and the other class executes statements repeatedly according to the specified number of times.
The following statements can be used in VBScript:

Do... Loop: Loop when the (or until) condition is True.
While... Wend: loop when the condition is True.
For... Next: specify the number of cycles and use the counter to run the statement repeatedly.
For Each... Next: executes a set of statements repeatedly For Each element in the set or array.
Use Do loop
You can use the Do... Loop statement to run the statement block multiple times. When the condition is True or before the condition changes to True, the statement block is repeatedly executed.
Execute the statement repeatedly when the condition is True.
The While keyword is used to check the conditions in the Do... Loop statement. There are two ways to check the conditions: Check the conditions before entering the loop (the following ChkFirstWhile example); or check the conditions after the loop is run at least once (the following ChkLastWhile example ). During ChkFirstWhile, if the initial value of myNum is set to 9 rather than 20, the statement in the loop body will never be executed. During ChkLastWhile, the statement in the loop body is executed only once, because the condition is already False during the check.
Sub ChkFirstWhile ()
Dim counter, myNum
Counter = 0
MyNum = 20
Do While myNum> 10
MyNum = myNum-1
Counter = counter + 1
Loop
MsgBox "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 "repeats" & counter & "times. "
End Sub

Execute the statement repeatedly until the condition changes to True.
The Until keyword is used to check the conditions in the Do... Loop statement. There are two ways to check the condition: Check the condition before entering the loop (the following ChkFirstUntil example); or check the condition after the loop is run at least once (the following ChkLastUntil example ). As long as the condition is False, a loop is executed.
Sub ChkFirstUntil ()
Dim counter, myNum
Counter = 0
MyNum = 20
Do Until myNum = 10
MyNum = myNum-1
Counter = counter + 1
Loop
MsgBox "repeats" & counter & "times. "
End Sub

Sub ChkLastUntil ()
Dim counter, myNum
Counter = 0
MyNum = 1
Do
MyNum = myNum + 1
Counter = counter + 1
Loop Until myNum = 10
MsgBox "repeats" & counter & "times. "
End Sub

Exit Loop
The Exit Do statement is used to Exit the Do... Loop. Because it is usually only necessary to exit the loop in some special circumstances (for example, to avoid endless loops), you can... then... use the Exit Do statement 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 lead to an endless loop. If... Then... Else statement checks this condition to prevent endless loops.

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 "repeats" & counter & "times. "
End Sub

Use While... Wend
The While... Wend statement is provided for users who are familiar with its usage. However, because While... Wend lacks flexibility, we recommend that you use the Do... Loop statement.
Use For... Next
The For... Next statement is used to run the statement block For a specified number of times. Use counter variable in a loop. The value of this variable increases or decreases with each loop.
For example, the following example repeats MyProc 50 times. The For statement specifies the counter variable x, its start value, and end value. The Next statement adds 1 to the counter variable each time.

Sub DoMyProc50Times ()
Dim x
For x = 1 To 50
MyProc
Next
End Sub

The keyword Step is used to specify the value of each increase or decrease of the counter variable. In the following example, the counter variable j is added with 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
The sum of MsgBox "is" & total &". "
End Sub

To reduce the number of counter variables, set Step to a negative value. The end value of the counter variable must be smaller than the start value. 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
The sum of MsgBox "is" & total &". "
End Sub

The Exit For statement is used to Exit the For... Next statement before the counter reaches its termination value. Because in some special circumstances (for example, when an error occurs), you can exit the loop... then... use the Exit For statement 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... the Next loop is similar to the For... Next loop. For Each... Next does not mean to run the specified number of times, but repeats a group of statements For Each element or object set in the array. This is useful when you do not know the number of elements in the collection.
In the following example, the content of a Dictionary object is used to place the text in multiple text boxes:

<HTML>
<HEAD> <TITLE> forms and elements </TITLE> </HEAD>
<Script language = "VBScript">
<! --
Sub transaction change_nclick
Dim d' create a variable
Set d = createObject ("Scripting. Dictionary ")
D. Add "0", "Athens" 'Add key and Project
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 = "Modify change" VALUE = "Click here"> <p>
</FORM>
</CENTER>
</BODY>
</HTML>

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.