# VBScript Tutorial 3:

# VBScript Tutorial 3:

### (VBScript Variables Part-2, Constants, VBScript Operators, Conditional Statements Part-1)

#### iii) Variables

1) What is Variable?

2) Declaration of variables

3) Implicit and Explicit variables.

4) Assigning Values to Variables

5) Purpose of Variables

**6) Naming Restrictions**

———————–

i) Variable names should start with Alfa bytes

**Ex:**

Dim abc ‘Correct

Dim a12 ‘Correct

Dim 1ab ‘Incorrect

ii) Should not contain embedded periods

**Ex:**

Dim abc ‘Correct

Dim ab c ‘Incorrect

Dim ab.c ‘Incorrect

Dim ab*c ‘Incorrect

Dim ab_c ‘Correct

—-

iii) Must not exceed 255 characters

1 to 255

———-

iv) Must be unique in the scope of declaration.

Dim a, b, c ‘Correct

Dim d, e ‘Correct

Dim f, g, A ‘Incorrect

———————————–

**7) Scope of Variables**

i) Script Level Variables

They can be used for entire script.

ii) Function Level Variables

They can be used within the Functions only

**Example:**

———–

Dim a, b, c ‘Script level variables

a = 10

b = 20

c = a + b

Msgbox c ’30

Function xyz()

Dim d, e ‘Function level variables

d = 40

e = a + d

Msgbox e ’50

End Function

Call xyz()

Dim f, g ‘Script level variables

f = 70

g = b + d + f

Msgbox g ’90

——————————

Option Explicit

Dim a, b, c ‘Script level variables

a = 10

b = 20

c = a + b

Msgbox c ’30

Function xyz()

Dim d, e ‘Function level variables

d = 40

e = a + d

Msgbox e ’50

End Function

Call xyz()

Dim f, g ‘Script level variables

f = 70

g = b + d + f

Msgbox g ‘Error

———————————–

**8) Types of Variables**

i) Scalar Variable

To store one value at a time, Value may vary throughout the Execution.

ii) Array Variable

To store series of values at a time based on size of the Array.

Declare Array variable:

Dim a, b(3)

b(0) = 10

b(1) = “abcd”

b(2) = 1.34

b(3) = 40

a = b(0) + b(3)

Msgbox a ’50

——————

Dim a, b(3)

b(0) = 10

b(1) = “abcd”

b(2) = 1.34

b(3) = 40

b(4) = 50 ‘Error

a = b(0) + b(3)

Msgbox a ’50

———————————-

Dynamic Array

Dim a, b(3), c()

ReDim c(3)

c(0) = 10

c(1) = 20

c(2) = 30

c(3) = 40

a = c(1) + c(2)

Msgbox a ’50

ReDim c(5)

c(4) = 50

c(5) = 60

a = c(3) + c(5)

Msgbox a ’60

————————

Use Preserve keyword

Dim a, b(3), c()

ReDim c(3)

c(0) = 10

c(1) = 20

c(2) = 30

c(3) = 40

a = c(1) + c(2)

Msgbox a ’50

ReDim Preserve c(5)

c(4) = 50

c(5) = 60

a = c(3) + c(5)

Msgbox a ‘100

————————————

Dimensional Arrays

———————-

Dim a, b(3), c(), d(4, 5)

d(0, 0) = 10

#### Assigning series of Values at a time to Array variables

**a) Using Array Function**

Dim a

Msgbox IsArray(a) ‘False

a = Array(100, “VBScript”, 20, 30, 40, #10/10/2010#)

Msgbox IsArray(a) ‘True

Msgbox a(1) ‘VBScript

Msgbox UBound(a) ‘5

**b) Using Split Function**

Dim a, b

a = “VB Script Language”

msgbox isArray(b) ‘False

b = Split(a)

Msgbox IsArray(b) ‘True

Msgbox b(1) ‘Script

Msgbox UBound (b) ‘2

———————-

Dim a, b

a = “VB,Script,Language”

msgbox isArray(b) ‘False

b = Split(a, “,”)

Msgbox IsArray(b) ‘True

Msgbox b(1) ‘Script

Msgbox UBound (b) ‘2

————————–

Dim a, b

a = “VB@Script@Language”

msgbox isArray(b) ‘False

b = Split(a, “@”)

Msgbox IsArray(b) ‘True

Msgbox b(1) ‘Script

Msgbox UBound (b) ‘2

—————————-

Dim a, b

a = “VB@%$Script@%$Language”

msgbox isArray(b) ‘False

b = Split(a, “@%$”)

Msgbox IsArray(b) ‘True

Msgbox b(1) ‘Script

Msgbox UBound (b) ‘2

——————————–

Note: In Split Function default delimiter is space, we can use any value as delimiter, but we need specify the delimiter.

#### iv) Constants

They are used to replace literal values and they never change.

Constants 2 types

**i) Built in constants**

Ex: Msgbox VarType(“abcd”) ‘8

**ii) User defined constants**

Syntax:

Const ConstantName = ConstantValue

Or

Const Constant1Name = Constant1Value, Constant2Name = Constant2Value,Constant3Name = Constant3Value

Example:

Const city = “London”, num = 100, x = #10/10/2010#

——————————————–

Variables versus Constants

Dim city Const city = “Hyderabad”

city=”Hyderabad” ————-

———– —————

———– city =”Mumbai” ‘Error

———

city=”Mumbai”

———–

———–

———

city=”Delhi”

———–

———–

———

#### v) VBScript Operators

Operators are used to perform mathematical, comparison and logical operations.

Categories of operators:

i) Arithmetic Operators

ii) Comparison Operators

iii) Logical Operators

——-

* Concatenation operators (Part of Arithmetic Operators)

#### Operator precedence:

Operator precedence is VBScript operator priority process,

Generally VBScript evaluate operators from left to right, but if any high priority operator is there in right side then first VBScript evaluate high priority operator then general rule.

Note: In order to override operator precedence, use () symbol

**Example:**

Msgbox 2 + 2 * 4 ^ 2 ’34

Msgbox (2 + 2) * 4 ^ 2 ’64

—————–

Msgbox 2 + 2 * 4 ’10

Msgbox (2 + 2) * 4 ’16

————————————

**i) Arithmetic Operators**

1) Exponentiation ^

2) Multiplication *

3) Division /

4) Integer Division \

5) Mod Operator

6) Addition +

7) Subtraction –

8) Concatenation &

———————————

**Example:**

Dim a, b, c

a = 10

b = 3

c = a ^ b

Msgbox c ‘1000

c = a * b

Msgbox c ’30

c = a / b

Msgbox c ‘3.33333333333

c = a \ b

Msgbox c ‘3

c = a Mod b

Msgbox c ‘1

c = a + b

Msgbox c ’13

c = a – b

Msgbox c ‘7

c = a & b

Msgbox c ‘103

——————————–

**+ Operator**

—————–

Dim a, b, c

a = 10

b = 3

c = a + b

Msgbox c ’13

a = 10

b = “3”

c = a + b

Msgbox c ’13

a = “10”

b = “3”

c = a + b

Msgbox c ‘103

a = “Hydera”

b = “bad”

c = a + b

Msgbox c ‘Hyderabad

a = “Hyderabad”

b = “123”

c = a + b

Msgbox c ‘Hyderabad123

a = “Hyderabad”

b = 123

c = a + b

Msgbox c ‘Error

————————————-

**& Operator**

Dim a, b, c

a = 10

b = 3

c = a & b

Msgbox c ‘103

a = 10

b = “3”

c = a & b

Msgbox c ‘103

a = “10”

b = “3”

c = a & b

Msgbox c ‘103

a = “Hydera”

b = “bad”

c = a & b

Msgbox c ‘Hyderabad

a = “Hyderabad”

b = “123”

c = a & b

Msgbox c ‘Hyderabad123

#### a = “Hyderabad”

b = 123

c = a & b

Msgbox c ‘Hyderabad123

———————————

ii) Comparison Operators (All are Equal)

1) >

2) >=

3) <

4) <=

5) =

6) <>

————————–

Note: Comparison operators return Boolean (logical) result.

Ex: True / False

————————–

Example:

Dim a, b, c

a = 10

b = 3

c = a > b

Msgbox c ‘True

c = a >= b

Msgbox c ‘True

c = a <> b

Msgbox c ‘True

c = a < b

Msgbox c ‘False

c = a <= b

Msgbox c ‘False

c = a = b

Msgbox c ‘False

#### iii) Logical Operators

**1) Not (Logical Negation)**

If Not Dialog(“Login”).Exist(3) Then

SystemUtil.Run “C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe”,””,”C:\Program Files\HP\Unified Functional Testing\samples\flight\app\”,””

End If

Dialog(“Login”).Activate @@ hightlight id_;_525276_;_script infofile_;_ZIP::ssf1.xml_;_

Dialog(“Login”).WinEdit(“Agent Name:”).Set “asdf” @@ hightlight id_;_66772_;_script infofile_;_ZIP::ssf2.xml_;_

Dialog(“Login”).WinEdit(“Password:”).SetSecure “555fe8c872cfa7718d3ea9b61ac921c85ea7371b” @@ hightlight id_;_66774_;_script infofile_;_ZIP::ssf3.xml_;_

Dialog(“Login”).WinButton(“OK”).Click @@ hightlight id_;_66776_;_script infofile_;_ZIP::ssf4.xml_;_

————————————–

**2) And (Logical Conjunction)**

Dim a, b, c

a = 100

b = 90

c = 80

If a > b And a > c Then

Msgbox “A is a Big Number”

Else

Msgbox “A is Not a Big Number”

End If

———————

Result Criteria:

Exp1 Exp2 Result

————————

True True True

True False False

False True False

False False False

——————————–

**3) Or (Logical Disjunction)**

Result Criteria:

Exp1 Exp2 Result

————————

True True True

True False True

False True True

False False False

**4) XOr (Logical exclusion)**

Result Criteria:

Exp1 Exp2 Result

————————

True True False

True False True

False True True

False False False

#### vi) Flow Control Statements

Conditional Statements

Loop Statements

———————-

**Conditional Statements**

**a) Types of Conditional statements**

i) If Statement

ii) Select Case Statement

—————————-

**b) Types of Conditions**

i) Single Condition

ii) Compound Condition

iii) Nested Condition

—————————-

**c) Purpose of conditional statements in UFT:**

i) To insert verification points

ii) For Error handling

——————————

**d) 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 statement 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/ 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/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 = 500

If a > b Then

Msgbox “A is a Big Number”

End If

——————————————–

(221)