3.6. ArithmeticThe arithmetic operators are summarized in Fig. 3.19. The asterisk (*) indicates multiplication, and the keyword Mod represents the Mod operator (also known as the modulus or modulo operator), which will be discussed shortly. Most of the arithmetic operators in Fig. 3.19 are binary operators. For example, the expression sum + value contains the binary operator + and the two operands sum and value. Visual Basic also provides unary operators that take only one operand. For example, unary versions of plus (+) and minus () are provided, so that programmers can write expressions such as +9 and 19.
Division OperatorsVisual Basic has separate operators for integer division (the backslash, \) and floating-point division (the forward slash, /). Integer division takes two Integer operands and yields an Integer result; for example, the expression 7 \ 4 evaluates to 1, and the expression 17 \ 5 evaluates to 3. Any fractional part in an Integer division result simply is truncated (i.e., discarded)no rounding occurs. When floating-point numbers such as 2.3456 and 845.7840 are used with the integer division operator, the numbers are first rounded to the nearest whole number, then divided. This means that, although 7.1 \ 4 evaluates to 1 as expected, the statement 7.7 \ 4 evaluates to 2, because 7.7 is rounded to 8 before the division occurs. To divide floating-point numbers without rounding the operands (which is normally what you want to do), use the floating-point division operator. Common Programming Error 3.2
Error-Prevention Tip 3.2
Mod OperatorThe Mod operator yields the remainder after division. The expression x Mod y yields the remainder after x is divided by y. Thus, 7 Mod 4 yields 3, and 17 Mod 5 yields 2. We will use this operator mostly with Integer operands, but it also can be used with other types. In later chapters, we consider interesting applications of the Mod operator, such as determining whether one number is a multiple of another. Rules of Operator PrecedenceVisual Basic applies the operators in arithmetic expressions in a precise sequence determined by the following rules of operator precedence, which are similar to those followed in algebra:
Operators in expressions contained within a pair of parentheses are evaluated before those that are outside a pair of parentheses. Parentheses can be used to group expressions and change the order of evaluation to occur in any sequence desired by the programmer. With nested parentheses, the operators contained in the innermost pair of parentheses are applied first. The rules of operator precedence enable Visual Basic to apply operators in expressions in the correct order. When we say operators are applied from left to right, we are referring to the associativity of the operators. All binary operators in Visual Basic associate from left to right. If there are multiple operators, each with the same precedence, the order in which the operators are applied is determined by the operators' associativity. Figure 3.20 summarizes the rules of operator precedence. This table will be expanded as we introduce additional operators in subsequent chapters. Appendix A contains a complete operator-precedence chart.
|