VBScript Looping Statements


VBScript Looping Statements
 

VBScript Flow Control Statements
    i) Conditional Statements
    ii) Looping Statements

Four Loop Structures in VBScript
 

i) For...Next

ii) While...Wend

iii) Do While / Until...Loop

iv) For Each...Next
-----------------------------
Loop statements used for repetitive execution.

i) For...Next
 

It repeats a block of statements for a specified number of times.

Syntax:
 

For Counter(Variable) = Start to End Step Increment/Decrement
Statements
----------
---------
--------
Next

Examples:
 

For i = 1 To 5 Step 1
    Msgbox i& " Hello UFT"
Next
-------------------------------------
'Open 1 to 5 Orders using For loop

 For ordNumber = 1 To 5
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set ordNumber
Wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
Next
-------------------------------------
'Open 5 to 1 Orders 

For ordNumber = 5 To 1 Step -1
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set ordNumber
Wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
Next
-----------------------------------
'Open even numbers up to 10

 For ordNumber = 2 To 10 Step 2
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set ordNumber
Wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
Next
----------------------------------------------
'Open 1 to 5 Orders except 4th Order

 For ordNumber = 1 To 5 Step 1
If ordNumber <> 4 Then
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set ordNumber
Wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
End If
Next
----------------------------------
'Open 1 to 10 Orders except 4th and 7th Orders 

For ordNumber = 1 To 10 Step 1
If ordNumber <> 4 And ordNumber <> 7 Then
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set ordNumber
Wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
End If
Next
------------------------------------
'Terminating For loop 

For ordNumber = 1 To 5 Step 1
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set ordNumber
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click

If Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").Exist(4) Then
Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").WinButton("OK").Click
Window("Flight Reservation").Dialog("Open Order").WinButton("Cancel").Click
Window("Flight Reservation").WinButton("Button_2").Click
Reporter.ReportEvent micWarning, "Res", "Up to " &ordNumber-1 & " Orders only exist"
Exit For
End If
Next
--------------------------------------
ii) While...Wend
 

It repeats a block of statements while condition is true.

Syntax: 


Initialization
While Condition
Statements
-----------
-----------
------------
Increment/Decrement
Wend

Examples: 


ordNumber = 1
While ordNumber  <=5
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set ordNumber
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
ordNumber = ordNumber+1
Wend
--------------------------------------
'Open 5 to 1 Orders
ordNumber = 5
While ordNumber  >=1
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set ordNumber
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
ordNumber = ordNumber-1
Wend
------------------------------------------
iii) Do While / Until...Loop

a) Using While
 

It repeats a block of statements while condition is True

1)
Initialization
Do While Condition
Statements
-----------
------------
---------
Increment/Decrement
Loop
-----------------
2)
Initialization
Do
Statements
-----------
------------
---------
Increment/Decrement
Loop While Condition
-------------------------

b) Using Until keyword
 

It repeats a block of statements until condition is True.

3)
Initialization
Do Until Condition
Statements
----------
----------
-------
Increment/Decrement
Loop
-----------------
4)
Initialization
Do
Statements
----------
----------
-------
Increment/Decrement
Loop Until Condition
----------------------------------
Examples:
 

ord = 1
Do While ord <= 5
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set ord
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click   
ord = ord + 1
Loop
--------------------------------------
ord = 10
Do
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set ord
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click   
ord = ord + 1
Loop While ord <= 5
-----------------------------------------------------
ord = 20
Do While ord <= 30
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set ord
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click

If Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").Exist (4) Then
Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").WinButton("OK").Click
Window("Flight Reservation").Dialog("Open Order").WinButton("Cancel").Click
Window("Flight Reservation").WinButton("Button_2").Click
Reporter.ReportEvent 3, "Res", "Up to "& ord-1 &" Orders only exist"
Exit Do
End If
ord = ord + 1
Loop
----------------------
ord = 1
Do Until ord > 5
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set ord
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
ord = ord + 1
Loop
-------------------------------
ord = 11
Do
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set ord
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
ord = ord + 1
Loop Until ord > 5
----------------------------------------
iv) For Each...Next
 

It executes all elements in an Array

Syntax:
 

For Each Element in Array
Statements
Next
---------------------
Example:
 

Dim a, b, res(3)
a = 10
b = 20

res(0) = "Addition of a, b is: " & a + b
res(1) = "Subtraction of a, b is: " & a - b
res(2) = "Multiplication of a, b is: " & a * b
res(3) = "Division of a, b is: " & a / b

For Each Element in res
    Msgbox Element
Next
----------------------------------------------------
VBScript Constants
 

Constants are used to replace literal values and they never change.

Two types of Constants

i) Built in Constants

ii) User defined Constants

Syntax:

Const Constant1Name = Value, Constant2Name = Value


Example:

Const city ="London", num =100

Constants                          Variables 

-------------------------------------------------
Const city="London"            Dim city
----------                            city="London"
---------------                      ------------
------------                          ------------
-------------                         city="Delhi"
city="Delhi"*Error                 ---------
----------------------------------------------

0 comments:

Post a Comment