Arithmetic Operators
Most programs will involve some arithmetic computations. The VB arithmetic operators and their order of precedence are given in the table below. Precedence refers to what operations will be done first when a computation involves more than one operation. The rules in VB are the same as those in algebra. You may find it helpful to think of the phrase "Please Excuse My Dear Aunt Sally" for Parentheses / Exponentiation / Multiplication and Division (same precedence left-to-right) / Addition and Subtraction (same precedence left-to-right). Bear in mind that in VB, the precedence of "integer division" and "modulo" are below multiplication and "real" division, but above addition and subtraction.
Operation |
Operator |
Example |
exponentiation |
^ (this symbol is the "caret" - the shifted character above the number "6" on the keyboard) |
intX = 2 ^ 3 (8 would be stored in intX because 2^{3 }= 8) |
multiplication and "real" division (division where any digits to right of the decimal point are retained) |
* for multiplication / for real division (Multiplication and real division have equal precedence left-to-right) |
intX = 4 * 3 / 2 * 5 (30 would be stored in intX) |
integer division |
\ |
intX = 15 \ 2 (7 would be stored in intX. With integer division, the portion of the result after the decimal point is lost. So even though the result is 7.5, the .5 is lost.) |
modulo |
Mod (the remainder of integer division) |
intX = 15 Mod 2 (1 would be stored in intX. 15 divided by 2 is 7, with a remainder of 1. It is the remainder of 1 that is stored with Mod.) |
addition and subtraction |
+ for addition - for subtraction (Addition and subtraction have equal precedence left-to-right) |
intX = 1 + 2 - 3 + 4 (4 would be stored in X.) |
Bear in mind that parentheses can override the precedence of operations listed above, for example:
intX = 3 + 4 * 5
would cause 23 to be stored in intX (because you multiply first, then add).
But
intX = (3 + 4) * 5
would cause 35 to be stored in intX (because the parentheses caused the addition to take place first).
Parentheses in an expression can be nested (that is, you can have one set of parentheses within another). In the case of nested parentheses, the inner-most set is always evaluated first. For example, the statement
intX = (6 * (5 + 4) - 1) * 2
would cause 106 to be stored in intX. Can you see why?
Here are two other expressions for you to check out.
(1) The following code will cause 16 to be stored in the variable intY. Can you see why?
intX = 2
intY = 3 + 4 * intX ^ intX - 4 + (5 / (3 + 2))
(2) The following code will cause -61 to be stored in the variable intA. Can you see why?
intB = 3
intA = 4 * 5 / 5 \ 5 + 3 - intB * 2 - ((6 + 4) * 5 + 2 ^ intB)
Although the integer division (\) and MOD operators are probably used less frequently than the other arithmetic operators, they are useful for solving certain types of problems. Here are two examples:
(1) In date validation routines, it is often necessary to check whether or not a year is a leap year. The rule is that if a year is evenly divisible by four ("evenly divisible" means that the remainder is zero), it is a leap year. For example, years like 1992, 1996, and 2000 are leap years, while 1900, 1994, and 1997 are not. A test can be made as follows:
If (intYear Mod 4) = 0 Then ...
(2) Here's a bit of code that, given a total number of minutes, breaks down those total minutes into hours and minutes:
intHours = intTotMins \ 60
intMins = intTotMins Mod 60
If the variable intTotMins contained 285, then after the above code was executed, the variable intHours would contain 4 and the variable intMins would contain 45.