156 Part II Programming Fundamentals
Total = 10 + 15 * 2 / 4 ^ 2
Total = 10 + 15 * 2 / 16
Total = 10 + 30 / 16
Total = 10 + 1.875
Total = 11.875
Using Parentheses in a Formula
You can use one or more pairs of parentheses in a formula to clarify the order of precedence
or impose your own order of precedence over the standard one. For example, Visual Basic
calculates the formula
Number = (8 – 5 * 3) ^ 2
by determining the value within the parentheses (–7) before doing the exponentiation—even
though exponentiation is higher in order of precedence than subtraction and multiplication,
according to the preceding table. You can further refine the calculation by placing nested
parentheses in the formula. For example,
Number = ((8 – 5) * 3) ^ 2
directs Visual Basic to calculate the difference in the inner set of parentheses first, perform
the operation in the outer parentheses next, and then determine the exponentiation.
The result produced by the two formulas is different: the first formula evaluates to 49
and the second to 81. Parentheses can change the result of a mathematical operation,
as well as make it easier to read.
Chapter 5 Quick Reference
To Do This
Declare a variable Type Dim followed by the variable name, the As keyword, and the
variable data type in the program code. To make the variable valid in all
a form’s event procedures, place this statement at the top of the code for
the form, before any event procedures. For example:
Dim Country As String
Change the value of
a variable
Assign a new value with the assignment operator (=). For example:
Country = "Japan"
Get input by using
a dialog box
Use the InputBox function and assign the result to a variable.
For example:
UserName = InputBox("What is your name?")