Types of VB.Net Operators: Arithmetic, Comparison & Logical
What is Operator in VB.Net?
An Operator in VB.Net refers to a symbol that instructs the compiler to perform a specific logical or mathematical manipulation. The operator performs the operation on the provided operands. Microsoft VB.Net comes with various types of operators. We will discuss them in this tutorial.
Arithmetic Operators in VB.Net
You can use arithmetic operators to perform various mathematical operations in VB.NET. Arithmetic operators in VB.Net include:
Arithmetic Operator | Description |
---|---|
^ | for raising an operand to the power of another operand |
+ | for adding two operands. |
– | for subtracting the second operand from the first operand. |
* | for multiplying both operands. |
/ | for dividing an operand against another. It returns a floating point result. |
\ | for dividing an operand against another. It returns an integer result. |
MOD | known as the modulus operator. It returns the remainder after division. |
Example of VB.Net Arithmetic Operator
Here is an example of VB.Net arithmetic operator:
Step 1) Create a new console application. To know this, visit our previous tutorial on Data Types and Variables.
Step 2) Add the following code:
Module Module1 Sub Main() Dim var_w As Integer = 11 Dim var_x As Integer = 5 Dim var_q As Integer = 2 Dim var_y As Integer Dim var_z As Single var_y = var_w + var_z Console.WriteLine(" Result of 11 + 5 is {0} ", var_y) var_y = var_w - var_x Console.WriteLine(" Result of 11 - 5 is {0} ", var_y) var_y = var_w * var_x Console.WriteLine(" Result of 11 * 5 is {0} ", var_y) var_z = var_w / var_x Console.WriteLine(" Result of 11 / 5 is {0}", var_z) var_y = var_w \ var_x Console.WriteLine(" Result of 11 \ 5 is {0}", var_y) var_y = var_w Mod var_x Console.WriteLine(" Result of 11 MOD 5 is {0}", var_y) var_y = var_x ^ var_x Console.WriteLine(" Result of 5 ^ 5 is {0}", var_y) Console.ReadLine() End Sub End Module
Step 3) Click the Start button to execute the code. You should get the following window:
Here is a screenshot of the code:
Explanation of Code:
- Creating a module named Module1.
- Creating the main sub-procedure.
- Creating an integer variable var_w with a value of 11.
- Creating an integer variable var_x with a value of 5.
- Creating an integer var_q with a value of 2.
- Creating an integer var_y.
- Creating an integer var_z.
- Adding the values of variables var_w and var_z and assigning the result to variable var_y.
- Printing the above result on the console.
- Subtracting the value of variables var_x from the value of variable var_w and assigning the result to variable var_y.
- Printing the above result on the console.
- Multiplying the values of variables var_w and var_x and assigning the result to variable var_y.
- Printing the above result on the console.
- Dividing the value of variable var_w by the value of variable var_x and assigning the result to variable var_z.
- Printing the above result on the console.
- Dividing the value of variable var_w by the value of variable var_x and assigning the result to variable var_y.
- Printing the above result on the console.
- Getting the remainder after dividing the value of variable var_w by the value of variable var_x and assigning the result to variable var_y.
- Printing the above result on the console.
- Getting the value of variable var_x raised to the power of the same and assigning the result to variable var_y.
- Printing the above result on the console.
- To exit the console when the user presses any key.
- Ending the subprocedure.
- Ending the Module
Comparison Operators in VB.Net
Comparison operators are used for making comparisons between variables. Comparison operators in VB.Net include the following:
Comparison Operators | Description |
---|---|
= | Checks if the two operands have equal values. If they are equal, the condition evaluates to True. |
<> | Checks if the two operands are not equal. If they are not equal, the condition evaluates to True. |
> | Checks if the value of the left operand is greater than the value of the right operand. If true, the condition evaluates to True. |
< | Checks if the value of the left operand is less than the value of the right operand. If true, the condition evaluates to True. |
>= | Checks if the value of the left operand is greater than or equal to the value of the right operand. If true, the condition evaluates to True. |
<= | Checks if the value of the left operand is less than or equal to the value of the right operand. If true, the condition evaluates to True. |
Example of VB.Net Comparison Operator
Let us demonstrate an example of VB.Net comparison operator:
Step 1) Create a new console application. If you don’t know how to do it, visit our previous tutorial on Data Types and Variables.
Step 2) Add the following code:
Module Module1 Sub Main() Dim x As Integer = 11 Dim y As Integer = 5 If (x = y) Then Console.WriteLine("11=5 is True") Else Console.WriteLine(" 11=5 is False") End If If (x < y) Then Console.WriteLine(" 11<5 is True") Else Console.WriteLine(" 11<5 is False") End If If (x > y) Then Console.WriteLine(" 11>5 is True") Else Console.WriteLine(" 11>5 is False") End If x = 3 y = 7 If (x <= y) Then Console.WriteLine(" 3<=7 is True") End If If (y >= x) Then Console.WriteLine(" 7>=3 is True") End If Console.ReadLine() End Sub End Module
Step 3) Click the Start button from the toolbar to run the code. You should get the following window:
We have used the following code:
Explanation of Code:
- Creating a module named Module1.
- Creating the main sub-procedure.
- Creating an integer variable x with a value of 11.
- Creating an integer variable y with a value of 5.
- Checking whether the value of variable x is equal to the value of variable y. We have the If…Then conditional statements.
- Printing some text on the console if the above condition is True.
- The Else part to execute if the above condition is False, that is, if x isn’t equal to y.
- Printing some text on the console if the above Else part executes.
- Ending the If condition.
- Checking whether the value of variable x is less than that of variable y.
- Printing some text on the console if the above condition is true.
- The Else part to execute if the above condition is False, that is, if the value of variable x is not less than the value of variable y.
- Printing some text on the console if the above Else part executes.
- Ending the If condition.
- Checking whether the value of variable x is greater than that of variable y.
- Printing some text on the console if the above condition is true.
- The Else part of executing if the above condition is False, that is, if the value of variable x is not greater than the value of variable y.
- Printing some text on the console if the above Else part executes.
- Ending the If condition.
- Assigning a new value to variable x, that is, from 11 to 3.
- Assigning a new value to variable y, that is, from 5 to 7.
- Checking whether the value of variable x is less than or equal to that of variable y.
- Printing some text on the console if the above condition is true.
- Ending the If condition.
- Checking whether the value of variable x is greater than or equal to that of variable y.
- Printing some text on the console if the above condition is true.
- Ending the If condition.
- To exit the console when the user presses any key.
Logical Operators in VB.Net
Logical operators help us in making logical decisions. Logical Operators in VB.Net are:
Logical Operator | Description |
---|---|
And | known as the logical/bitwise AND. Only true when both conditions are true. |
Or | known as the logical/bitwise OR. True when any of the conditions is true. |
Not | The logical/bitwise NOT. To reverse operand’s logical state. If true, the condition becomes False and vice versa. |
Xor | bitwise Logical Exclusive OR operator. Returns False if expressions are all True or False. Otherwise, it returns True. |
AndAlso | It is also known as the logical AND operator. Only works with Boolean data by performing short-circuiting. |
OrElse | It is also known as the logical OR operator. Only works with Boolean data by performing short-circuiting. |
IsFalse | Determines whether expression evaluates to False. |
IsTrue | Determines whether expression evaluates to True. |
Example of VB.Net Logical Operator
Let us demonstrate how to use logical operators using an example:
Step 1) Create a new console application. If you don’t know how to do it, visit our previous tutorial on Data Types and Variables.
Step 2) Add the following code:
Module Module1 Sub Main() Dim var_w As Boolean = True Dim var_x As Boolean = True Dim var_y As Integer = 5 Dim var_z As Integer = 20 If (var_w And var_x) Then Console.WriteLine("var_w And var_x - is true") End If If (var_w Or var_x) Then Console.WriteLine("var_w Or var_x - is true") End If If (var_w Xor var_x) Then Console.WriteLine("var_w Xor var_x - is true") End If If (var_y And var_z) Then Console.WriteLine("var_y And var_z - is true") End If If (var_y Or var_z) Then Console.WriteLine("var_y Or var_z - is true") End If 'Only logical operators If (var_w AndAlso var_x) Then Console.WriteLine("var_w AndAlso var_x - is true") End If If (var_w OrElse var_x) Then Console.WriteLine("var_w OrElse var_x - is true") End If var_w = False var_x = True If (var_w And var_x) Then Console.WriteLine("var_w And var_x - is true") Else Console.WriteLine("var_w And var_x - is not true") End If If (Not (var_w And var_x)) Then Console.WriteLine("var_w And var_x - is true") End If Console.ReadLine() End Sub End Module
Step 3) Run the code by clicking the Start button from the toolbar. You will get the following window:
Here are screenshots of the above code:
Explanation of Code:
- Creating a module named Module1.
- Creating the main sub-procedure.
- Declaring a Boolean variable var_w with a value of True.
- Declaring a Boolean variable var_x with a value of True.
- Declaring an integer variable var_y with a value of 5.
- Declaring an integer variable var_z with a value of 20.
- Performing And operation on values of variable var_w and var_x. We have used the If…Then condition to take action based on the result of the operation.
- Text to print on the console if the result of the above operation is True.
- Ending the If statement.
- Performing Or operation on values of variable var_w and var_x. We have used the If…Then condition to take action based on the result of the operation.
- Text to print on the console if the result of the above operation is True.
- Ending the If statement.
- Performing Xor operation on values of variable var_w and var_x. We have used the If…Then condition to take action based on the result of the operation.
- Text to print on the console if the result of the above operation is True.
- Ending the If statement.
- Performing And operation on values of variable var_y and var_z. We have used the If…Then condition to take action based on the result of the operation.
- Text to print on the console if the result of the above operation is True.
- Ending the If statement.
- Performing Or operation on values of variable var_y and var_z. We have used the If…Then condition to take action based on the result of the operation.
- Text to print on the console if the result of the above operation is True.
- Ending the If statement.
- A comment. The compiler will skip this.
- Performing AndAlso operation on values of variable var_w and var_x. We have used the If…Then condition to take action based on the result of the operation.
- Text to print on the console if the result of the above operation is True.
- Ending the If statement.
- Performing OrElso operation on values of variable var_w and var_x. We have used the If…Then condition to take action based on the result of the operation.
- Text to print on the console if the result of the above operation is True.
- Ending the If statement.
- Changing the value of variable w from true to False.
- The value of variable var_x will remain to be True.
- Performing And operation on values of variables var_w and var_x. We have used the If…Then condition to take action based on the result of the operation.
- Text to print on the console if the result of the above operation is True.
- Else part to be executed if the above If the condition is not True.
- Text to print on the console if the result of the above If the operation is False. Then it is under the Else statement.
- Ending the If statement.
- Performing And operation on values of variables var_w and var_x then reversing the result using the Not operator. We have used the If…Then condition to take action based on the result of the operation.
- Text to print on the console if the result of the above operation is True.
- Ending the If statement.
- Accept input from the user via the keyboard.
Bit Shift Operators in VB.Net
These operators are used for performing shift operations on binary values.
Bit Shift Operatiors | Description |
---|---|
And | Known as the Bitwise AND Operator. It copies a bit to result if it is found in both operands. |
Or | Known as the Binary OR Operator. It copies a bit if found in either operand. |
Xor | The Binary XOR Operator. For copying a bit if set in one of the operands rather than both. |
Not | It is known as the Binary Ones Complement Operator. It is a unary operator that ‘flips’ the bits. |
Bit Shift Operator Example in VB.Net
Let us demonstrate bit shift operators using an example:
Step 1) Create a new console application. If you don’t know how to do it, visit our previous tutorial on Data Types and Variables.
Step 2) Add the following code:
Module Module1 Sub Main() Dim w As Integer = 50 Dim x As Integer = 11 Dim y As Integer = 0 y = w And x Console.WriteLine("y = w And x is {0}", y) y = w Or x Console.WriteLine("y = w Or x is {0}", y) y = w Xor x Console.WriteLine("y = w Xor x is {0}", y) y = Not w Console.WriteLine("y = Not w is {0}", y) Console.ReadLine() End Sub End Module
Step 3) Run the code by clicking the Start button. You should get the following window:
Here is a screenshot of the code:
Explanation of Code:
- Creating a module named Module1.
- Creating the main sub-procedure.
- Creating an integer variable w with a value of 50.
- Creating an integer variable x with a value of 11.
- Creating an integer variable y with a value of 0.
- Applying the bitwise And operator to the values of variables w and x and assigning the result to variable y.
- Printing some text and the result of the above operation on the console.
- Applying the bitwise Or operator to the values of variables w and x and assigning the result to variable y.
- Printing some text and the result of the above operation on the console.
- Applying the bitwise Xor operator to the values of variables w and x and assigning the result to variable y.
- Printing some text and the result of the above operation on the console.
- Applying the bitwise Not operator to the value of variable w and assigning the result to variable y.
- Printing some text and the result of the above operation on the console.
- Pause the console to wait for user input via the keyboard.
- End the sub-procedure.
- End the module.
Assignment Operators in VB.Net
Below are the assignment operators in VB.Net:
Assignment Operator | Description |
---|---|
= | The simple assignment operator. It assigns the value of the right operand to the left operand. |
+= | Known as the Add AND assignment operator. It adds the right operand to the left operand, then assigns the result to the left operand. |
= | Known as the Subtract AND assignment operator. It subtracts the right operand from the left operand, then assigns the result to the left operand. |
*= | Known as the Multiply AND assignment operator. It multiplies the left operand by the right operand, then assigns the result to the left operand. |
Assignment Operator Example in VB.Net
Below is the assignment operator example in VB.Net:
Step 1) Create a new console application. If you don’t know how to do it, visit our previous tutorial on Data Types and Variables.
Step 2) Add the following code:
Module Module1 Sub Main() Dim x As Integer = 5 Dim y As Integer y = x Console.WriteLine(" y = x gives y = {0}", y) y += x Console.WriteLine(" y += x gives y = {0}", y) y -= x Console.WriteLine(" y -= x gives y = {0}", y) y *= x Console.WriteLine(" y *= x gives y = {0}", y) Console.ReadLine() End Sub End Module
Step 3) Now, run the code by clicking the Start button from the toolbar. You should get the following window:
The following code has been used:
Explanation of Code:
- Creating a module named Module1.
- Creating the main sub-procedure.
- Creating an integer variable x with a value of 5.
- Creating an integer variable y.
- Assigning the value of variable x to variable y. The value of variable y now becomes 5.
- Printing some text and the result of the above operation on the console.
- Adding the value of variable y to value of variable x, that is, 5 + 5, then assigning the result to variable y. This gives y = 10.
- Printing some text and the result of the above operation on the console.
- Subtracting the value of variable x (5) from the value of variable y (10) and assigning the result to variable y, that is, 10-5.
- Printing some text and the result of the above operation on the console.
- Multiplying the value of variable y with the value of variable x and assigning the result to variable y, that is, 5 * 5.
- Printing some text and the result of the above operation on the console.
- Pause the console waiting for user input.
- Ending the sub-procedure.
- Ending the module.
Miscellaneous Operators in VB.Net
There are other Miscellaneous operators supported by VB.Net. Let us discuss them:
Miscellaneous Operators | Description |
---|---|
GetType | This operator gives the Type of objects for a specified expression. |
Function Expression |
|
Miscellaneous Operator Example in VB.Net
Here is an example of VB.Net miscellaneous operator:
Step 1) Create a new console application. If you don’t know how to do it, visit our previous tutorial on Data Types and Variables.
Step 2) Add the following code:
Module Module1 Sub Main() Dim x As Integer = 5 Console.WriteLine(GetType(Integer).ToString()) Console.WriteLine(GetType(String).ToString()) Console.WriteLine(GetType(Double).ToString()) Dim trippleValue = Function(val As Integer) val * 3 Console.WriteLine(trippleValue(2)) Console.WriteLine(If(x >= 0, "Positive", "Negative")) Console.ReadLine() End Sub End Module
Step 3) Run the code by clicking the Start button on the toolbar. You should get the following window:
We have used the following code:
Explanation of Code:
- Creating a module named Module1.
- Creating the main sub-procedure.
- Declaring an integer variable x with a value of 5.
- Getting the Type object of Integer data type and converting the result to a string by calling the ToString() method and print it on the console.
- Getting the Type object of String data type and converting the result to a string by calling the ToString() method and print it on a console.
- Getting the Type object of Double data type and converting the result to a string by calling the ToString() method and printing it on the console.
- Declaring a lambda function and assigning it to the variable triple value. The lambda function takes an integer parameter named val and multiplies it by 3.
- Calling the lambda function and passing to it an argument of 2. It will triple this value to give 6.
- Checking whether the value of variable x is positive or negative. If greater than or equal to 0, it will print Positive, otherwise, Negative.
- Pause the console waiting for user input.
- Ending the sub-procedure.
- Ending the module.
Summary
- An Operator in VB.Net refers to a symbol that instructs the compiler to perform a specific logical or mathematical manipulation.
- VB.Net supports the use of operators to perform arithmetic, logical, and comparison operations.
- Operators are divided into various categories.
- Operators operate on operands.
- We can use arithmetic operators to perform various mathematical operations in VB.NET.
- Comparison operators are used for making comparisons between variables.
- Logical operators help us in making logical decisions.