# VB.Net Maths operators

An arithmetic expression is one that can be evaluated to give a numeric value, and combines variables, arithmetic operators and keywords. The table below describes VB.Net's primary math operators.

Operator | Example | Description |
---|---|---|

+ | a = b + c | Addition - adds two values together |

- | a = b - c | Subtraction - subtracts the second value from the first value |

* | a = b * c | Multiplication - multiplies two values together |

/ | a = b / c | Division - divides the first value by the second value |

\ | a = b \ c | Integer division - similar to division, but rounds the result down to the nearest integer |

Mod | a = b Mod c | Modulus - returns the remainder of dividing the value to the left of the Mod operator by the value to its right |

^ | a = b ^ c | Exponentiation - raises the value to the left of the exponentiation operator to the power of the value on its right |

+= | a += b | Addition shortcut - adds the value of two variables together and assigns the result to the first variable |

-= | a -= b | Subtraction shortcut - subtracts the value of the second variable from the value of the first variable and assigns the result to the first variable |

*= | a *= b | Multiplication shortcut - multiplies the values of two variables together and assigns the result to the first variable |

/= | a /= b | Division shortcut - divides the value of the first variable by the value of the second variable and assigns the result to the first variable |

\= | a \= b | Integer division shortcut - divides the value of the first variable by the value of the second variable and assigns the result (rounded down to the nearest integer) to the first variable |

^= | a ^= b | Exponentiation shortcut - raises the value of the first variable to the power of the value of the second variable, and assigns the result to the first variable |

Note that all variables used in an expression that are to the right of an assignment operator must be declared and initialised before they can be used. The value of the expression that follows the equals symbol (=) is *assigned* to the variable that precedes the equals symbol.

## Operator precedence

In a complex expression involving a number of operators, VB.Net carries out all of the calculations before assigning the result. Consider the following program statement:

Result = 8 * 10 - 3 + 12 * 20

Combining expressions in this way can produce unexpected results, because VB.Net carries out the various arithmetical operations in a predetermined order, as follows:

- exponentiation
- multiplication and division (working from left to right)
- integer division
- modulus
- addition and subtration

The result of the above expression (i.e. the value of **Result**) is therefore 317. VB.Net first computes the value of 8 * 10, then the value of 12 * 20. This reduces the expression to the following:

Result = 80 - 3 + 240

The order of evaluation is usually referred to as *operator precedence*. Note that, in the event of two operators in an expression having the *same* level of operator precedence, the order of evaluation is strictly *left-to-right*.

It is possible to override operator precedence using *parentheses*. VB.Net always computes values inside parentheses before anything else in an expression. The following statement stores a value of 296 in **Result**, because the use of parentheses forces VB.Net to compute the subtraction first:

Result = 8 * (10 - 3) + 12 * 20