Visual Basic Operators
This section lists the most common mathematical &
string operators.
Operator |
Meaning |
Example |
Result |
^ |
Exponential |
2^3 |
8 |
* |
Multiplication |
2*2 |
4 |
/ |
Division |
10/2 |
5 |
+ |
Addition |
2+5 |
7 |
- |
Subtraction |
5-2 |
3 |
Mod |
Modulus |
11 Mod 3 |
2 |
/\ |
Integer Division |
11\3 |
3 |
+ or &
|
String concatenation |
"V"
+ "B" |
"VB" |
An operator manipulates data by combining or computing
results. |
The plus sign is an overloaded operator because it
performs more than one function depending on the data
you place on either side of it.
Order of Operators
VB performs math in a strict predefined order. The
following table lists some examples to show the order.
Order |
Operators |
Example |
Result |
1 |
Parenthesis () |
(2 + 2) * 5 |
20 |
2 |
^, + |
2 ^ 3 + 1 |
9 |
3 |
+, * |
2 + 3 * 5 |
17 |
4 |
+, -, * |
10 - 4 * 2 +
1 |
3 |
VB always calculates the intermediate
results of any multiplication and division in an
expression before the addition and subtraction,
unless parentheses override the default order. Exponentiation
has higher priority. If * and / both appear in an
expression, VB performs from left to right.
|
|
In this chapter, you have
seen that VB supports several kinds of data
types. You use variables for intermediate
storage as a program runs. You must declare
a variable before you use it. Moreover, you
have seen several math & string operators
in this chapter.
|
|