Operators in VB.
NET
[Link]
Introduction
Operator is defined as a symbol that operates the variables
and constants to produce a required result.
[Link]
[Link] Operators
Arithmetic operators
Comparison operators
Logical operators
Concatenation operators
[Link]
Arithmetic operators
Arithmetic operators are used to perform arithmetic
calculations such as addition and subtraction.
[Link] supported arithmetic are listed in the given table.
[Link]
Example:
Operator Purpose Output
Let a=10
+ Addition b=a+5 55
- Subtraction c=a–5 45
* Multiplication d = a * 10 500
/ Division e=a/6 8.33
Division (integer
\ part only given as f=a\6 8
output)
Modulas
MOD (Remainder after g = a mod 7 1
division)
^ Power h = a^2 2500
= Assignment m = 100
[Link]
Comparison operators
Comparison operators are used to compare two or more
values.
[Link] supported comparison operators are listed in the
following table.
[Link]
Example:
Operator Purpose Output
Let a=50
Check if a value is Dim b As Boolean
= FALSE
equal to another b=(a=55)
Check if a value is
<> b=(a<>55) TRUE
not equal to another
Check if a value is
> b=(a>100) FALSE
greater than another
Check if a value is
< b=(a<100) TRUE
less than another
Check if a value is
>= greater than or b=(a>=50) TRUE
equal to another
Check if a value is
<= less than or equal to b=(a<=75) TRUE
another
Check if a string Dim P As string
LIKE matches a given P=“Good” FALSE
pattern b=(“God” LIKE P)
Check if two object
IS references refer to a
[Link] same object.
Logical operators
Logical operators are used to perform logical operations such
as AND, OR, NOT on one or more expressions.
[Link]
AND
This operator is used as a conjunction of two conditions. The
result of the AND operation is True only if both the conditions
are true.
Syntax:
Result = Expression1 AND Expression2
[Link]
Result of AND operation table
Expression 1 Expression 2 Result
TRUE TRUE TRUE
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE FALSE
[Link]
Example of AND Operator
Dim a, b, c As Integer
Dim d As Boolean
a = 10
b = 20
c = 30
d = (a<b) AND (b<c) „Returns True‟
d = (a>b) AND (b<c) „Returns False‟
[Link]
OR
This operator is used as a disjunction of two conditions. The
result of the OR operator is false only if both the conditions are
false.
Syntax:
Result = Expression1 OR Expression2
[Link]
Result of OR operation table
Expression 1 Expression 2 Result
TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE
[Link]
Example of OR Operator
Dim a, b, c As Integer
Dim d As Boolean
a = 10
b = 20
c = 30
d = (a<b) OR (b>c) „Returns True‟
d = (a>b) OR (b>c) „Returns False‟
[Link]
XOR
The result this operator is True only if one of the conditions is
true and other is false.
Syntax:
Result = Expression1 XOR Expression2
[Link]
Result of XOR operation table
Expression 1 Expression 2 Result
TRUE TRUE FALSE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE
[Link]
Example of XOR Operator
Dim a, b, c As Integer
Dim d As Boolean
a = 10
b = 20
c = 30
d = (a<b) XOR (b<c) „Returns False‟
d = (a>b) XOR (b<c) „Returns True‟
[Link]
NOT
This logical operator is used to perform a logical negation on
a boolean expression.
Syntax:
Result = NOT (boolean Expression)
[Link]
Result of NOT operation table
Boolean Expression Result
TRUE FALSE
FALSE TRUE
[Link]
Example of XOR Operator
Dim a, b, c As Integer
Dim d As Boolean
a = 10
b = 20
c = NOT (a>b) „Returns True‟
c = NOT (a<b) „Returns False‟
[Link]
AndAlso
This operator is same as AND operator but if the first condition
is false then the second condition is not checked and the
result is false.
Syntax:
Result = Expression 1 AndAlso Expression2
[Link]
OrElse
This operator is same as OR operator but if the first condition
is true then the second condition is not checked and the result
is True.
Syntax:
Result = Expression 1 OrElse Expression2
[Link]
Concatenation operators
Concatenation operators are used to join two string values.
& and + are the two concatenation operators available in
[Link].
The operator & is used to concatenate two strings and + is
used to add two numbers and concatenate two strings.
[Link]
Example of concatenation operators
MsgBox(“Good” & “Night”) „Returns GoodNight‟
MsgBox(“Good” + “Night”) „Returns GoodNight‟
MsgBox(“22” & “33”) „Returns 2233‟
MsgBox(“22” + “33”) „Returns 2233‟
MsgBox(22 + 33) „Returns 55‟
[Link]
The End
….. Thank You ……
[Link]