Section 3.6. Arithmetic


3.6. Arithmetic

The 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.

Figure 3.19. Arithmetic operators.

Visual Basic operation

Arithmetic operator

Algebraic expression

Visual Basic expression

Addition

+

f + 7

f + 7

Subtraction

p c

p - c

Multiplication

*

bm

b * m

Division (floating point)

/

x / y

Division (integer)

\

none

v \ u

Modulus

Mod

r mod s

r Mod s

Exponentiation

^

qp

q ^ p

Unary minus

-

e

e

Unary plus

+

+g

+g


Division Operators

Visual 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

Using the integer division operator (\) when the floating-point division operator (/) is expected (i.e., when one or both of the operands is a floating-point value) can lead to incorrect results.


Error-Prevention Tip 3.2

Ensure that each integer division operator has only integer operands.


Mod Operator

The 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 Precedence

Visual 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:

  1. Exponentiation is applied first. If an expression contains several exponentiation operations, they are applied from left to right.

  2. Unary plus and minus, + and - (also called sign operations), are applied next. If an expression contains several sign operations, they are applied from left to right. Sign operations + and - have the same level of precedence.

  3. Multiplication and floating-point division operations are applied next. If an expression contains several multiplication and floating-point division operations, they are applied from left to right. Multiplication and floating-point division have the same level of precedence.

  4. Integer division is applied next. If an expression contains several Integer division operations, they are applied from left to right.

  5. Modulus operations are applied next. If an expression contains several modulus operations, they are applied from left to right.

  6. Addition and subtraction operations are applied last. If an expression contains several addition and subtraction operations, they are applied from left to right. Addition and subtraction have the same level of precedence.

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.

Figure 3.20. Precedence of arithmetic operators.

Operator(s)

Operation

Order of evaluation (precedence)

^

Exponentiation

Evaluated first. If there are several such operators, they are evaluated from left to right.

+,

Sign operations

Evaluated second. If there are several such operators, they are evaluated from left to right.

*, /

Multiplication and Division

Evaluated third. If there are several such operators, they are evaluated from left to right.

\

Integer division

Evaluated fourth. If there are several such operators, they are evaluated from left to right.

Mod

Modulus

Evaluated fifth. If there are several such operators, they are evaluated from left to right.

+,

Addition and Subtraction

Evaluated sixth. If there are several such operators, they are evaluated from left to right.




Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net