Arithmetic Operators: The Following Is A Table of Arithmetic Operators in Visual Basic
Arithmetic Operators: The Following Is A Table of Arithmetic Operators in Visual Basic
Operators in programming languages are taken from mathematics. Programmers work with data. The operators are used to process data.
Arithmetic operators
The following is a table of arithmetic operators in Visual Basic.
Symbol Name
+ * / \ Mod ^
In the preceding example, we use addition, subtraction, multiplication and division operations. This is all familiar from the mathematics.
$ ./arithmetic.exe 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 Console.WriteLine(c) c = 5 \ 2 Console.WriteLine(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
This is integer division. The result of this operation is always and integer. The c variable has value 2.
$ ./division.exe 2.5 2
The last two operators that we will mention are modulo operator and exponentiation operator.
Console.WriteLine(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.
Console.WriteLine(9 ^ 2) ' Prints 81
9 ^ 2 = 9 * 9 = 81
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 compares references
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() Console.WriteLine("six" = "six") ' Prints True ' Console.WriteLine("a" > 6) this would throw
' an exception Console.WriteLine("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.
Console.WriteLine("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() Console.WriteLine("a" < "b") Console.WriteLine("a is: {0}", Console.WriteLine("b is: {0}", End Sub End Module Asc("a")) Asc("b"))
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.
$ ./compare.exe True a is: 97 b is: 98
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 Console.WriteLine(o1 Is o2) Console.WriteLine(o3 Is o2) End Sub End Module
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 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.
If TextBox1.Text = "Hello World!" Then 'Checks if "Hello World!" is written in the textbox. ListBox1.Items.Add("Hello") 'If "Hello World!" is written then it enters "Hello" into the listbox. End If 'Ends the If statement.
You can also use an Else statement after the Then statement. The Else statement tells it what to do if the statement is false. Example: If TextBox1.Text = "Hello World!" Then 'Checks if "Hello World!" is written in the textbox. ListBox1.Items.Add("Hello") 'If "Hello World!" is written then it enters "Hello" into the listbox. Else : ListBox1.Items.Add("How are you?") 'If "Hello World" is not written then it adds "How are you?" to the list box instead. End If 'Ends the If statement.
You can also use the ElseIf statement to string multiple If statements together. ElseIf statements are very
handy when you are using many If statements as it preserves space and is much easier to read.
If/Then/Else statements with ElseIf: If TextBox1.Text.Length = 0 Then TextBox1.Text = "01" ElseIf TextBox2.Text.Length = 0 Then TextBox2.Text = "02" ElseIf TextBox3.Text.Length = 0 Then TextBox3.Text = "03" ElseIf TextBox4.Text.Length = 0 Then TextBox4.Text = "04" ElseIf TextBox5.Text.Length = 0 Then TextBox5.Text = "05" ElseIf TextBox6.Text.Length = 0 Then TextBox6.Text = "06" ElseIf TextBox7.Text.Length = 0 Then TextBox7.Text = "07" End If
If/Then/Else statements without ElseIf: If TextBox1.Text.Length = 0 Then TextBox1.Text = "01" Else If TextBox2.Text.Length = 0 Then TextBox2.Text = "02" Else If TextBox3.Text.Length = 0 Then TextBox3.Text = "03" Else If TextBox4.Text.Length = 0 Then TextBox3.Text = "04" Else If TextBox5.Text.Length = 0 Then
TextBox3.Text = "05" Else If TextBox6.Text.Length = 0 Then TextBox3.Text = "06" Else If TextBox7.Text.Length = 0 Then TextBox2.Text = "07" End If End If End If End If End If End If End If