Site icon Software Testing

VBScript Conditional Statements

VBScript Conditional Statements

VBScript for UFT

VBScript Conditional Statements, VBScript Language Fundamentals, VBScript Control Flow, VBScript If Statement, and ‘Select Case’ statement.

VBScript Conditional Statements

1. Execute a Statement when the 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 the 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 the 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


VBScript Tutorial
VBScript Videos
Follow me on social media: