You are on page 1of 7

Microsoft Visual Basic .

Net
Operators Listed by Functionality
S.# Operators Description
1 Arithmetic These operators perform mathematical calculations.
2 Assignment These operators perform assignment operations.
3 Comparison These operators perform comparisons.
4 Concatenation These operators combine strings.
5 Logical/Bitwise These operators perform logical operations.
6 Bit Shift These operators perform arithmetic shifts on bit patterns.

1. Arithmetic Operators
The following are the arithmetic operators defined in Visual Basic .NET.
S.# Operators Description
1 ^ (Number ^ exponent) Raises a number to the power of another number.
2 * (Number1 * Number2) Multiplies two numbers.
3 / (Number1 / Number2) Divides two numbers and returns quotient in floating-point result.
4 \ (Number1 \ Number2) Divides two numbers and returns quotient in integer result.
5 Mod (Number1 Mod Number2) Divides two numbers and returns only the remainder.
6 + (expression1 + expression2) Adds two numbers. Also used to concatenate two strings.
7 - (expression1 - expression2) subtraction operator for the difference between two numbers.
Examples Of (Arithmetic Operators)
Module Module1
Sub Main()
Console.WriteLine(2 ^ 2) ‘4
Console.WriteLine(3 ^ 3 ^ 3) ‘19683
Console.WriteLine((-5) ^ 2) ‘25 (According Mathematic rule Even for +)
Console.WriteLine((-5) ^ 9) ‘-1953125 (According Mathematic rule odd for -)
Console.WriteLine()
Console.WriteLine(10 * 4) ‘40
Console.WriteLine()
Console.WriteLine(10 / 4) ‘2.5
Console.WriteLine(10 \ 4) ‘2
Console.WriteLine()
Console.WriteLine(10 Mod 5) ‘0
Console.WriteLine(10 Mod 3) ‘1
Console.WriteLine(12 Mod 4.3) ‘3.4
Console.WriteLine()
Console.WriteLine(10 + 4) ‘14
Console.WriteLine(10 - 4) ‘6
Console.ReadLine()
End Sub
End Module

1 of 7
Microsoft Visual Basic .Net
2. Assignment Operators
The following are the Assignment operators defined in Visual Basic .NET.
S.# Operators Description
1 = (var=value) Used to assign a value to a variable or property.
2 ^=(var ^= expression) Raises the value of a variable to the power of an expression and assigns the
result back to the variable.
3 *=(var *= expression) Multiplies the value of a variable by the value of an expression and assigns
the result to the variable.
4 /=(var /= expression) Divides the value of a variable by the value of an expression and assigns the
floating-point result to the variable.
5 \=(var \= expression) Divides the value of a variable by the value of an expression and assigns the
integer result to the variable.
6 +=(var += expression) Adds the value of an expression to the value of a variable and assigns the
result to the variable. Also concatenates a String expression to a String
variable and assigns the result to the variable.
7 -=(var -= expression) Subtracts the value of an expression from the value of a variable and assigns
the result to the variable.
8 <<=(var >>=amount) Performs an arithmetic left shift on the value of a variable and assigns
the result back to the variable.
9 >>=(var >>=amount) Performs an arithmetic right shift on the value of a variable and
assigns the result back to the variable.
10 &= (var &= expression) Concatenates a String expression to a String variable and assigns the result
to the variable.

Examples Of (Assignment Operators)


Module Module1
Sub Main()
Dim var As Integer
Dim var1 As String = "Hello" : Dim var2 As String = "Dear"
var = 5
Console.WriteLine(var) ‘5
var ^= 3
Console.WriteLine(var) ‘125
var *= 3
Console.WriteLine(var) ‘375
var /= 3
Console.WriteLine(var) ‘125
var \= 3
Console.WriteLine(var) ‘41
var += 3
Console.WriteLine(var) ‘44
var -= 3
Console.WriteLine(var) ‘41
var <<= 3
Console.WriteLine(var) ‘328
var >>= 3
Console.WriteLine(var) ‘41
var &= 3
Console.WriteLine(var) ‘413
Console.WriteLine(var1) ‘Hello
Console.WriteLine(var2) ‘Dear
var1 &= var2
Console.WriteLine(var1) ‘HelloDear
Console.ReadLine()
2 of 7
Microsoft Visual Basic .Net
End Sub
End Module
3. Comparison Operators
Compares expressions.
S.# Operator True if False if
1 < (Less than) expression1 < expression2 expression1 >= expression2
2 <= (Less than or equal to) expression1 <= expression2 expression1 > expression2
3 > (Greater than) expression1 > expression2 expression1 <= expression2
4 >= (Greater than or equal to) expression1 >= expression2 expression1 < expression2
5 = (Equal to) expression1 = expression2 expression1 <> expression2
6 <> (Not equal to) expression1 <> expression2 expression1 = expression2
7 Is and Like Have specific comparison functionality that differs from above operators
Examples Of (Comparison Operators)
Module Module1
Sub Main()
Console.WriteLine(30 < 35) ‘T
Console.WriteLine(40 < 35) ‘F
Console.WriteLine(30 <= 30) ‘T
Console.WriteLine(30 <= 25) ‘F
Console.WriteLine(35 > 30) ‘T
Console.WriteLine(35 > 45) ‘F
Console.WriteLine(30 >= 35) ‘F
Console.WriteLine(30 = 30) ‘T
Console.WriteLine(30 = 35) ‘F
Console.WriteLine(30 <> 35) ‘T
Console.ReadLine()
End Sub
End Module
4. Concatenation Operators
S.# Operators Description
1 & (expression1 & expression2) Generates a string concatenation of two expressions.
2 + (expression1 + expression2) Adds two numbers. Also used to concatenate two strings.
Note for + Concatenation Operator
S.# If Then
1 Both expressions are numeric Add.
2 Both expressions are strings Concatenate.
One expression is numeric and the Implicitly cast the numeric expression to Double and add. If
3 other is a string expression cannot be converted to a numeric value, an
InvalidCastException is thrown.
Examples Of (Concatenation Operators)
Module Module1
Sub Main()
Dim intvar1 As Integer = 10
Dim intvar2 As Integer = 20
Dim strvar1 As String = "Hello"
Dim strvar2 As String = "Dear"
Console.WriteLine(intvar1 & intvar2) ‘1020
Console.WriteLine(strvar1 & strvar2) ‘HelloDear
Console.WriteLine(intvar1 & strvar1) ‘10Hello
Console.WriteLine(intvar1 + intvar2) ‘30

3 of 7
Microsoft Visual Basic .Net
Console.WriteLine(strvar1 + strvar2) ‘HelloDear
Console.WriteLine(intvar1 + strvar1) ‘Exception is thrown
Console.ReadLine()
End Sub
End Module

5. Logical/Bitwise Operators
S.# Operators Description
1 And Performs a logical conjunction on two expressions
2 Not Performs logical negation on an expression.
3 Or Used to perform a logical disjunction on two expressions
4 Xor Performs a logical exclusion operation on two expressions
5 AndAlso Performs short-circuiting logical conjunction on two expressions.
6 OrElse Perform short-circuiting logical disjunction on two expressions.
Chart for And , Not , Or , Xor Operators
expression1 expression2 Value of result Value of Value of Value of
S.#
is is And result Or result Not result Xor
1 True True True True False False
2 True False False True False True
3 False True False True True True
4 False False False False True False
Chart for AndAlso Operators
S.# expression1 is expression2 is Value of result AndAlso
1 True (not evaluated) False
2 False True False
3 False False False
Chart for OrElse Operators
S.# expression1 is expression2 is Value of result OrElse
1 True True True
2 False True False
3 False (not evaluated) False
Examples Of (Logical/Bitwise Operators)
Module Module1
Sub Main()
Dim A As Integer = 10
Dim B As Integer = 8
Dim C As Integer = 6
Dim myCheck As Boolean
myCheck = A > B And B > C ' Returns True.
Console.WriteLine(myCheck)
myCheck = B > A And B > C ' Returns False.
Console.WriteLine(myCheck)
myCheck = Not (A > B) ' Returns False.
Console.WriteLine(myCheck)
myCheck = Not (B > A) ' Returns True.
Console.WriteLine(myCheck)
myCheck = A > B Or B > C ' Returns True.
Console.WriteLine(myCheck)
4 of 7
Microsoft Visual Basic .Net
myCheck = B > A Or B > C ' Returns True.
Console.WriteLine(myCheck)
myCheck = B > A Or C > B ' Returns False.
Console.WriteLine(myCheck)
myCheck = A > B Xor B > C ' Returns False.
Console.WriteLine(myCheck)
myCheck = B > A Xor B > C ' Returns True.
Console.WriteLine(myCheck)
myCheck = B > A Xor C > B ' Returns False.
Console.WriteLine(myCheck)
myCheck = A > B AndAlso B > C ' True.
Console.WriteLine(myCheck)
myCheck = B > A AndAlso B > C ' False. Second expression not evaluated.
Console.WriteLine(myCheck)
myCheck = A > B AndAlso C > B ' False. Second expression evaluated.
Console.WriteLine(myCheck)
myCheck = A > B OrElse B > C ' True. Second expression is not evaluated.
Console.WriteLine(myCheck)
myCheck = B > A OrElse B > C ' True. Second expression is evaluated.
Console.WriteLine(myCheck)
myCheck = B > A OrElse C > B ' False.
Console.WriteLine(myCheck)
Console.ReadLine()
End Sub
End Module

6.Bit Shift Operators


S.# Operators Description
1 << (pattern << amount) Performs an arithmetic left shift on a bit pattern.
2 >> (pattern >> amount) Performs an arithmetic right shift on a bit pattern.
Parts
pattern
Required. Integral numeric expression. The bit pattern to be shifted. The data type must be Byte, Short,
Integer, or Long.
amount
Required. Numeric expression. The number of bits to shift the bit pattern. The data type must be Integer
or widen to Integer.
Remarks
Arithmetic shifts are not circular, which means the bits shifted off one end of the result are not reintroduced at the
other end. In an arithmetic left shift, the bits shifted beyond the range of the result data type are discarded, and
the bit positions vacated on the right are set to zero.
To prevent shifting by more bits than the result can hold, Visual Basic masks the value of amount with a size
mask corresponding to the data type of pattern. The binary AND of these values is used for the shift amount. The
size masks are as follows:
S.# Data type of Size mask Size mask
pattern (decimal) (hexadecimal)
1 Byte 7 &H00000007
2 Short 15 &H0000000F
3 Integer 31 &H0000001F
4 Long 63 &H0000003F
If amount is zero, the value of result is identical to the value of pattern. If amount is negative, it is taken as an
unsigned value and masked with the appropriate size mask.
Arithmetic shifts never generate overflow exceptions.
5 of 7
Microsoft Visual Basic .Net
Examples Of ( << Operators)
This example uses the << operator to perform arithmetic left shifts on integral values. The result always has the
same data type as that of the expression being shifted.
Dim Pattern As Short = 192 ' Bit pattern is 0000 0000 1100 0000.
Dim Result1, Result2, Result3, Result4, Result5 As Short
Result1 = Pattern << 0 ' Result is 192 (0000 0000 1100 0000).
Result2 = Pattern << 4 ' Result is 3072 (0000 1100 0000 0000).
Result3 = Pattern << 9 ' Result is -32768 (1000 0000 0000 0000).
Result4 = Pattern << 17 ' Result is 384 (0000 0001 1000 0000).
Result5 = Pattern << -1 ' Result is 0 (shifted 15 places to left).

The shift amount for Result4 is calculated as 17 AND 15, which equals 1.

Examples Of ( >> Operators)


This example uses the >> operator to perform arithmetic right shifts on integral values. The result always has the
same data type as that of the expression being shifted.
Dim Pattern As Short = 2560 ' Bit pattern is 0000 1010 0000 0000.
Dim Result1, Result2, Result3, Result4, Result5 As Short
Result1 = Pattern >> 0 ' Result is 2560 (0000 1010 0000 0000).
Result2 = Pattern >> 4 ' Result is 160 (0000 0000 1010 0000).
Result3 = Pattern >> 10 ' Result is 2 (0000 0000 0000 0010).
Result4 = Pattern >> 18 ' Result is 640 (0000 0010 1000 0000).
Result5 = Pattern >> -1 ' Result is 0 (shifted 15 places to right).
The shift amount for Result4 is calculated as 18 AND 15, which equals 2.
Dim NegPattern As Short = -8192 ' Bit pattern is 1110 0000 0000 0000.
Dim NegResult1, NegResult2 As Short
NegResult1 = NegPattern >> 4 ' Result is -512 (1111 1110 0000 0000).
NegResult2 = NegPattern >> 13 ' Result is -1 (sign bit propagated)..

Operator Precedence in Visual Basic.Net


When several operations occur in an expression, each part is evaluated and resolved in a predetermined order
called operator precedence.
When expressions contain operators from more than one category, they are evaluated according to the following
rules:

• The arithmetic and concatenation operators have the order of precedence described below, and all have
higher precedence than the comparison, logical, and bitwise operators.
• Comparison operators have equal precedence, and all have higher precedence than the logical and bitwise
operators, but lower precedence than the arithmetic and concatenation operators.
• Logical and bitwise operators have the order of precedence described below, and all have lower
precedence than the arithmetic, concatenation, and comparison operators.
• Operators with equal precedence are evaluated left to right in the order in which they appear in the
expression.
Operators are evaluated in the following order of precedence:
Arithmetic and Concatenation Operators
Exponentiation (^)
Unary negation (–)
Multiplication and division (*, /)
Integer division (\)
6 of 7
Microsoft Visual Basic .Net
Modulus arithmetic (Mod)
Addition and subtraction (+, –), string concatenation (+)
String concatenation (&)
Arithmetic bit shift (<<, >>)
Comparison Operators
All comparison operators (=, <>, <, <=, >, >=, Like, Is, TypeOf...Is)
Logical and Bitwise Operators
Negation (Not)
Conjunction (And, AndAlso)
Disjunction (Or, OrElse, Xor)
Note
The string concatenation operator (&) is not an arithmetic operator, but in precedence it is grouped with the
arithmetic operators.

The Is operator is an object reference comparison operator. It does not compare objects or their values; it checks
only to determine if two object variables refer to the same object.

When operators of equal precedence occur together in an expression, for example multiplication and division,
each operation is evaluated as it occurs from left to right. Parentheses can be used to override the order of
precedence and to force some parts of an expression to be evaluated before others. Operations within parentheses
are always performed before those outside. Within parentheses, however, operator precedence is maintained. The
following example illustrates this:

Examples Of (Operator Precedence)


Module Module1
Sub Main()
Dim A, B, C, D, E, F, G As Double
A = 3.0
B = 6.0
C = 4.0
D = 2.0
E = 1.0
F = A + B - C / D * E
Console.WriteLine(F)
' The previous line sets F to 7.0. Because of natural operator
' precedence, it is exactly equivalent to the following line:
F = (A + B) - ((C / D) * E)
Console.WriteLine(F)
' The following line overrides the natural operator precedence:
G = A + (B - C) / (D * E)
Console.WriteLine(G)
' The previous line sets G to 4.0.
Console.ReadLine()
End Sub
End Module

7 of 7