VBScript Conditional Statements


VBScript Conditional Statements
Two Types of Conditional Statements in 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 in 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 another block of statements.

iv) Decide among several alternates (Elseif)

v) Execute a block of Statements when more than one condition is True (Nested if)

vi) Decide among several alternates (Using Select Case)
----------------------------------------
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
-----------------
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 condition is true otherwise execute another 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 we use numeric values then VBScript compares based on values

If we use String type data then VBScript compares based on ANSI Character codes.

'A to Z (65 to 90)

'a to z (97 to 122)

'0 to 9 (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 among 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 and verify the Range of the Value
'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 a High Number
'Otherwise display Value is either zero 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 > 1000 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 > 1000 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 more than one condition 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 and Verify whether the Value is valid Mobile Number or not?
'Should be a Numeric value
'Must contain 10 digits
'Should start with either 9 or 8
'------------------------------------
Dim val
val = InputBox("Enter a Value")

If IsNumeric(val) 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 an Invalid Mobile Number"
        End If
        Else
        Msgbox "Val is not a 10 digit value"
    End If
    Else
    Msgbox "Val is not 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) = 9 Or Left(val, 1) = 8) Then
    Msgbox "Val is Valid Mobile Number"
    Else
    Msgbox "Val is an Invalid Mobile Number"
End If
---------------------------------------------
vi) Decide among several alternates using Select Case
Syntax:
Select case 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
-----------------------------------------------

0 comments:

Post a Comment