Operators
Operators
Operators
An operator is a symbol that tells the compiler to
perform specific mathematical or logical
manipulations.
VB.Net is rich in built-in operators and provides
following types of commonly used operators:
▪ Arithmetic Operators
▪ Comparison Operators
▪ Logical/Bitwise Operators
▪ Assignment Operators
Arithmetic Operators
Operator Description Example
^ Raises one operand to the power of another B^A
+ Adds two operands A+B
- Subtracts second operand from the first A–B
* Multiplies both operands A*B
/ Divides one operand by another and returns B/A
a floating point result
\ Divides one operand by another and returns B\A
an integer result
MOD Modulus Operator and remainder of after an B MOD A
integer division
Comparison Operators
Operator Description Example
= Checks if the values of two operands are (A = B)
equal or not; if yes, then condition becomes
true.
<> Checks if the values of two operands are (A <> B)
equal or not; if values are not equal, then
condition becomes true.
> Checks if the value of left operand is greater (A > B)
than the value of right operand; if yes, then
condition becomes true.
Comparison Operators
Operator Description Example
< Checks if the value of left operand is less (A < B)
than the value of right operand; if yes,
then condition becomes true.
>= Checks if the value of left operand is (A >= B)
greater than or equal to the value of right
operand; if yes, then condition becomes
true.
<= Checks if the value of left operand is less (A <= B)
than or equal to the value of right operand;
if yes, then condition becomes true.
Comparison Operators
VB.Net provides three more comparison operators,
which we will be using in forthcoming chapters;
however, we give a brief description here.
▪ Is Operator - It compares two object reference variables and
determines if two object references refer to the same object
without performing value comparisons. If object1 and object2
both refer to the exact same object instance, result is True;
otherwise, result is False.
▪ IsNot Operator - It also compares two object reference
variables and determines if two object references refer to
different objects. If object1 and object2 both refer to the exact
same object instance, result is False; otherwise, result is True.
▪ Like Operator - It compares a string against a pattern.
Logical/Bitwise Operators
Operator Description Example
And It is the logical as well as bitwise AND A>B And B>C
operator. If both the operands are true,
then condition becomes true. This
operator does not perform short-
circuiting, i.e., it evaluates both the
expressions.
Or It is the logical as well as bitwise OR A>B Or B>C
operator. If any of the two operands is
true, then condition becomes true. This
operator does not perform short-
circuiting, i.e., it evaluates both the
expressions.
Logical/Bitwise Operators
Operator Description Example
Not It is the logical as well as bitwise NOT Not(A>B And
operator. Use to reverses the logical state B>C)
of its operand. If a condition is true, then
Logical NOT operator will make false.
Xor It is the logical as well as bitwise Logical A Xor B
Exclusive OR operator. It returns True if
both expressions are True or both
expressions are False; otherwise it returns
False. This operator does not perform
short-circuiting, it always evaluates both
expressions and there is no short-
circuiting counterpart of this operator.
Logical/Bitwise Operators
Operator Description Example
AndAlso It is the logical AND operator. It works (A AndAlso B)
only on Boolean data. It performs short- is False.
circuiting.
OrElse It is the logical OR operator. It works only (A OrElse B) is
on Boolean data. It performs short- True.
circuiting.
IsFalse It determines whether an expression is
False.
IsTrue It determines whether an expression is
True.
Truth Table for Logical Operators
Operator Expression1 Expression2 Result
And True True True
True False False
False True False
False False False
Or True True True
True False True
False True True
False False False
Truth Table for Logical Operators
Operator Expression1 Expression2 Result
Not True True False
True False False
False True False
False False True
Xor True True True
True False False
False True False
False False True
Assignment Operators
Operator Description Example
= Simple assignment operator, Assigns C = A + B will
values from right side operands to left assign value of A
side operand + B into C
+= Add AND assignment operator, It adds C += A is
right operand to the left operand and equivalent to C =
assigns the result to left operand C+A
-= Subtract AND assignment operator, It C -= A is
subtracts right operand from the left equivalent to C =
operand and assigns the result to left C-A
operand
Assignment Operators
Operator Description Example
*= Multiply AND assignment operator, It C *= A is
multiplies right operand with the left equivalent to C =
operand and assigns the result to left C*A
operand
/= Divide AND assignment operator, It C /= A is
divides left operand with the right equivalent to C =
operand and assigns the result to left C/A
operand (floating point division)
\= Divide AND assignment operator, It C \= A is
divides left operand with the right equivalent to C =
operand and assigns the result to left C \A
operand (Integer division)
Assignment Operators
Operator Description Example
^= Exponentiation and assignment operator. C^=A is
It raises the left operand to the power of equivalent to C =
the right operand and assigns the result C^A
to left operand.
<<= Left shift AND assignment operator C <<= 2 is same
as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same
as C = C >> 2
&= Concatenates a String expression to a Str1 &= Str2 is
String variable or property and assigns same as
the result to the variable or property. Str1 = Str1 & Str2
Hands-on Exercise
Write a VB program that will input name of employee, annual gross income,
civil status and number of qualified dependents. Your program should be able
to compute for the gross taxable income, determine personal and additional
deductions, net taxable income and the net income tax. The tax rate shall be
based on the table provided to you (as demonstrated).