Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 4: Data, Operations, and Built-In Functions
Last change: February 13 2016 18:47:36.
<<PreviousNext>>

Numeric Operations

Refer back to the discussion of the assignment statement, the code that appears on the right side of the equal sign is recognized as expression, which can be a constant, a variable, or any operations on any combinations of constants and variables that result in a value. The symbols used to express arithmetic operations in VB are similar to the daily arithmetic symbols and are listed in the following table.

Symbol Arithmetic Operations Example Meaning
- Negation (unary) -A Negative value of A
+ Addition A + B A plus B
- Subtraction A – B A minus B
* Multiplication A * B A times B
/ Division A / B A divided by B
\ Integer division A \ B A divided by B
^ Power A ^ B AB
Mod Modulus A Mod B Remainder of A divided by B

The following are examples of valid expressions:

Salary + Commission Mph * Hours (Fahrenheit – 32) * 5 / 9 3.1416 * R ^ 2 20 Mod 3

The Mod operation divides the first operand by the second operand, and returns the remainder. The last expression, 20 Mod 3, will give 2 as a result because the remainder of 20/3 is 2.

Operational Precedence

When you combine several arithmetic operations in one expression, the order of execution resembles the ordinary arithmetic rules of precedence as follows (from highest to lowest):
Power (^)
Unary negation (-)
Multiplication and division (*, /)
Integer division (\)
Mod (Mod)
Addition and subtraction (+, -)
When the expression involves two or more operations of the same level of precedence, the execution goes from left to right.

Use of Parentheses

In many cases, you may find this order not exactly what you want. You can use parentheses to change the order of execution. An operation enclosed in a pair of parentheses will always be performed first. You can place as many pairs of parentheses as you want in an expression. You can also nest the parentheses. In this case, operations in the innermost pair will be performed first. Here are some examples of expressions using parentheses:

(7 + 8) * 3 (1 + Rate) ^ N ‘ compound interest for n periods H * (A * X ^ (1 + B)) ‘total costs for the learning curve effect

Last change: February 13 2016 18:47:34.
<<PreviousNext>>