Loops
There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in VBScript.
Loops are used seldom and only when data needs to be iterated over an array or collection of items/things.
Loop Type
VBScript provides the following types of loops to handle looping requirements.
for loop
Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
For…Each loop
This is executed if there is at least one element in group and reiterated for each element in a group.
While…Wend Loop
This tests the condition before executing the loop body.
Do..while loops
The do..While statements will be executed as long as condition is True.(i.e.,) The Loop should be repeated till the condition is False.
Do..until loops
The do..Until statements will be executed as long as condition is False.(i.e.,) The Loop should be repeated till the condition is True.
Loop Control Statements
Loop control statements change execution from its normal sequence.
for loop
A for loop is a repetition control structure that allows a developer to efficiently write a loop that needs to execute a specific number of times.
Syntax
The syntax of a for loop in VBScript is:
For counter = start To end [Step stepcount]
[statement 1]
[statement 2]
....
[statement n]
[Exit For]
[statement 11]
[statement 22]
....
[statement n]
Next
Flow Diagram
Here is the flow of control in a For Loop:
- The For step is executed first. This step allows you to initialize any loop control variables and increment the step counter variable.
- Secondly, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the For Loop.
- After the body of the for loop executes, the flow of control jumps to the Next statement. This statement allows you to update any loop control variables. It is updated based on the step counter value.
- The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the For Loop terminates.
Simple For Loop Example
Dim Result For i=0 to 5 Result = Result + i Next Result
When the above code is compiled and executed the variable Result will have a value of 15 (1 + 2 + 3 + 4 + 5)
Example With Step
Dim a Dim Result a=10 For i=0 to a Step 2 'i is the counter variable and it is incremented by 2 Result = Result + i Next Result
When the above code is compiled and executed the variable Result will have a value of 30 (2 + 4 + 6 + 8 + 10)
For…Each loop
A For Each loop is used when we want to execute a statement or a group of statements for each element in an array or collection.
A For Each loop is similar to For Loop; however, the loop is executed for each element in an array or group. Hence, the step counter won't exist in this type of loop and it is mostly used with arrays or used in context of File system objects in order to operate recursively.
Syntax
The syntax of a For Each loop in VBScript is:
For Each element In Group
[statement 1]
[statement 2]
....
[statement n]
[Exit For]
[statement 11]
[statement 22] Next
Example
Dim Result
Dim FruitNames
FruitNames = Array("apple","orange","cherries")
For Each Item in FruitNames
Result = Result & Item & ","
Next
Result
When the above code is executed, it prints all the fruitnames separated by a comma:
apple,orange,cherries,
While…Wend Loop
In a While..Wend loop, if the condition is True, all statements are executed until Wend keyword is encountered. If the condition is false, the loop is exited and the control jumps to very next statement after Wendkeyword.
Syntax
The syntax of a While..Wend loop in VBScript is:
While condition(s)
[statements 1]
[statements 2]
...
[statements n]
Wend
Flow Diagram:
Example
Dim Counter Dim Result Counter = 10 While Counter < 15 'Test value of Counter. Counter = Counter + 1 ' Increment Counter. Result = Result + Counter Wend Result
Do..while loops
A Do..While loop is used when we want to repeat a set of statements as long as the condition is true. The Condition may be checked at the beginning of the loop or at the end of the loop.
Syntax
The syntax of a Do..While loop in VBScript is:
Do While condition
[statement 1]
[statement 2]
...
[statement n]
[Exit Do]
[statement 1]
[statement 2]
...
[statement n]
Loop
Flow Diagram
Example
The below example uses Do..while loop to check the condition at the beginning of the loop. The statements inside the loop are executed only if the condition becomes True.
Dim i Dim Result i = 0 Do While i < 5 'Test value of i. i = i + 1 Result = Result + i Loop Result
When the above code is executed, the value of the Result variable is 15 (1 + 2 + 3 + 4 + 5)
Alternate Syntax
There is also an alternate Syntax for Do..while loop which checks the condition at the end of the loop. The Major difference between these two syntax is explained below with an example.
Do
[statement 1]
[statement 2]
...
[statement n] [Exit Do]
[statement 1]
[statement 2]
...
[statement n]
Loop While condition
Flow Diagram
Example
The below example uses Do..while loop to check the condition at the end of the loop. The Statements inside the loop are executed at least once even if the condition is False.
Dim i Dim Result i = 10 Do i = i + 1 Result = Result + i Loop While i < 5 'Test value of i. Result
When the above code is executed, the value of the Result variable is 11
Do..until loops
A Do..Until loop is used when we want to repeat a set of statements as long as the condition is false. The Condition may be checked at the beginning of the loop or at the end of loop.
Syntax
The syntax of a Do..Until loop in VBScript is:
Do Until condition
[statement 1]
[statement 2]
...
[statement n] [Exit Do]
[statement 1]
[statement 2]
...
[statement n] Loop
Flow Diagram
Example
The below example uses Do..Until loop to check the condition at the beginning of the loop. The Statements inside the loop are executed only if the condition is false. It exits out of the loop when the condition becomes true.
Dim i Dim Result i = 10 Do Until i > 15 ' 'Condition is False.Hence loop will be executed i = i + 1 Result = Result + i Loop Result
When the above code is executed, the value of the Result variable is 65 (11 + 12 + 13 + 14 + 15)
Alternate Syntax
There is also an alternate Syntax for Do..Until loop which checks the condition at the end of the loop. The Major difference between these two syntax is explained below with an example.
Do
[statement 1]
[statement 2]
...
[statement n]
[Exit Do]
[statement 1]
[statement 2]
...
[statement n]
Loop Until condition
Flow Diagram
Example
The below example uses Do..Until loop to check the condition at the end of the loop. The Statements inside the loop are executed atleast once even if the condition is True.
Dim i Dim Result i = 10 Do i = i + 1 Result = Result + i Loop Until i < 15 'Condition is True.Hence loop is executed once. Result
When the above code is executed, the value of the Result variable is 11
Loop Control Statements
Loop control statements change execution from its normal sequence. When execution leaves a scope, all the remaining statements in the loop are NOT executed.
Exit For statement
Terminates the For loop statement and transfers execution to the statement immediately following the loop
Exit Do statement
Terminates the Do While statement and transfers execution to the statement immediately following the loop
Exit For Statement
A Exit For Statement is used when we want to Exit the For Loop based on certain criteria. When Exit For is executed, the control jumps to next statement immediately after the For Loop.
Syntax
The syntax for Exit For Statement in VBScript is:
Exit For
Flow Diagram
Example
The below example uses Exit For. If the value of the Counter reaches 4, the For Loop is Exited and control jumps to the next statement immediately after the For Loop.
Dim i
Dim Result
For i=0 to a Step 2 'i is the counter variable and it is incremented by 2
Result = Result + i
If i=4 Then
Exit For 'Exited when i=4
End If
Next
Result
When the above code is executed, the value of the Result variable is 6 (2 + 4)
Exit Do Statement
An Exit Do Statement is used when we want to Exit the Do Loops based on certain criteria. It can be used within both Do..While and Do..Until Loops.
When Exit Do is executed, the control jumps to next statement immediately after the Do Loop.
Syntax
The syntax for Exit Do Statement in VBScript is:
Exit Do
Flow Diagram
Example
The below example uses Exit Do. If the value of the Counter reaches 10, the Do Loop is Exited and control jumps to the next statement immediately after the For Loop.
Dim i
Dim Result
i = 0
Do While i <= 100
If i > 10 Then
Exit Do
End If
Result = Result + i
Loop
Result
When the above code is executed, the value of the Result variable is 55 (1 + 2 + ... + 9 + 10)