Programing in Visual Basic 2010 65
Programing in Visual Basic 2010 65
Table 2.4
Variables and Constants A Place for Everything and Everything in Its Place
Exponentiation
Mathematical Sign
+
A+B
XY
RS
H K
VB Equivalent
+
shoNum1 + shoNum2
decSubtotal decDiscountAmount
( )n
T2
decSubtotal decDiscountRate
/, \, Mod
shoTotal / shoNumScores
shoNumCases \ shoCaseSize
shoTotalBottles Mod shoCases
shoSide 3
an integer. For example, 17\5 = 3. Use Mod to find the remainder. For example,
17 Mod 5 = 2. Mod is short for modulus. Use the circumflex (), sometimes
called the caret or hat, for exponentiation. Table 2.4 shows the mathematical
operators.
Rules of Precedence
One other major difference is how multiplication is done when parentheses are
involved. In math, you might have written 2(L + W) when you wanted to add L
and W and then multiply by 2. The computer will not automatically multiply by
2. You literally have to put in the multiplication ( ) sign. So, if you were writing
a formula to find the perimeter of a rectangle, the formula would be
Perimeter = 2(length + width)
but the assignment statement in the computer might look like this:
sngPerimeter = 2*(sngLength + sngWidth)
Its almost the same, but its just different enough to be maddening and
confusing.
The computer doesnt respond well to errors. You cannot try to do math with
strings; the computer wont like it. You cannot divide by zero either it will
cause your program to crash. There are ways to avoid this, and youll have to, to
write good programs. But dont lose sleep over it just yet.
When you assign a value to a variable, you must make sure it can store the
answer. For example, if you were trying to find your test average, you would add
up the scores on your tests. Well say there are four scores. Youd then divide that
total by 4 to find the average. The mathematical formula would look like this:
Average = (Test1 + Test2 + Test3 + Test4)/4