VBScript Tutorial 4

VBScript Tutorial 4:
(VBScript Conditional and Loop Statements)

I) VBScript Conditional Statements 
i) Execute a Statement when condition is True.
Syntax:

If Condition Then Statement

Example:

Dim myDate
myDate = #10/10/2010#

If myDate < Date Then myDate = Date
Msgbox myDate

myDate = #10/10/2017#

If myDate < Date Then myDate = Date
Msgbox myDate
------------------------------------------------
ii) Execute a Block of statements when condition is True.
Syntax:

If Condition Then
Statements
------------
-----------
------------
End If

Example:

Dim a, b
a = 100
b = 900

If a > b Then
    Msgbox "A is a Big Number"
End If
--------------------------------------
iii) Execute a Block of statements when condition is True, otherwise execute another block of statements.

Syntax:


If Condition Then
Statements
-----------
-----------
-----------
Else
Statements
-----------
-----------
-----------
End If

Examples:
-----------
Dim a, b
a = 100
b = 900

If a > b Then
    Msgbox "A is a Big Number"
    Else
    Msgbox "B is a Big Number"
End If
------------------
Dim a, b
a = InputBox("Enter A Value")
b = InputBox("Enter B Value")

If Cint (a) > Cint (b) Then
    Msgbox "A is a Big Number"
    Else
    Msgbox "B is a Big Number"
End If
-------------------------------
Dim a, b
a = InputBox("Enter A Value")
b = InputBox("Enter B Value")

If ISNumeric(a) = True  And IsNumeric(b) = True Then
If Cint (a) > Cint (b) Then
    Msgbox "A is a Big Number"
    Else
    Msgbox "B is a Big Number"
End If
Else
Msgbox "Invalid Input"
End If
-------------------------------------------------
iv) Decide among several alternates (Else If)

Syntax:

If Condition then
Statements
-------------
------------
----------
ElseIf Condition Then
Statements
-------------
------------
----------
ElseIf Condition Then
Statements
-------------
------------
----------
ElseIf Condition Then
Statements
-------------
------------
----------
Else
Statements
-------------
------------
----------
End If
------------------------------
Example:
'Read a Value and Verify the Range
'If the value is in between 1 and 100 then display "Value is a Small Number"
'If the value is in between 101 and 1000 then display "Value is a Medium Number"
'If the value is in between 1001 and 10000 then display "Value is a Big Number"
'If the value is more than 10000 then display "Value is High Number"
'Otherwise display "value is either Zero or Negative value.
'---------------------------------------------------------------------
Dim val
val = InputBox("Enter a Value")

If IsNumeric(val) Then
If val >= 1 And val <=100 Then
Msgbox "Value is Small Number"
   
ElseIf val > 100 And val <= 1000 Then
Msgbox "Value is a Medium Number"

ElseIf val > 1000 And val <= 10000 Then
Msgbox "Value is a Big Number"

ElseIf val > 10000 Then
Msgbox "Value is High Number"

Else
Msgbox "value is either Zero or Negative Value"
End If
Else
msgbox "Invalid Input"
End If
-----------------------------
v) Execute a block of statements when more than one condition is True
Syntax:

If Condition Then
 If Condition Then
  If Condition Then
  Statements
  ---------
  -----------
  Else
  ----------
  ---------
  End If
 End If
End If
------------------------------------
Example:

'Read a Value and verify whether the Value is Valid Mobile Number or not?
'Value should be Numeric
'Value must contain 10 digits
'First letter should be either 9 or 8
'-----------------------------------------------
Dim val
val = InputBox ("Enter a Value")

If IsNumeric(val) = True Then
    If Len(val) = 10 Then
        If Left(val, 1) = 9 Or Left(val, 1) = 8 Then
            Msgbox "val is a Valid Mobile Number"
            Else
            Msgbox "Val is Invalid Mobile Number"
        End If
        Else
        Msgbox "It is not a 10 digit value"
    End If
    Else
    Msgbox "It is not a Numeric Value"
End If
--------------------------------
'Handle . Symbol
----------------------------------------------
vi) Decide among several alternates (using Select Case)
Syntax:
Select Case TestExpression
Case "Case1Name"
Statements
----------
----------
Case "Case2Name"
Statements
----------
----------
Case "Case3Name"
Statements
----------
----------
Case Else
Statements
----------
--------
----------
End select
------------------------------------

Example:

Dim a, b, operation
a = 10
b = 20
operation = LCase (InputBox("Enter a value"))

Select Case operation
    Case "add"
    Msgbox "Addition of a, b is: "& a+b
   
    Case "sub"
    Msgbox "Subtraction of a, b is: "& a-b
   
    Case "mul"
    Msgbox "Multiplication of a, b is: "& a*b
   
    Case "div"
    Msgbox "Division of a, b is: "& a/b
   
    Case Else
    msgbox "Invalid Operation"
End Select
------------------------------------------
II) VBScript Loop Statements
a) For...Next
b) While...Wend
c) Do While / Until...Loop
d) For Each...Next
-----------------------------------------------
i) For...Next

Description:

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

Syntax:

For Counter (Variable) = Start to End Step Incr/Decr
Statements
---------
----------
-----------
Next

Example:

For i = 1 To 5 Step 1
    Msgbox i & " Hello UFT"
Next
----------------------------------------------
For OrderNumber = 1 To 10 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 OrderNumber
Wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
Next
-------------------------------
' Terminating For loop
For OrderNumber = 20 To 25 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 OrderNumber
Wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click

If Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").Exist(3) 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 "& OrderNumber -1 & " Orders only Exist"
Exit For
End If
Next
------------------------------------------------
ii) While...Wend

It repeats a block of statements while condition is True

Syntax:

While Condition
Statements
----------
----------
---------
Incr/Decr
Wend
-----------------------------------
Example:

OrderNo = 1
While OrderNo <= 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 OrderNo
Wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
OrderNo = OrderNo + 1
Wend
---------------------------------- 

iii) Do While / Until...Loop

Using While Keyword
It repeats a block of statements while condition is True

Using Until Keyword

It repeats a block of statements Until condition is True
-----------------------------------------
i)

Do While Condition
Statements
----------
----------
Incr/Decr
Loop

ii)
Do
Statements
----------
----------
Incr/Decr
Loop While Condition
---------------------------------
iii)

Do Until Condition
Statements
-----------
------------
----------
Incr/Decr
Loop

iv)
Do
Statements
-----------
------------
----------
Incr/Decr
Loop Until Condition
---------------------------------------
Examples:
OrderNo = 1
Do While OrderNo <= 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 OrderNo
Wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
OrderNo = OrderNo + 1
Loop
--------------------------------------------
OrderNo = 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 OrderNo
Wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
OrderNo = OrderNo + 1
Loop While OrderNo <= 5
--------------------------------
OrderNo = 1
Do Until OrderNo > 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 OrderNo
Wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
OrderNo = OrderNo + 1
Loop
--------------------------------
OrderNo = 1
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 OrderNo
Wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
OrderNo = OrderNo + 1
Loop Until OrderNo > 5
---------------------------------------
'Terminating Do loop
OrderNo = 20
Do Until OrderNo > 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 OrderNo
Wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click

If Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").Exist(3) 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 "& OrderNo-1 & " Orders only exist"
Exit Do
End If

OrderNo = OrderNo + 1
Loop
-------------------------------------
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
-------------------------------------------------
III VBScript Functions

> What is Function?

A reusable code.

> When we choose Functions?

Whenever we want to perform same operation multiple times then we choose Functions.

> Two types of Functions

i) Built in Functions

    Array functions
    String functions
    I/O Functions
    Date & Time Functions
    Conversion functions etc....

ii) User defined Functions

    a) Sub Procedures
    b) Function Procedures
--------------------------------
User defined Functions

a) Sub Procedures

Set of statements enclosed with Sub and End sub statements to perform an operation.

Sub Procedure takes Arguments, but Arguments are optional.

Sub Procedure won't return any value.

Syntax:
-----------
Sub procedureName (Argumnets)
Statements
-----------
---------
---------
End Sub
----------------------------------------------------
b) Function Procedures

Set of statements enclosed with Function and End Function statements to perform an Operation.

Function Procedure takes Arguments, but Arguments are optional

Function Procedure Can return a value.

Syntax:

Function procedureName (Arguments)
Statements
-----------
-------------
------------
End Function

Note: Sub procedures are faster in Execution than Function Procedures.
--------------------------------------------------------------
Access Modifiers

Public

Private
-----------------
Usage of Functions

Internal - Defining and calling within the script

External - Calling from external library files.
-------------------------------------------------

0 comments:

Post a Comment