Operators
Operators
1
Visual Basic Division
Division is achieved in Visual Basic using the / operator. In division, the left hand operand is divided
by the right hand operand. For example, the following example returns 5 (20 divided by 4):
Dim num1, num2, num3 As Integer
num1 = 20
num2 = 4
num3 = num1/num2
Suppose, in the about example if num1, num2 and num3 are declared as Double instead of Integer.
So let us see the code segment below:
Dim num1, num2, num3 As Double
num1 = 23
num2 = 4
num3 = num1/num2
The above code segment will returns 5.75 as result of the division operation. Sometimes if we want to
discard the decimal places from the division operation, we have another division operator ”\”, which
return the result as Integer without the decimal places. Let us modify the above code to get the code
return only integer as result
Dim num1, num2, num3 As Double
num1 = 23
num2 = 4
num3 = num1\num2
The above code segment will return 5 as result.
2
Operators Type Description
= Equal to Returns True if first operand equals second
Returns True if the value of the first operand is less than the
< Less than
second
Returns True if the value of the first operand is greater than the
> Greater than
second
Less than or Returns True if the value of the first operand is less than, or
<=
equal to equal to, the second
Greater than or Returns True if the value of the first operand is greater than, or
>=
equal to equal to, the second
Returns True if the value of the first operand is not equal to, the
<> Not equal to
second
3
Dim num1 As Integer
num1 = 10
If num1 < 25 Xor num2 > 45 Then
MessageBox.Show("OK")
End If
The final Logical Operator is the Not operator which simply inverts the result of an expression:
We can also compare strings with the operators. However, there are certain rules to follow where
upper case letters are less than lowercase letters, and number are less than letters.