VBScript Operators used in UFT or QTP

VBScript Operators

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

  • 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.

Sign Name syntax Description
= Assignment Operator Variable=value Assigns 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.

Sign Name Syntax Description
^ Exponentiatial 2^5 this 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”.

* Multiplication 2*5 Multiplies 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 division 5/4 returns 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.

Division 3/2 returns 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.

Mod Modulo 3 mod 2 Returns 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.

+ Addition 3+2 returns 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.

 

Subtraction 3-2 returns 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.

Sign Name Syntax Description
= Equal to variable1=variable2 returns true if variable1=variable2
else returns false
<> Not equal to variable1<>variable2 returns true if variable1<>variable2
else returns false
< less than variable1<variable2 returns true if variable1<variable2
else returns false
> greater than variable1>variable2 returns true if variable1>variable2
else returns false
>= Greater than or equal to variable1>=variable2 returns true if variable1>=variable2
else returns false
<= Less than or equal to variable1<=variable2 returns true if variable1<=variable2
else returns false
Is Compare objects Object1 Is Object2 returns true if Object1 Is Object2
else returns false
Is Not Compare objects Object1 Is Not Object2 returns 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:

Sign Name Syntax Description
& Ampersand Variable1 & variable 2 it 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(“”)

+ Plus Variable1 + variable 2 it 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 Type1 Variable Type2 Operator +
numeric numeric Will add them
String String Will Concatenate them
numeric String Will 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.

Sign Name Syntax Description
Not Logical Not if (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.
AND Logical AND expression1 AND expression2 It checks if both expressions are true.Expressions need to be evaluated as boolean.
It returns true if both the expressions are true,false otherwise.
OR Logical OR expression1 OR expression2 It 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
XOR Logical XOR expression1 XOR expression2 It 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.
Eqv Logical Equivalence expression1 Eqv expression2 A 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.
Imp Logical Implication expression1 Imp expression2 Logical implication operator follows the following table as truth table.

The truth table for logical implication:

Expression1 Expression2 Output
true true true
false true true
false false true
false Null true
Null true true
true false false
true Null Null
Null false Null
Null Null Null

Bitwise Operators:

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

Sign Name Syntax Description
Not Bitwise Not if (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)
AND Bitwise AND expression1 AND expression2 AND 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
OR Bitwise OR expression1 OR expression2 OR 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
XOR Bitwise XOR expression1 XOR expression2 XOR 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
Eqv Bitwise Equivalence expression1 Eqv expression2 Eqv 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
Imp Bitwise Implication expression1 Imp expression2 Imp 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:

Expression1 Expression2 Output
1
1 1
1 1 1
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

Operator Precedence Expressions
Arithmetic Operator 1 ^,-,*,\,Mod,+,-
Concatenation Operator 2 &,+
Comparison Operator 3 =,<>,<,>,<=,>=,IS
Bitwise Operator 4 NOT,AND,OR,XOR,Eqv,Imp
Assignment Operator 5 =

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  How to Work With Arrays in VBScript or UFT?

Leave a Comment

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