VB6 Tutorial 08: Operators & Expressions

By | 2018-03-23

An operator in a programming language is a symbol that tells the compiler or interpreter to perform specific mathematical, relational or logical operation and produce final result.

OperatorsMeaningsOperatorsMeanings
+Addition>=Greater than or equal to
Subtraction<>Not equal to
*Multiplication=equal to
/Division&String Concatenation
\Integer DivisionAndLogical And
ModModulo DivisionNotLogical Not
<Less thanOrLogical Or
>Greater thanXorLogical Xor
<=Less than or equal to^Power

Assigning values to variables : ‘=’ operator

The Assignment operator (=) is used to assign a value or an expression to a variable. When you assign a value, its not the same as the Equal to operation, but the value is copied to a variable. Besides assigning values and expressions, you can assign a variable to a variable also.

Example

x=a+b

‘=’ , ‘+’ are the operators.

x,a,b are the operands and ‘a+b’ is an expression. x=a+b is a statement.

The following program will print 20.

Private Sub cmdDisplay_Click()
    Dim num As Integer, r As Integer, n As Integer
    n = 10
    r = n ^ 2      'n to the power 2
    num = n + r / n
    Print num
End Sub

Output : 20

The Integer Division Operator (‘\’)

The Integer Division operator(‘\’) eliminates the decimal part after division.

Example

Private Sub cmdDivision_Click()
    Dim a As Double, b As Double, c As Double
    a = 12: b = 5
    c = a \ b
    Print c
End Sub

Output : 2

The ‘Mod’ operator

The Mod operator is used to calculate remainder.

Example :

Private Sub cmdShow_Click()
    Dim remainder As Integer, num As Integer
    num = 21
    remainder = num Mod 10
    Print remainder
End Sub

Output : 1

Boolean Operators : And, Or, Not, Xor

To learn about Boolean operators, you first need to have the concept of If-Else condition. So Boolean operators are explained in a later lesson

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.