VBScript Conditional statements

VBScript Conditional statements

We use Conditional statements to write VBScript code that makes decisions and repeats actions. 

Root:

Visual Basic Scripting Edition
        
          Flow Control Statements

                    Conditional Statements

                              If Statement
                         Select Case Statement
----------------------------------------------------------------- 
Types of Conditional statements

VBScript has 2 types of conditional statements,

a) If Statement

b) Select case Statement

Usage of Conditional statements

VBScript has 6 types of usage

a) Execute a Statement when condition is True/Simple If

Syntax:
--------

If Condition Then Statement

Example:
-----------
Dim myDate
myDate = #10/10/2010#

If myDate < Date Then myDate = Date
Msgbox myDate

myDate = #10/10/2013#

If myDate < Date Then myDate = Date
Msgbox myDate
----------------------------------------------------------
b) Execute a block statements when condition is True

Syntax:
---------

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

Example:
----------
Dim a, b
a=100
b= 50

If a > b Then
        Msgbox "A is a Big Number"
End If
-------------------------------------------
c) Execute a block of statements when condition is True
Otherwise execute another block of statements when condition False

Syntax:
---------

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

Example1:
----------
Dim a, b
a=100
b= 500

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

a= Cdbl (a)
b= Cdbl (b)

'A-Z (65 to 90)
'a-z (97 to 122)
'0-9 (48 to 57)

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

If IsNumeric(a) = True And IsNumeric(b) = True Then
        a= Cdbl (a)
        b=Cdbl (b)
       
        If a > b Then
                Msgbox "A is a Big Number"
                Else
                Msgbox "B is a Big Number"
        End If
        Else
        Msgbox "Invalid Data"
End If
----------------------------------------------------------
d) Decide among several alternates/ElseIf

Syntax:
--------

If 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 "Val is a Small Number
'If the value is in between 101 and 1000 then display "Val is a Medium Number
'If the value is in between 1001 and 10000 then display "Val is a Big Number
'If the value is more than 10000 then display "Val is a High Number
'Otherwise display val is either Zero or Negative value
'----------------------------------------------------------------------------------------------------------

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 value"
       
        End If
        Else
        Msgbox "Invalid Data"
End If
---------------------------------------------------------------------
e) Execute a block of Statements when more than one condition is true

Nested If
-------------------

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

Example:
----------
'Read a value and verify weather the value is Valid Mobile Number or Not?
'Numeric Value
' 10 digit value
'should start with either 9 or 8
'then display value is a Valid Mobile Number
'---------------------------------------------------------------------------------------------

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 "It is a Valid Mobile Number"
                        Else
                        Msgbox "It is an 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

--------------------------------------------------------------
f) Decide among several alternates (Using Select Case)

Syntax:
--------

Select Case test expression

Case "case1 name"
Statements
-------
-------
Case "case2 name"
Statements
-------
-------
Case "case3 name"
Statements
-------
-------
Case Else
Statements
-------
-------
End Select

Example
----------
Dim num1, num2, operation
num1= 100
num2= 200

operation = LCase (InputBox ("Enter an Operation"))

Select Case operation

Case "add"
        Msgbox "Addition of num1, num2 is: "&num1+num2

Case "sub"
        Msgbox "Subtraction of num1, num2 is: "&num1-num2

Case "div"
        Msgbox "Division of num1, num2 is: "&num1/num2

Case "mul"
        Msgbox "Multiplication of num1, num2 is: "&num1*num2

Case Else
        Msgbox "Invalid Operation"

End Select
--------------------------------------------------------------------

Types of Conditions:

a) Single Condition

It contains a single condition statement

Syntax:

If Condition Then

Example: 

If x > y Then

b) Compound Condition / Multi  Condition

Compound Condition or Multiple conditions comprise more than one condition statement in a single condition item. 

Syntax:

If Condition  And / Or Condition Then
Example:

If x > y And x > z Then


c) Nested Conditions

Nested conditions comprise condition statements contained within the definition of other condition statements.


Syntax:

If Condition Then
 If Condition Then
  If Condition And / Or Condition Then


Example: 


If IsNumeric (Mobile_Number) = True Then
        If Len(Mobile_Number)=10 Then
                If Left(Mobile_Number, 1) =9 Or Left(val, 1) =8 Then

------------------------------------------------------------------------
Usage of VBScript Conditional Statements in QTP:

We use Conditional statements for Inserting Verification Points

We use Conditional statements for Error Handling
-----------------------------------------------------------------------------------


0 comments:

Post a Comment