An
operator is a special symbol which indicates a certain process is carried out.
Operators in programming languages are taken from mathematics. Programmers work with
data. The operators are used to process data.
We have several types of operators:
- Arithmetic operators
- Boolean operators
- Relational operators
- Bitwise operators
An operator may have one or two operands. An
operand is one of the inputs (arguments) of an operator.
Those operators that work with only one operand are called
unary operators.
Those who work with two operands are called
binary operators.
Option Strict On
Module Example
Sub Main()
Print(2)
Print(-2)
Print(2+2)
Print(2-2)
End Sub
End Module
+ and - signs can be addition and subtraction operators as well as unary sign operators.
It depends on the situation.
Option Strict On
Module Example
Dim a As Byte
Sub Main()
a = 1
Print(-a) ' Prints -1
Print(-(-a)) ' Prints 1
End Sub
End Module
The plus sign can be used to indicate that we have a positive number.
But it is mostly not used. The minus sign changes the sign of a value.
Option Strict On
Module Example
Dim a As Byte
Dim b As Byte
Sub Main()
a = 3 * 3
b = 2 + 2
Print(a) ' Prints 9
Print(b) ' Print 4
End Sub
End Module
Multiplication and addition operators are examples of binary operators. They are used with two operands.
The assignment operator
The assignment operator
= assigns a value to a variable. A
variable is a placeholder for a value.
In mathematics, the
= operator has a different meaning. In an equation,
the
= operator is an equality operator. The left side of the equation is equal to the right one.
x = 1
Print(x) ' Prints 1
Here we assign a number to the x variable.
x = x + 1
Print(x)
The previous expression does not make sense in mathematics. But it is legal in programming.
The expression adds 1 to the x variable. The right side is equal to 2 and 2 is assigned to x.
3 = x
This code example results in syntax error. We cannot assign a value to a literal.
Arithmetic operators
The following is a table of arithmetic operators in Visual Basic.
Symbol | Name |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
\ | Integer Division |
Mod | Modulo |
^ | Exponentiation |
The following example shows arithmetic operations.
Option Strict On
Module Example
Dim a As Byte
Dim b As Byte
Dim c As Byte
Dim add As Byte
Dim sb As Byte
Dim mult As Byte
Dim div As Byte
Sub Main()
a = 10
b = 11
c = 12
add = a + b + c
sb = c - a
mult = a * b
div = CType(c / 3, Byte)
Print(add)
Print(sb)
Print(mult)
Print(div)
End Sub
End Module
In the preceding example, we use addition, subtraction, multiplication and
division operations. This is all familiar from the mathematics.
33
2
110
4
Output of the example.
Next we will show the distinction between normal and integer division.
Option Strict On
Module Example
Dim a As Single = 5
Dim b As Single = 2
Dim c As Single
Sub Main()
c = 5 / 2
Print(c)
c = 5 \ 2
Print(c)
End Sub
End Module
In the preceding example, we divide two numbers using normal
and integer division operator. Visual Basic has two distinct operators
for division.
Dim a As Single = 5
We use floating point data types.
c = 5 / 2
Print(c)
This is the 'normal' division operation. It returns 2.5, as expected.
c = 5 \ 2
Print(c)
This is integer division. The result of this operation is always and
integer. The c variable has value 2.
2.5
2
Result of the division
The last two operators that we will mention are modulo
operator and exponentiation operator.
Print(9 Mod 4) ' Prints 1
The
Mod
operator is called the modulo operator.
It finds the remainder of division of one number by another.
9 Mod 4
, 9 modulo 4 is 1, because 4 goes into 9
twice with a remainder of 1. Modulo operator can be handy for example
when we want to check for prime numbers.
Finally, we will mention
exponentiation operator.
Print(9 ^ 2) ' Prints 81
9 ^ 2 = 9 * 9 = 81
Concatenating strings
In Visual Basic we have two operators for string concatenation.
The plus + operator and the & ampersand operator.
Option Strict On
Module Example
Sub Main()
Print("Return " & "of " & "the king")
Print("Return " + "of " + "the king")
End Sub
End Module
We join three strings together using both operators.
Return of the king
Return of the king
And this is, what we get. Same result for both cases.
Boolean operators
In Visual Basic, we have the following logical operators.
Boolean operators are also called logical.
Symbol | Name |
And | logical conjunction |
AndAlso | short circuit And |
Or | logical inclusion |
OrElse | short circuit Or |
Xor | logical inclusion |
Not | negation |
Boolean operators are used to work with truth values.
Option Strict On
Module Example
Dim x As Byte = 3
Dim y As Byte = 8
Sub Main()
Print(x = y)
Print(y > x)
If (y > x)
Print("y is greater than x")
End If
End Sub
End Module
Many expressions result in a boolean value. Boolean values are used
in conditional statements.
Print(x = y)
Print(y > x)
Relational operators always result in a Boolean value. These two lines
print False and True.
If (y > x)
Print("y is greater than x")
End If
The body of the
If
statement
is executed only if the condition inside the parentheses
is met. The x > y returns True, so the message "y is greater than x"
is printed to the terminal.
Option Strict On
Module Example
Dim a As Boolean
Dim b As Boolean
Dim c As Boolean
Dim d As Boolean
Sub Main()
a = (True And True)
b = (True And False)
c = (False And True)
d = (False And False)
Print(a)
Print(b)
Print(c)
Print(d)
End Sub
End Module
Example shows the logical
And
operator.
It evaluates to True only if both operands are True.
True
False
False
False
The logical
Xor
operator evaluates to True,
if exactly one of the operands is True.
Option Strict On
Module Example
Dim a As Boolean
Dim b As Boolean
Dim c As Boolean
Dim d As Boolean
Sub Main
a = (True Xor True)
b = (True Xor False)
c = (False Xor True)
d = (False Xor False)
Print(a)
Print(b)
Print(c)
Print(d)
End Sub
End Module
The logical
Xor
evaluates to False, if
both operands are True or both False.
False
True
True
False
The logical
Or
operator evaluates to True,
if either of the operands is True.
Option Strict On
Module Example
Sub Main()
Dim a As Boolean = True Or True
Dim b As Boolean = True Or False
Dim c As Boolean = False Or True
Dim d As Boolean = False Or False
Print(a)
Print(b)
Print(c)
Print(d)
End Sub
End Module
If one of the sides of the operator is True, the outcome of the operation is True.
True
True
True
False
The
negation operator
Not
makes True False and False True.
Option Strict On
Module Example
Sub Main(
)
Print(Not True)
Print(Not False)
Print(Not (4 < 3))
End Sub
End Module
The example shows the negation operator in action.
False
True
True
Relational Operators
Relational operators are used to compare values. These operators always result in a boolean value.
Symbol | Meaning |
< | less than |
<= | less than or equal to |
> | greater than |
>= | greater than or equal to |
== | equal to |
<> | not equal to |
Is | compares references |
Relational operators are also called comparison operators.
Print(3 < 4) ' Prints True
Print(3 = 4) ' Prints False
Print(4 >= 3) ' Prints True
As we already mentioned, the relational operators return boolean values.
Note that in Visual Basic, the comparison operator is (=). Not (==) like in
C and C influenced languages.
Notice that the relational operators are not limited to numbers.
We can use them for other objects as well. Although they might not
always be meaningful.
Option Strict On
Module Example
Sub Main()
Print("six" = "six") ' Prints True
' Print("a" > 6) 'this would throw
' an exception
Print("a" < "b") ' Prints True
End Sub
End Module
We can compare string objects too. Comparison operators in a string
context compare the sorting order of the characters.
Print("a" < "b") ' Prints True
What exactly happens here? Computers do not know characters or strings. For them, everything is just a number.
Characters are special numbers stored in specific tables. Like ASCII.
Option Strict On
Module Example
Sub Main()
Print("a" < "b")
Print("a is: {0}", Asc("a"))
Print("b is: {0}", Asc("b"))
End Sub
End Module
Internally, the a and b characters are numbers. So when we compare two characters,
we compare their stored numbers. The built-in
Asc
function
returns the ASCII value of a single character.
True
a is: 97
b is: 98
In fact, we compare two numbers. 97 with 98.
Print("ab" > "aa") ' Prints True
Say we have a string with more characters. If the first characters are equal,
we compare the next ones. In our case, the b character at the second position
has a greater value than the a character. That is why "ab" string is greater
than "aa" string. Comparing strings in such a way does not make much sense, of course.
But it is technically possible.
Finally, we will mention the
Is
operator.
The operator checks if two object references refer to the same object.
It does not perform value comparisons.
Option Strict On
Module Example
Sub Main()
Dim o1 As Object = New Object
Dim o2 As Object = New Object
Dim o3 As Object
o3 = o2
Print(o1 Is o2)
Print(o3 Is o2)
End Sub
End Module
We create three objects and compare them with the
Is
operator.
Dim o1 As Object = New Object
Dim o2 As Object = New Object
We declare and initialize two Object instances. The Object class is a base class
for all classes in the .NET framework. We will describe it later in more detail.
Dim o3 As Object
The third variable is only declared.
o3 = o2
The o3 now refers to the o2 object. They are two references to the
same object.
Console.WriteLine(o1 Is o2)
Console.WriteLine(o3 Is o2)
In the first case, we get False. o1 and o2 are two different object.
In the second case, we get True. o3 and o2 refer to the same object.
Bitwise operators
Decimal numbers are natural to humans. Binary numbers are native to computers.
Binary, octal, decimal or hexadecimal symbols are only notations of the same number.
Bitwise operators work with bits of a binary number. Bitwise operators are seldom
used in higher level languages like Visual Basic.
Symbol | Meaning |
Not | bitwise negation |
Xor | bitwise exclusive or |
And | bitwise and |
Or | bitwise or |
The
bitwise negation operator changes each 1 to 0 and 0 to 1.
Print(Not 7) ' Prints -8
Print(Not -8) ' Prints 7
The operator reverts all bits of a number 7. One of the bits also determines, whether the number is negative or not.
If we negate all the bits one more time, we get number 7 again.
The
bitwise and operator performs bit-by-bit comparison between two numbers. The result for a bit
position is 1 only if both corresponding bits in the operands are 1.
00110
And 00011
= 00010
The first number is a binary notation of 6. The second is 3. The result is 2.
Print(6 And 3) ' Prints 2
Print(3 And 6) ' Prints 2
The
bitwise or operator performs bit-by-bit comparison between two numbers.
The result for a bit position is 1 if either of the corresponding bits in the operands is 1.
00110
Or 00011
= 00111
The result is
00110
or decimal 7.
Print(6 Or 3) ' Prints 7
Print(3 Or 6) ' Prints 7
The
bitwise exclusive or operator performs bit-by-bit comparison between two numbers.
The result for a bit position is 1 if one or the other (but not both) of the corresponding bits in the operands is 1.
00110
Xor 00011
= 00101
The result is
00101
or decimal 5.
v(6 Xor 3) ' Prints 5
Compound assignment operators
The compound assignment operators consist of two operators. They are shorthand operators.
Option Strict On
Module Example
Dim a As Integer
Sub Main
a = 1
a = a + 1
Print(a) ' Prints 2
a += 1
Print(a) ' Prints 3
End Sub
End Module
The += compound operator is one of these shorthand operators.
They are less readable than the full expressions but
experienced programmers often use them.
Other compound operators are:
-= *= \= /= &= ^=