VBScript Conditional Statements
Two Types of Conditional Statements inwards VBScript
i) If Statement
ii) Select Case Statement
Types of Conditions
i) Single Condition (Positive Condition/Negative Condition)
Ex:
If a > b Then
.
.
------
If Not b > a Then
.
.
------------------
ii) Compound Condition
Ex:
If a > b And a > c Then
.
.
----------------------
iii) Nested Condition
Ex:
if a > b Then
If a > c Then
If a > d Then
.
.
.
--------------------------
Usage of Conditional Statements inwards UFT Test Automation
i) To Insert Verification Points
ii) For Error handling
Usage of Conditional Statements
i) Execute a Statement when Condition is True/Simple if
ii) Execute a block of Statements when Condition is True
iii) Execute a block of Statements when Condition is True otherwise execute to a greater extent than or less other block of statements.
iv) Decide with several alternates (Elseif)
v) Execute a block of Statements when to a greater extent than than ane status is True (Nested if)
vi) Decide with several alternates (Using Select Case)
----------------------------------------
i) Execute a Statement when status 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 status 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
-----------------
Dim a, b, c
a = 100
b = 90
c = 70
If a > b And a > c Then
Msgbox "A is a Big Number"
End If
-----------------------------------
iii) Execute a block of statements when status is truthful otherwise execute to a greater extent than or less other block of statements.
Syntax:
If Condition Then
Statements
-----
---------
----------
Else
---------
---------
End If
Example:
Dim a, b
a = "delhi"
b = "London"
If a > b Then
Msgbox "A is a Big"
Else
Msgbox "B is a Big"
End If
------------------------
Note:
If nosotros role numeric values thence VBScript compares based on values
If nosotros role String type information thence VBScript compares based on ANSI Character codes.
'A to Z (65 to 90)
'a to z (97 to 122)
'0 to ix (48 to 57)
-------------------------
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 with several Alternates (ElseIf)
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 in addition to verify the Range of the Value
'If the value is inwards betwixt 1 in addition to 100 thence display Value is a Small Number
'If the value is inwards betwixt 101 in addition to grand thence display Value is a Medium Number
'If the value is inwards betwixt 1001 in addition to 10000 thence display Value is a Big Number
'If the value is to a greater extent than than 10000 thence display Value is a High Number
'Otherwise display Value is either zilch or negative Value.
'---------------------------------------------------
Dim val
val = InputBox("Enter a Value")
If val >= 1 And val <=100 Then
Msgbox "Val is a Small Number"
Elseif val > 100 And val <=1000 Then
Msgbox "Val is a Medium Number"
Elseif val > grand And val <=10000 Then
Msgbox "Val is a Big Number"
Elseif val > 10000 Then
Msgbox "Val is a High Number"
Else
Msgbox "Val is either Zero or Negative Number"
End If
--------------------------------
'With Error Handling
Dim val
val = InputBox("Enter a Value")
If IsNumeric(val) = True Then
If val >= 1 And val <=100 Then
Msgbox "Val is a Small Number"
Elseif val > 100 And val <=1000 Then
Msgbox "Val is a Medium Number"
Elseif val > grand And val <=10000 Then
Msgbox "Val is a Big Number"
Elseif val > 10000 Then
Msgbox "Val is a High Number"
Else
Msgbox "Val is either Zero or Negative Number"
End If
Else
Msgbox "Invalid Input"
End If
-----------------------------------------
v) Execute a block of Statements when to a greater extent than than ane status is True(Nested Condition)
Syntax:
If Condition Then
If Condition Then
If Condition Then
Statements
--------
-----------
Else
---------
--------
End If
End If
End If
-------------------
Example:'Read a value in addition to Verify whether the Value is valid Mobile Number or not?
'Should last a Numeric value
'Must comprise 10 digits
'Should origin with either ix or 8
'------------------------------------
Dim val
val = InputBox("Enter a Value")
If IsNumeric(val) Then
If Len(val) = 10 Then
If Left(val, 1) = ix Or Left(val, 1) = 8 Then
Msgbox "Val is a Valid Mobile Number"
Else
Msgbox "Val is an Invalid Mobile Number"
End If
Else
Msgbox "Val is non a 10 digit value"
End If
Else
Msgbox "Val is non a Numeric Value"
End If
------------------
Assignment
Handle . Symbol
------------------------------------------
'Using Compound Condition
Dim val
val = InputBox("Enter a Value")
If IsNumeric(val) And Len(val) = 10 And (Left(val, 1) = ix Or Left(val, 1) = 8) Then
Msgbox "Val is Valid Mobile Number"
Else
Msgbox "Val is an Invalid Mobile Number"
End If
---------------------------------------------
vi) Decide with several alternates using Select Case
Syntax:
Select instance Test Expression
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 an Operation"))
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
-----------------------------------------------
Two Types of Conditional Statements inwards VBScript
i) If Statement
ii) Select Case Statement
Types of Conditions
i) Single Condition (Positive Condition/Negative Condition)
Ex:
If a > b Then
.
.
------
If Not b > a Then
.
.
------------------
ii) Compound Condition
Ex:
If a > b And a > c Then
.
.
----------------------
iii) Nested Condition
Ex:
if a > b Then
If a > c Then
If a > d Then
.
.
.
--------------------------
Usage of Conditional Statements inwards UFT Test Automation
i) To Insert Verification Points
ii) For Error handling
Usage of Conditional Statements
i) Execute a Statement when Condition is True/Simple if
ii) Execute a block of Statements when Condition is True
iii) Execute a block of Statements when Condition is True otherwise execute to a greater extent than or less other block of statements.
iv) Decide with several alternates (Elseif)
v) Execute a block of Statements when to a greater extent than than ane status is True (Nested if)
vi) Decide with several alternates (Using Select Case)
----------------------------------------
i) Execute a Statement when status 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 status 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
-----------------
Dim a, b, c
a = 100
b = 90
c = 70
If a > b And a > c Then
Msgbox "A is a Big Number"
End If
-----------------------------------
iii) Execute a block of statements when status is truthful otherwise execute to a greater extent than or less other block of statements.
Syntax:
If Condition Then
Statements
-----
---------
----------
Else
---------
---------
End If
Example:
Dim a, b
a = "delhi"
b = "London"
If a > b Then
Msgbox "A is a Big"
Else
Msgbox "B is a Big"
End If
------------------------
Note:
If nosotros role numeric values thence VBScript compares based on values
If nosotros role String type information thence VBScript compares based on ANSI Character codes.
'A to Z (65 to 90)
'a to z (97 to 122)
'0 to ix (48 to 57)
-------------------------
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 with several Alternates (ElseIf)
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 in addition to verify the Range of the Value
'If the value is inwards betwixt 1 in addition to 100 thence display Value is a Small Number
'If the value is inwards betwixt 101 in addition to grand thence display Value is a Medium Number
'If the value is inwards betwixt 1001 in addition to 10000 thence display Value is a Big Number
'If the value is to a greater extent than than 10000 thence display Value is a High Number
'Otherwise display Value is either zilch or negative Value.
'---------------------------------------------------
Dim val
val = InputBox("Enter a Value")
If val >= 1 And val <=100 Then
Msgbox "Val is a Small Number"
Elseif val > 100 And val <=1000 Then
Msgbox "Val is a Medium Number"
Elseif val > grand And val <=10000 Then
Msgbox "Val is a Big Number"
Elseif val > 10000 Then
Msgbox "Val is a High Number"
Else
Msgbox "Val is either Zero or Negative Number"
End If
--------------------------------
'With Error Handling
Dim val
val = InputBox("Enter a Value")
If IsNumeric(val) = True Then
If val >= 1 And val <=100 Then
Msgbox "Val is a Small Number"
Elseif val > 100 And val <=1000 Then
Msgbox "Val is a Medium Number"
Elseif val > grand And val <=10000 Then
Msgbox "Val is a Big Number"
Elseif val > 10000 Then
Msgbox "Val is a High Number"
Else
Msgbox "Val is either Zero or Negative Number"
End If
Else
Msgbox "Invalid Input"
End If
-----------------------------------------
v) Execute a block of Statements when to a greater extent than than ane status is True(Nested Condition)
Syntax:
If Condition Then
If Condition Then
If Condition Then
Statements
--------
-----------
Else
---------
--------
End If
End If
End If
-------------------
Example:'Read a value in addition to Verify whether the Value is valid Mobile Number or not?
'Should last a Numeric value
'Must comprise 10 digits
'Should origin with either ix or 8
'------------------------------------
Dim val
val = InputBox("Enter a Value")
If IsNumeric(val) Then
If Len(val) = 10 Then
If Left(val, 1) = ix Or Left(val, 1) = 8 Then
Msgbox "Val is a Valid Mobile Number"
Else
Msgbox "Val is an Invalid Mobile Number"
End If
Else
Msgbox "Val is non a 10 digit value"
End If
Else
Msgbox "Val is non a Numeric Value"
End If
------------------
Assignment
Handle . Symbol
------------------------------------------
'Using Compound Condition
Dim val
val = InputBox("Enter a Value")
If IsNumeric(val) And Len(val) = 10 And (Left(val, 1) = ix Or Left(val, 1) = 8) Then
Msgbox "Val is Valid Mobile Number"
Else
Msgbox "Val is an Invalid Mobile Number"
End If
---------------------------------------------
vi) Decide with several alternates using Select Case
Syntax:
Select instance Test Expression
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 an Operation"))
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
-----------------------------------------------