Formula is a statement that combines numbers, variables, operators, and keywords to create a new value. Visual Basic contains several language elements designed for use in formulas. In this section, you’ll practice working with arithmetic (or mathematical) operators, the symbols used to tie together the parts of a formula. With a few exceptions, the arithmetic symbols you’ll use are the ones you use in everyday life, and their operations are fairly intuitive. You’ll see each operator demonstrated in the following exercises.
Arithmetic Operators
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.
No comments:
Post a Comment