0% found this document useful (0 votes)
1 views4 pages

Operators

The document provides an overview of operators in Visual Basic, detailing how expressions are formed using operators and operands for arithmetic and logical evaluations. It covers various operations including addition, subtraction, multiplication, division, exponentiation, modulus, comparison, and logical operators, with examples for each. Additionally, it explains how to use these operators in conditional statements to control program flow.

Uploaded by

Ningombam Jimson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

Operators

The document provides an overview of operators in Visual Basic, detailing how expressions are formed using operators and operands for arithmetic and logical evaluations. It covers various operations including addition, subtraction, multiplication, division, exponentiation, modulus, comparison, and logical operators, with examples for each. Additionally, it explains how to use these operators in conditional statements to control program flow.

Uploaded by

Ningombam Jimson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Visual Basic Operators

Expressions in Visual Basic


Arithmetic calculations and logical evaluations are defined in Visual Basic using expressions. The
calculation 1 + 1 is, for example, an expression.
Expressions consist of two key components called operators and operands. The operands define
the values or variable on which the arithmetic or logical evaluation are to be made. The operators
defines what action is to be performed on the operands. Often, an assignment operator is also used to
define what should happen to the result of the expression. For example, consider the following VB
statement
Dim result As Integer
result = 4+6
In the above example, the numbers ’4’ and ’5’ are both operands. The ’+’ is an operator which adds
together the two operands. Similarly, the ’=’ is an operator which assigns the result of the addition to
the result variable which is an operand.

Visual Basic Addition


Addition is performed in Visual Basic using the plus (+) operator. For example:
Dim total As Integer
total = 4 + 5 + 8
Variables may also be used as operands:
Dim num1, num2, num3 As Integer
num1 = 10
num2 = 20
num3 = num1 + num2

Visual Basic Subtraction and Negation


The subtraction (-) is used to perform subtraction in Visual Basic:
Dim num1, num2, num3 As Integer
num1 = 50
num2 = 45
num3 = 10 - num1 - num2
A negative number is denoted by prefixing the number with the (-) sign:
Dim num As Integer
num = -67

Visual Basic Multiplication


Visual Basic, like most other programming languages, use the (*) operator (rather than the × we all
use when writing mathematical expressions) to perform multiplications:
Dim num1, num2, num3 As Integer
num1 = 50
num2 = 45
num3 = num1 * num2

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.

Visual Basic Exponentiation


Exponentiation involves raising a number to a particular power. For example 10ˆ3 would evaluate
to 1000. The carat (ˆ) character is used to represent exponentiation in Visual Basic:
Dim num As Integer
num = 10ˆ3

Visual Basic Modulus Arithmetic


Modulus involves the division of two numbers and obtaining the remainder of that division. Modulus
calculations are performed in Visual Basic using the Mod keyword. For example:
10 Mod 2 - returns 0
10 Mod 4 - returns 2
12 Mod 5 - returns 2

Visual Basic Comparison/Conditional Operators


Comparison operators provide the ability to compare one value against another and return either a
True or False result depending on the status of the match. The comparison operators are used with
two operands, one to the left and one to the right of the operator. Conditional operators are very
powerful tools, they let the VB program compare data values and then decide what action to take,
whether to execute a program or terminate the program and more. The following table outlines the
Visual Basic comparison operators and provides brief descriptions

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

Visual Basic Logical Operator


Logical Operators are also known as Boolean Operators because they evaluate parts of an expression
and return a True or False value, allowing decisions to be made about how a script should proceed.
The Visual Basic Logical operators are shown in table below:
Operators Description
And Both sides must be true
Or One side or other must be true
Xor One side or other must be true but not both
Not Negates true
The first step to understanding how logical operators work is to construct a sentence rather than to
look at a script example right away. Let’s assume we need to check some aspect of two variables
named num1 and num2. Our sentence might read:
If num1 is less than 25 AND num2 is greater than 45 display a message.
Here the logical operator is the ”AND” part of the sentence. If we were to express this in Visual
Basic we would use the AND logical operator:
Dim num1, num2 As Integer
num1 = 10
num2 = 45
If num1 < 25 And num2 > 45 Then
MessageBox.Show("OK")
End If
Similarly, if our sentence was to read:
If intVal1 is less than 25 OR intVal2 is greater than 45 display a message.
Then we would use the Visual Basic Or operator in our If statement:
Dim num1, num2 As Integer
num1 = 10
num2 = 45
If num1 < 25 Or num2 > 45 Then
MessageBox.Show("OK")
End If
The Exclusive Or (XOR) operator returns true if only one of the expressions evaluates to be true. For
example:
If num1 is EITHER less than 25 OR greater than 45 display a message
We represent XOR with the keyword Xor:

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:

(10 > 1) ’ returns ’’True’’


Not(10 > 1) ’ returns ’’False’’ because we have inverted
the result with the logical NOT

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.

You might also like