Wednesday, 16 September 2015

Operators in VBscripting

We have used these operators many times knowingly or unknowingly.It was when we added the marks in English and Mathematics and also when we concatenated strings nothing but linked too strings in the code sample #2. An Operator is nothing but it works either on values or variables to perform some task. Operators are very important in programming because you cannot assign values to variables or perform tasks without these operators.

Suppose, you want to calculate the sum of two variables a and b and save the result in another variable c.Then we have to assign variables a, b and c.
c = a + b
Here, a, b and c are operands and + and = are the operators. VB scripting also consists of operators. There are different types of operators in VB Script that you would be using frequently while working with QTP scripts. Operators in VB Script can be divided into four different types. These are -
  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Concatenation Operator

Arithmetic Operators: 

These operators are used to perform mathematical operations. Arithmetic operators are nothing but addition , subtraction , multiplication, division, exponent, modulus. For the better understanding we can follow the below table.

Operator
What it Does
Example
+
Addition of numbers
3+5 = 8
-
Subtraction of numbers
7-5 = 2
/
Finds the quotient when a number is divided by another
5/3 = 1, 9/3 = 3, 15/2 = 7
*
Multiplication of numbers
2*5*3 = 30
^
Exponent. Computes the power.
2^3 = 8, 3^2 = 9
Mod
Finds the remainder when a number is divided by another.

5 mod 3 = 2, 9 mod 5 = 4, 10 mod 5 = 0

Comparison Operators:
Comparison operators are used to compare different numbers/strings and are mostly used in conditional statements such as If condition.Suppose, you have two variables a and b with values 5 and 9 respectively, then the results for the following comparison will be like in the below table 

Operator
What it Does
Example
=
Checks if a number/string is equal to another
5=5 is True, “abc”=”def” is False
Checks if a number is greater than the other
10 > 5 is True
Checks if a number is less than the other
10 < 5 is False
>=
Checks for greater than or equal to
5 >= 5 is True
<=
Checks for less than or equal to
10 <= 10 is True
<> 
Checks if a number or string is not equal to  another
10 <> 5 is True, “abc” <> “def” is True

Logical Operators: 
Logical operators help you get some result based on certain set of conditions. For example, if ‘today is holiday’ and ‘its not raining’ then ‘i will play outside’Some of the logical operators are AND, OR, NOT and XOR. Suppose, you have two variables x and y with values true and false respectively

Operator
What it Does
Example
And
All conditions must be true for the output to hold.
If var > 99 and var < 1000 then var is a three digit number.
Or
Output holds if any of the conditions is true.
If num = 50 or num = 100 then num is divisible by 10.
Not
Inverts the result. If value is true, Not converts it to false. If value is false, Not converts it to true.
tr = True 
fl = False 
msgbox NOT(tr) ’will display False 
msgbox NOT(fl) ‘will display True

Concatenation Operators:
Concatenation operator is used to concatenate or join strings. Concatenation is nothing but linking two things or objects. So the name itself suggests that this operator is used to link two variables or strings or values.There is only one concatenation operator – & (ampersand)

Operator
What it Does
Example
&
Used to concatenate two or more strings.
“auto” & “mat” & “ion” -> “automation”

So this is all about the operators in Vbscripting.






















No comments:

Post a Comment