Appendix: VBScript 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 made " & counter & " repetitions." End Sub Repeating a Statement Until a Condition Becomes True There are two ways to use the Until keyword to check a condition in a Do…Loop statement. You can check the condition before you enter the loop (as shown in the following ChkFirstUntil example), or you can check it after the loop has run at least once (as shown in the ChkLastUntil example). As long as the condition is False , the looping occurs.
Sub ChkFirstUntil() Dim counter, myNum counter = 0 myNum = 20 Do Until myNum = 10 myNum = myNum - 1 counter = counter + 1 Loop MsgBox "The loop made " & counter & " repetitions." 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 made " & counter & " repetitions." End Sub Exiting a Do…Loop Statement from Inside the Loop You can exit a Do…Loop by using the Exit Do statement. Because you usually want to exit only in certain situations, such as to avoid an endless loop, you should use the Exit Do statement in the True statement block of an If…Then…Else statement.
If the condition is False , the loop runs as usual. In the following example, myNum is assigned a value that creates an endless loop. The If…Then…Else statement checks for this condition, preventing the endless repetition. Sub ExitExample() Dim counter, myNum counter = 0 myNum = 9 Do Until myNum = 10 myNum = myNum - 1 counter = counter + 1 If myNum Loop MsgBox "The loop made " & counter & " repetitions." End Sub Using While…Wend The While…Wend statement is provided in VBScript for those who are familiar with its usage. However, because of the lack of flexibility in While…Wend , it is recommended that you use Do…Loop instead.
Using For…Next You can use For…Next statements to run a block of statements a specific number of times. For loops, use a counter variable whose value increases or decreases with each repetition of the loop. Page 672 InduSoft Web Studio