Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of Loop statement in VBScript

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the usage of cyclic sentences in VBScript". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the usage of cyclic sentences in VBScript.

A loop is used to repeat a set of statements. Loops can be divided into three categories: those that repeat statements before the condition becomes False, those that repeat statements before the condition becomes True, and those that repeat statements a specified number of times.

The following loop statements can be used in VBScript:

Do...Loop: loop when (or until) the condition is True.

While...Wend: loop when the condition is True.

For...Next: specify the number of loops and run the statement repeatedly using a counter.

For Each...Next: repeat a set of statements for each item in the collection or each element in the array.

(the above statement will be described in detail in the following)

Use Do Loop

You can use Do...Loop statements to run statement blocks multiple times (indefinitely). Repeat the statement block when the condition is True or before the condition becomes True.

Repeat the statement 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 condition: check the condition before entering the loop (such as the ChkFirstWhile example below), or check the condition after the loop has been run at least once (such as the ChkLastWhile example below). During ChkFirstWhile, if the initial value of myNum is set to 9 instead of 20, the statement in the body of the loop will never be executed. During the ChkLastWhile procedure, the statement in the body of the loop is executed only once, because the condition is already 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 repeated" & 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 repeated" & counter & "times." End Sub repeats the statement until the condition becomes 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 (such as the ChkFirstUntil example below), or check the condition after the loop has been run at least once (such as the ChkLastUntil example below). As long as the condition is False, the loop occurs.

Sub ChkFirstUntil () Dim counter, myNum counter = 0 myNum = 20 Do Until myNum = 10 myNum = myNum-1 counter = counter + 1 Loop MsgBox "loop repeated" & 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 "loop repeated" & counter & "times." End Sub exits the loop

The Exit Do statement is used to exit the Do...Loop loop. Because you usually exit the loop only in some special cases (such as avoiding an endless loop), you can use the Exit Do statement in the True statement block of the If...Then...Else statement. If the condition is False, the loop will run as usual.

In the following example, the initial value of myNum will result in an endless loop. The If...Then...Else statement checks this condition to prevent an endless 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 repeated" & counter & "times." End Sub uses While...Wend

The While...Wend statement is for users who are familiar with its usage. However, due to the lack of flexibility in While...Wend, it is recommended that you use Do...Loop statements.

Use For...Next

The For...Next statement is used to run the statement block a specified number of times. Use a counter variable in a loop, whose value increases or decreases with each loop.

For example, the following example repeats the procedure MyProc 50 times. The For statement specifies the counter variable x and its starting and ending values. The Next statement increments the counter variable by 1 at a time.

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

The keyword Step is used to specify the value by which the counter variable increases or decreases each time. In the following example, the counter variable j adds 2 each time. At the end of the loop, the value of total 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 is & total &". End Sub

To decrement the counter variable, set Step to a negative value. At this point, the ending value of the counter variable must be less than the starting value. In the following example, the counter variable myNum is minus 2 each time. At the end of the loop, the value of total 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 "the sum is" & total & ". End Sub

The Exit For statement is used to exit the For...Next statement before the counter reaches its termination value. Because you usually exit the loop only in some special cases, such as when an error occurs, you can use the Exit For statement in the True statement block of the If...Then...Else statement. If the condition is False, the loop will run as usual.

Use For Each...Next

For Each...Next loops are similar to For...Next loops. Instead of running the statement a specified number of times, For Each...Next repeats a set of statements for each element in the array or each item in the collection of objects. This is useful when you don't know the number of elements in the collection.

In the following example, the contents of the Dictionary object are used to place text in multiple text boxes:

Forms and elements

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report