VBScript Operators

VBScript Operators used in UFT or QTP
VBScript Operator
VBScript Operator

What is an Operator in VBScript?

An Operator is a keyword that operates on two operands to generate an output.Operators are used for performing mathematical, comparison and logical operations.An operator acts on one or more operands when comparing,assigning,concatenating, calculating and performing some logical operations.This tutorial holds good for Operators in QTP as well.

VBScript has a full range of operators, including

Ads code goes here
  • Assignment operator
  • Arithmetic operators
  • Comparison operators
  • Concatenation operators
  • Logical operators
  • Bitwise Operators

Assignment Operator:

Assignment operator is used to assign a value to a or series of  variable.

SignNamesyntaxDescription
=Assignment OperatorVariable=valueAssigns the right hand side value or expression to right hand side
Exp=x=5

Arithmetic Operator:

Arithmetic operators supports arithmetic operations on numeric values.We can use them with assignment operator.

SignNameSyntaxDescription
^Exponentiatial2^5this operator raises a given number to the power of a given exponent.

The number can be negative if the exponent is an integer. The exponent is evaluated from left to right.

Incase there are more than one exponent in a single expression, the result will be “Null” if the number or exponent is “Null”.

*Multiplication2*5Multiplies two numbers/variables.

In case an expression is empty , it will be treated as zero(0). The result is Null if one or both the expressions are Null.

/Floating-point division5/4returns a floating point result when this operator is applied.

In case an expression is empty , it will be treated as zero(0). The result is Null if one or both the expressions are Null.

Division3/2returns the integer from the division operation.

In case an expression is empty , it will be treated as zero(0). The result is Null if one or both the expressions are Null.

ModModulo3 mod 2Returns the remainder when dividing two given numbers/variables.

In case an expression is empty , it will be treated as zero(0). The result is Null if one or both the expressions are Null.

+Addition3+2returns the result after addition of two given number/variable.

The result is an integer if both expressions ate Empty. In case one expression is empty then it has no impact on other expression. The other expression will be unchanged.

If one or both the expressions are Null then the result will be null.

 

Subtraction3-2returns the result after subtraction of two given number/variable.

In case an expression is empty , it will be treated as zero(0). The result is Null if one or both the expressions are Null.

Note:The numeric expressions are rounded to byte/integer/long subtype before integer division takes place.Subtraction supports the unary Subtraction as well
Example-


 -(5-3)=-2
 

For addition if both the variables are of type numeric then they will add. If one of them are String,it will throw VBRuntime error.If both are type String they will concatenate to Single String.
Like-


"Hi"+5//generates run time error
"1"+"5"="15"
"Hi"+" "+"Good Moring"="Hi Good Morning" 
Unary Negation:

Unary negation is part of arithmetic operator.The unary negation operator(-)  indicates the negative value of the numeric expression. It is applicable for variable or value.

Example:

Option Explicit
Dim a
a=-45
msgbox a 
//it represents the negative number

 

Comparison operators:

Comparison operators enables the coder to test two expressions. It compares if the two expressions are True or False.Hence the datatype of Comparison operator is always boolean.

SignNameSyntaxDescription
=Equal tovariable1=variable2returns true if variable1=variable2
else returns false
<>Not equal tovariable1<>variable2returns true if variable1<>variable2
else returns false
<less thanvariable1<variable2returns true if variable1<variable2
else returns false
>greater thanvariable1>variable2returns true if variable1>variable2
else returns false
>=Greater than or equal tovariable1>=variable2returns true if variable1>=variable2
else returns false
<=Less than or equal tovariable1<=variable2returns true if variable1<=variable2
else returns false
IsCompare objectsObject1 Is Object2returns true if Object1 Is Object2
else returns false
Is NotCompare objectsObject1 Is Not Object2returns true if Object1 Is Not Object2
else returns false

In last two cases VbScript Engine checks if both the objects refers to the same memory locations.We need to use Nothing keyword with Is or Is Not operator to check the location or object reference’s validity. It will return true if the object has been destroyed before this operation.

Example:

Set a =CreateObject("Scripting.Dictionary")
Set b =CreateObject("Scripting.Dictionary")
Set c =CreateObject("Scripting.Dictionary")

Set a=c
Set b=c

msgbox a Is b

The output is true.

Note: Is(compare object operator) does not check the value in order to test if they are same kind of object. Rather it verifies if two objects refers to the same object.(Or two objects refers to the same memory location)

Concatenation operators:

SignNameSyntaxDescription
&AmpersandVariable1 & variable 2it concatenates two given expressions.

In case an expression is empty , it will be treated as space (” “). The result is Null if both the expressions are Null.

Incase one expression is null, then it will also be treated as zero length string(“”)

+PlusVariable1 + variable 2it concatenates two given expressions if they are string.

The & operator converts the variable to String Subtype before concatenation that means when we use ‘&’ and concatenate a variable with string then an implicit conversion takes place from the result subtype to String subtype. Then it gets concatenated.

Be careful with & and + operator in VBscript as well as UFT.

+ operator concatenates when both the variables are String.


Dim a,b,c,d
a="Hello"
b="Good Morning"
c=a + b
msgbox c
d=a & b
msgbox d
 

However for other datatypes + has a different side effect.


Dim a
Dim b
a=cStr(10)
b=CLng(50)
msgbox a + b
 

Here the output is different than we expected. The bottom line is that we need to make sure before we apply + operator. In case of + operator the operands are of type String.Other wise for concatenation we need to use &. It is safe.

+ should be used in mathematical context as an arithmetic operator.

The summary:

Variable Type1Variable Type2Operator +
numericnumericWill add them
StringStringWill Concatenate them
numericStringWill try to add them if the String is not in proper format
we will get subtype mismatch error

Logical operators:

Logical operators can be used to test or perform logical operations or expressions. We use them mostly in control statements.

SignNameSyntaxDescription
NotLogical Notif (Not variable1)Returns the logical negation of an expression,except Null.Null will return Null only.
It returns false if the expression is true and returns true if the expression is false.
ANDLogical ANDexpression1 AND expression2It checks if both expressions are true.Expressions need to be evaluated as boolean.
It returns true if both the expressions are true,false otherwise.
ORLogical ORexpression1 OR expression2It checks if one of the expressions is true.Expressions need to be evaluated as boolean.
It returns true if one of the expressions are true,false otherwise
XORLogical XORexpression1 XOR expression2It checks if one and only one expression is true.It returns true when one of the expressions is true,
false otherwise.Incase of Null,Null will be returned,if one of the expression is evaluated as Null.
EqvLogical Equivalenceexpression1 Eqv expression2A equivalence operation to check if both expressions or variables evaluates same value.
In case if one of the expression is evaluated as Null,this check will return Null.
ImpLogical Implicationexpression1 Imp expression2Logical implication operator follows the following table as truth table.

The truth table for logical implication:

Expression1Expression2Output
truetruetrue
falsetruetrue
falsefalsetrue
falseNulltrue
Nulltruetrue
truefalsefalse
trueNullNull
NullfalseNull
NullNullNull

Bitwise Operators:

Bitwise operators are comparison operators. They check bit by bit information while comparing.

SignNameSyntaxDescription
NotBitwise Notif (Not variale1/Expression1)Not is used to invert the bit value.It will return 1 if the bit is 0 and will return 0 if the bit value is 1.
Expression-111 will output as 000(Not Expression)
ANDBitwise ANDexpression1 AND expression2AND is used to check if both the bits are set to 1.
It returns 1 if both the bits are 1.0 otherwise.
Expression1=1000
Expression2=1010
Output=1000
ORBitwise ORexpression1 OR expression2OR is used to check if one bit or both the bits is either expression is set to 1
returns 1 if one of the bits is 1,0 therwise
Expression1=1010
Expression2=0101
Output=1111
XORBitwise XORexpression1 XOR expression2XOR is used to check if one and only one bits is set to 1.
Returns if only one of the bits is 1 , 0 otherwise.Expression1=1010
Expression2=0110
Output=1100
EqvBitwise Equivalenceexpression1 Eqv expression2Eqv is used to check if both bits are of same value.
It returns 1 if both bits are same value
returns 0 if both bits are different value.Expression1=1001
Expression2=1011
Output=1101
ImpBitwise Implicationexpression1 Imp expression2Imp is used to perform implication of 2 bits. If follows the below truth table.

Expression1=1010
Expression2=1110
Output=1111

The truthtable of Bitwise Implication is given as:

Expression1Expression2Output
1
11
111
1

Operator Precedence:

When several operations occur in an expression, each part is evaluated and resolved in a predetermined order called operator precedence.

We can use parentheses to override the order of precedence and force some parts of an expression to be evaluated before others.

Operations within parentheses are always performed before those outside. Within parentheses, however, standard operator precedence is maintained.

When expressions contain operators from more than one category, arithmetic operators are evaluated first, comparison operators are evaluated next, and logical operators are evaluated last.

Comparison operators all have equal precedence; that is, they are evaluated in the left-to-right order in which they appear.

Arithmetic and logical operators are evaluated in the following order of precedence.

Precedence Table in VBScript

OperatorPrecedenceExpressions
Arithmetic Operator1^,-,*,\,Mod,+,-
Concatenation Operator2&,+
Comparison Operator3=,<>,<,>,<=,>=,IS
Bitwise Operator4NOT,AND,OR,XOR,Eqv,Imp
Assignment Operator5=

Difference between ampersand(&) character and the underscore (_) character?

Ampersand (&) character is an operator to use concatenation of Strings. it is used to join two small strings together. On the other hand underscore (_) character is to continue a VBScript long statement across multiple lines.

Share and Enjoy !

READ  Generate HTML Report Through QTP/UFT

Leave a Comment

Your email address will not be published. Required fields are marked *