Looping Statements VBScript | Automation Testing QTP/UFT

Looping Statements VBScript | Automation Testing QTP/UFT

Looping Statements VBScript:

Control statements are subdivided in to two types.

  1. Conditional Control Statements
  2. Looping Control Statements

Here we are going to see Looping Statements VBScript. In the previous post we talked about Conditional Statements.

LOOPING STATEMENTS VBScript:

Looping control statements allow to run a group of statements repeatedly when the condition is true.
Following looping control statements are available in VBScript:
While…Wend statement – It executes only when a condition is true. Use the Do-Loop statement instead of this.
Do – Loop While statement – loops  while a condition is true
Do While – Loop statement – It executes one time even if the condition is False
Do – Loop Until statement – loops  until a condition is true
For…Next statement – runs code a specified number of times
For Each…Next statement – runs code for each item in a collection or each element of an array

SIMPLE WHILE STATEMENT: (Pre-conditional looping statement)

It executes only when a condition is True.

SYNTAX:
1
2
3
WHILE
set of statements
WEND
Example:
1
2
3
4
5
6
Dim a
a=0
while a<=10
msgbox a
a=a+1
wend

DO-WHILE STATEMENT: (Post-conditional looping statement)

It gets execute at least one time even if the condition is False because the condition in the while statement gets checked at the end of the first iteration.

SYNTAX:
1
2
3
DO
set of statements
LOOP WHILE

Example:

1
2
3
4
5
6
7
Dim a
a=11
do
msgbox a
a=a+1
loop while a<=10
msgbox “end of script”

DO-WHILE STATEMENT: (PRE CONDITIONAL DO-WHILE)

It gets executed the set of statements only when the condition in the while statement holds true.

SYNTAX:
1
2
3
DO WHILE
set of statements
LOOP

Example:

1
2
3
4
5
Dim a
a=11
do while a<=10
msgbox a
loop

change the value of a and try how it works.

Incase if the program is keep on running. You could end the task by navigating the following path:
Task Manager – Processes – Apps – Microsoft Windows Based Script Host

DO-UNTIL STATEMENT:

It gets executed until the condition becomes true.

SYNTAX:
1
2
3
DO
set of statements
LOOP UNTIL
Example:
1
2
3
4
5
6
7
Dim a
a=0
do
msgbox a
a=a+1
loop until a>10
msgbox End of script

How to Exit a “DO” Loop
Use the keyword EXIT to come out of the loop:
In the above example, the code executes until the value of “a” is greater than “10”. Let’s see how to exit from the loop when the value of a reaches “5”

1
2
3
4
5
6
7
8
9
10
Dim a
a=0
do
msgbox a
a=a+1
if a = 5 then
exit do
end if
loop until a>10
msgbox “End of script”

FOR LOOP:

There are two types of for loops: 1. Incremental and 2. Decrement

INCREMENTAL:
SYNTAX:
1
2
3
For i=0 to n [step 1]
set of statements
next

[step 1] is not mandatory for one step increment

DECREMENTAL:
SYNTAX:
1
2
3
For i=n to 0 [step 1]
set of statements
next

[step -1] is mandatory here

Example:
1
2
3
4
5
6
Option explicit
Dim i
For i=0 to 10
Msgbox i
Next
Msgbox “end of script”
Example:
1
2
3
4
5
6
Option explicit
Dim i
For i=0 to 10 step 2
Msgbox i
Next
Msgbox “end of script”
Example:
1
2
3
4
5
6
Option explicit
Dim i
For i=10 to 0 step 1
Msgbox i
Next
Msgbox “end of script”

How to Exit a “FOR” Loop

Use the keyword EXIT to come out of the loop:

Syntax:
1
2
3
4
5
6
For i=0 to n [step 1]
set of statements
if i=5 Then
exit for
End if
next
Example:
1
2
3
4
5
6
7
8
9
option explicit
Dim i
For i=1 to 10 step 1
If i=5 Then
Exit for
End if
Msgbox i
Next
Msgbox end of script

FOR EACH STATEMENT:

repeats a block of code for each element of an array.

SYNTAX:
1
2
3
FOR EACH <> IN <>
set of statements
NEXT
Example:
1
2
3
4
5
6
7
Dim site(2)
site(0)=“Software”
site(1)=“Testing”
site(2)=“Material”
FOR EACH sitepart IN site
msgbox(sitepart)
Next

VBScript Series:

VBScript for Automation (QTP/UFT) Testing – Part 1

VBScript for Automation (QTP/UFT) Testing – Part 2

VBScript for Automation (QTP/UFT) Testing – Part 3

VBScript for Automation (QTP/UFT) Testing – Part 4

VBScript for Automation (QTP/UFT) Testing – Part 5

ghtechproject.net