VB Script Loops
Loops are set of instructions that repeat elements in specific number of
times. Counter variable is used to increment or decrement with each
repetition of the loop. The two major groups of loops are, For..Next
and Do..Loop. While..Wend is another type of Do..Loop.
The For statements are best used when you want to perform a loop in specific number of times.
The Do and While statements are best used to perform a loop an undetermined number
of times. For..Next syntax example.
Dim counter=0 for counter = 0 to 5
msgbox"The counter is: "&counter next
This example increments a variable counter from 0 to 5 and pops up message box
containing the current value of counter. The values are incremented by 1. We can modified how
the values are incremented by adding step # to the for counter statement.
Here is For..Next syntax example incrementing values by two.
Dim counter=0 for counter = 0 to 8 step 2
msgbox"The counter is: "&counter next
This display the value like this on message boxes: 0,2,4,6,8.
To decrement the values with each loop, switch the counter value big number to small number
using step -# Here is an example decrementing the counter value by 2
Dim counter=0 for counter = 10 to 1 step -2
msgbox"The counter is: "&counter next
This counter starts from 10 to 1 decrementing the values by 2.
Do..Loop
The Do..Loop structure repeats a block of statements until a specified
condition is met. There are three types of Do..Loops. Do..Until,
Do..While, and While..Wend. Do..While and While..Wend performs a loop statement as long
as the condition being tested is true while Do..Until performs a loop
statement as long as the condition tested is false. In both cases, you
have a choice to perform the test at start of the loop or at the end of the
loop. This Do..Until loop example performs the test at the start of the loop
dim number number=inputbox("What is 5x5?")
Do Until number=25 msgbox"Wrong answer, try again"
number=inputbox("What is 5x5?") loop msgbox"Your answer was right"
Bellow is the same example but performs the test at the end of the loop.
dim number number=inputbox("What is 5x5?")
Do msgbox"Wrong answer, try again"
number=inputbox("What is 5x5?") Loop Until number=25 msgbox"Your answer was right"
The syntax are same for Do..Until and Do..While but keep in mind
one tests false condition and the other tests true condtion. Bellow is Do..While example
that performs the test at the start of the loop.
dim number number=inputbox("What is 5x5?")
Do While number=25 msgbox"Correct answer, but do..while loops while true"
number=inputbox("What is 5x5?") loop msgbox"Wrong answer, loop terminated" Notice
the syntax are same for Do..Until and Do..While but the way they work
are opposite. This time, the loop will continues while the answer is correct which is 25.
The loop terminates when it detects any answer other than 25. Bellow is the same example
that performs the test at the end of the loop.
dim number number=inputbox("What is 5x5?")
Do msgbox"Correct answer, but do..while loops while true"
number=inputbox("What is 5x5?") loop while number=25
msgbox"Wrong answer, loop terminates"
The difference between performing the test at the start and at the end is
that. When tested at the end, the test is performed at least once when
other ways test may not be performed. For that reason, if you provide
right answer first time for this example, it will test and ask you again.
The previous test counts so if you provide wrong answer for the second time,
still the loop will terminate.
The syntax for Do..Loop and While..Loop are similar except key words Do and
Loop are not used instead While and Wend are independent. Bellow is
our previous example, this time using While..Wend loop.
dim number number=inputbox("What is 5x5?")
While number=25 msgbox"Correct answer, but do..while loops while
true"
number=inputbox("What is 5x5?") Wend msgbox"Wrong answer, loop
terminates"
As it did in Do..While, the loop continues while correct answer is provided. |