Loading

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

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