Looping Statements VBScript | Automation Testing QTP/UFT
Looping Statements VBScript:
Control statements are subdivided in to two types.
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:
WHILE set of statements WEND
Example:
Dim a a=0 while aDO-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:
DO set of statements LOOP WHILEExample:
Dim a a=11 do msgbox a a=a+1 loop while aDO-WHILE STATEMENT: (PRE CONDITIONAL DO-WHILE)
It gets executed the set of statements only when the condition in the while statement holds true.SYNTAX:
DO WHILE set of statements LOOPExample:
Dim a a=11 do while a 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 HostDO-UNTIL STATEMENT:
It gets executed until the condition becomes true.SYNTAX:
DO set of statements LOOP UNTILExample:
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”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:
For i=0 to n [step 1] set of statements next[step 1] is not mandatory for one step incrementDECREMENTAL:
SYNTAX:
For i=n to 0 [step -1] set of statements next[step -1] is mandatory hereExample:
Option explicit Dim i For i=0 to 10 Msgbox i Next Msgbox "end of script"Example:
Option explicit Dim i For i=0 to 10 step 2 Msgbox i Next Msgbox "end of script"Example:
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:
For i=0 to n [step 1] set of statements if i=5 Then exit for End if nextExample:
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:
FOR EACH IN set of statements NEXTExample:
Dim site(2) site(0)="Software" site(1)="Testing" site(2)="Material" FOR EACH sitepart IN site msgbox(sitepart) NextVBScript 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