Difference between revisions of "VBScript loop block"
m (→Notes) |
m (1 revision imported) |
(No difference)
|
Latest revision as of 08:20, 1 December 2017
VBScript loop blocks execute groups of code repeatedly based on a "condition" being true or a given counter. The loop statements are explained here.
Contents
For loop
The For
loop repeats a block of code a number of times. The number of times is determined by the start of the for loop.
Counting: For loop
In the standard For
loop, we assign a "counter" variable to first number we would like to start at and say we want it to go To
an ending number.
For i = 1 To 10 AddChat vbCyan, "Hi." Next
Here we will loop ten times, printing "Hi." each time.
The counter variable is often called i
, but you can call it whatever you like. Inside the loop we can retrieve the value of i
and use it. Each time, the value will be the "count" of the iteration.
For i = 1 To 10 AddChat vbCyan, "This is iteration #" & i & "." Next
This example will print the numbers 1 through 10 in place of the i
.
We can also use For
loops with variables or functions in the start line, and it doesn't have to start at 1.
Here we will loop through from index zero (since VBScript arrays start at zero), to the upper bound index of Arr
, some variable containing an array, and print its contents.
For i = 0 To UBound(Arr) AddChat vbCyan, "Element #" & i & " contains " & Arr(i) Next
Step statement
In all of the previous examples, when execution reaches the Next
line, it increases the counter by 1 and goes back to the beginning.
We can change this behavior by using the Step
keyword.
For i = 0 To 10 Step 2 AddChat vbCyan, "This is iteration #" & i & "." Next
This time, the counter will go: 0, 2, 4, 6, 8, 10. When execution reaches Next
it will increase by the value provided by Step
before going back to the beginning (it defaults to Step 1
).
We can go in reverse, to loop through an array backwards, for example, by having a negative step.
For i = UBound(Arr) To 0 Step -1 AddChat vbCyan, "Element #" & i & " contains " & Arr(i) Next
Iterating: For Each
A special version of the For
loop allows you to loop through an array.
For Each Element In Arr AddChat vbCyan, Element Next
The counter is now hidden from use, but sometimes this type of loop is easier, if you don't care about using the counter inside the block when looping through an array.
The above is the same as doing this:
For i = 0 To UBound(Arr) Element = Arr(i) AddChat vbCyan, Element Next
Notes
- You can use
Exit For
to jump out of a loop to the point after theNext
statement. - A step of 0 can cause an infinite loop.
- If you do
For i = 1 To 10 Step 2
, an infinite loop will not occur because at theNext
statement. It checks if the counter is greater than the end and stops, rather than check if it is equal to the end. - After a For...To[...Step]...Next loop, the counter will be equal to the next value after end value following the rules of Step. For example, in the previous bullet,
i
would equal 11. - You can embed
For
loops inside other loops or any other blocks. - You should indent your code for readability based on the starting and ending of the loop.
Do loop
A Do
loop is the common "conditional" loop. It will loop while a condition is True or False.
As simple as it can get, the loop looks like:
Do AddChat vbRed, "This is a loop." Loop
Warning: The above code will cause an infinite loop. It will continue to execute over and over until execution is stopped by the dialog box or timeout.
We can stop it from being infinite by introducing a condition after the Do
or the Loop
keyword (but not both).
Do While
A While
statement will check whether a statement is True. If it is, then the loop will start or continue.
Var1 = 3 Do While Var1 < 3 AddChat vbMagenta, "This loop executed." Var1 = 0 Loop
The above loop will execute zero times because the While
statement will check the condition before executing.
Do Until
An Until
statement will check whether a statement is False. If it is, then the loop will start or continue.
Var1 = 3 Do Until Var1 < 3 AddChat vbMagenta, "This loop executed." Var1 = 0 Loop
The above loop will execute one time because the Until
statement will be positive the first time, but after 0 is assigned it will be negative.
Loop While
The condition can be put at the end of the loop, so that the loop will always execute at least once. A While
statement will still check if the statement is True.
Var1 = 0 Do AddChat vbMagenta, "This loop executed." Loop While Var1 > 0
This loop will execute once even though Var1 = 0
. After the first execution it will check and stop executing.
Loop Until
An Until
statement can be put at the end as well.
Var1 = 0 Do AddChat vbMagenta, "This loop executed." Var1 = Var1 + 1 Loop Until Var1 = 0
This loop will execute infinite times even though Var1 = 0
the first time. Only after the first execution it will check and find the statement to always be False.
Warning: The above code will cause an infinite loop.
While loop
The While
loop is nearly the same as the Do While
loop.
Var1 = 3 While Var1 = 3 AddChat vbMagenta, "This loop executed." Var1 = 0 Wend
The above loop will execute one time because the While
statement will pass the first time, but fail the second time after 0 is stored in Var1
.
It is preferred to use Do While...Loop over While...Wend.
Notes
- As you can see, you can easily run into an infinite loop.
- You can use
Exit Do
to exit a Do...Loop and end up after theLoop
statement. You cannot use theExit
statement with the While....Wend loop (the only difference between this and aDo While
loop). - You can embed
Do
loops inside other loops or any other blocks. - You should indent your code for readability based on the starting and ending of the loop.