VB Script Operators

VBScript Operators


Operators are used for performing mathematical, comparison and logical operations.
VB Script has a full range of operators, including arithmetic operators, comparison operators, concatenation operators, and logical operators.

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.

1)  Arithmetic Operators:

Operator Description
1) Exponentiation Operator (^) Raises a number to the power of an exponent

2) Multiplication Operator (*) Multiplies two numbers.

3) Division Operator (/) Divides two numbers and returns a floating-point 
result.

4) Integer Division Operator (\) Divides two numbers and returns an integer result.

5) Mod Operator Divides two numbers and returns only the remainder.

6) Addition Operator (+) Sums two numbers.

7) Subtraction Operator (-) Finds the difference between two numbers or indicates the negative value of a numeric expression.

8) Concatenation Operator (&) Forces string concatenation of two expressions.

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

c=a\b
msgbox c '3

c=a mod b
msgbox c '1

c=a-b
msgbox c '7

Dim a,b,c
a=10
b=2
c=3
d=c*a^b
'c=a+b
msgbox d '1000

Addition (+) operator

Dim a,b,c
a=10
b=2
c=a+b
msgbox c '12  (if both are numeric, then it adds)

a="10"
b=2
c=a+b
msgbox c '12  (one is string another numeric, then it adds)

a="10"
b="2"
c=a+b
msgbox c '102 (if both are strings, then it concatenates)

a="hydera"
b="bad"
c=a+b
msgbox c 'hyderabad

a="gagan"
b=2
c=a+b
msgbox c 'error

Concatenation Operator

Dim a,b,c
a=10
b=2
c=a&b
msgbox c '102

a="10"
b=2
c=a&b
msgbox c '102

a="10"
b="2"
c=a&b
msgbox c '102

a="hydera"
b="bad"
c=a&b
msgbox c '102

2) Comparison Operators:

Used to compare expressions.

Operator Description
1) = (Equal to) Used to compare expressions.
2) <> (Not equal to) Used to compare expressions.
3) < Less than 4) > Grater than
5) <= Less than or equal to 6) >= Greater than or equal to
7) Is Object equivalence

Example:

Dim x,y,z
x=10
y=20
z=x=y
Msgbox z 'False

x=10
y=20
z=x>y
Msgbox z 'False

x=10
y=20
z=x>=y
Msgbox z 'False

x=10
y=20
z=x<>y
Msgbox z 'True

x=10
y=20
z=x<y
Msgbox z 'True

x=10
y=20
z=x<=y
Msgbox z 'True

3) Concatenation Operators

Operator Description
1) Addition Operator (+)

Sums two numbers
If Then
1) Both expressions are numeric Add.
2) Both expressions are strings Concatenate.
3) One expression is numeric and the other is a string Add.

2) Concatenation Operator (&) Forces string concatenation of two expressions.


4)  Logical Operators:

Operator Description Syntax
1) Not Performs logical negation on an expression result= Not expression
2) And Performs a logical conjunction on two expressions. result= expression1 And expression2
3) Or Performs a logical disjunction on two expressions. result= expression1 Or expression2
4) Xor Performs a logical exclusion on two expressions. result= expression1 Xor expression2
5) Eqv Performs a logical equivalence on two expressions. result= expression1 Eqv expression2

6) Imp Performs a logical implication on two expressions. result= expression1 Imp expression2

Example:

0 comments:

Post a Comment