VBScript Operators


VBScript has three types of operators: Arithmetic, Comparison and Logical. Each operator in VBScript has its own precedence.

Operator Precedence:

When we use several operators in an expression, each operator would be executed in a predetermined order called operator precedence.

Operations within parenthesis would be executed first.

When coming to the precedence of operator categories in an expression, Arithmetic operators would be executed first, Comparison operators would be executed next, and Logical operators would be executed last.

When coming to the precedence of operators in each category, operators belonging to a category would be executed in the same order as explained below in each category.

Arithmetic Operators:

Arithmetic operators would be executed in the same order as explained below, in an expression.

1. Exponentiation (^): It is used raise the value to the power of an exponent.

Ex: a = 2^3            Output: 8

2. Multiplication (*): It is used to multiply two numbers.

Ex: a = 2*3            Output: 6

3. Division (/): It is used to divide two numbers. It returns floating point result (Quotient).

Ex: a = 3/2             Output: 1.5

4. Integer Division (\): It is also used to divide two numbers. But returns integer result (Quotient).

Ex: a = 3\2             Output: 1

5. MOD: It is also used to divide two numbers. But returns the remainder value.

Ex: a = 3 MOD 2      Output: 1

6. Addition (+): It is used to add two numbers.

Ex: a = 3+2            Output: 5

7. Subtraction (-): It is used to subtract a number from another number. It is also used for indicating negative values.

Ex: a = 3-2             Output: 1 

8. Concatenation (&): It is used to concatenate two expressions.

Ex:

1) a = 3, b = 2, c = a&b               Output: 32

2) x = "ab", y = "cd", z = x&y       Output: abcd

Note: If both expressions are strings then Addition operator can also act as a Concatenation operator.

i.e., x = "ab", y = "cd", z = x+y -> would also generate the output "abcd"

So, VBScript has two operators for string Concatenation.

Comparison Operators:

All Comparison operators have equal precedence. They would be executed from left to right in an expression.

Comparison operators return boolean values (True / False).

1. Equal To (=): It is used to compare two expressions.

Ex: a = x=y            Output: True / False

2. Not Equal To (<>): It is also used to compare two expressions.

Ex: a = x<>y                   Output: True / False

3. Less Than (<): It is also used to compare two expressions.

Ex: a = x<y            Output: True / False

4. Greater Than (>): It is also used to compare two expressions.

Ex: a = x>y            Output: True / False

5. Less Than Or Equal To (<=): It is also used to compare two expressions.

Ex: a = x<=y                   Output: True / False

6. Greater Than Or Equal To (>=): It is also used to compare two expressions.

Ex: a = x>=y                   Output: True / False

7. Is (Object Equivalence): It has different comparison functionality that differs from all other comparison operators. Is operator compares two object reference variables (We'll go through the object related concepts in next posts).

Ex: Result = Object1 Is Object2

If both Object1 and Object2 refer to the same object then result is True. If they do not then result is False.

This operator does not compare two objects and also does not compare two values. It simply verifies whether the two object references refer to the same object or not.

Logical Operators:

Logical operators would be executed in the same order as explained below, in an expression.

1. NOT: It is used to perform a logical negation on an expression

Ex
Dim a
a = 5
b = Not(a>0)

Output: False

If expression is True then Ouput is False and vice versa. If expression is Null then Output is also Null.

2. AND: It is used to perform logical conjunction

Ex:
Dim a,b
a = 5
b = 6
c = (a>0) AND (b>0)

Output: True

3. OR: It is used to perform logical disjunction

Ex:
Dim a,b
a = 5
b = 6
c = (a>0) OR (b>0)

Output: True

4. XOR: It is used to perform logical exclusion

Ex:
Dim a,b
a = 5
b = 6
c = (a>0) XOR (b>0)

Output: False

5. EQV: It is used to perform logical equivalence

Ex:
Dim a,b
a = 5
b = 6
c = (a>0) EQV (b>0)

Output: True

6. IMP: It is used to perform logical implication

Ex:
Dim a,b
a = 5
b = 6
c = (a>0) IMP (b>0)

Output: True

No comments:

Post a Comment