# Visual Basic 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 Visual Basic's primary math operators.

Visual Basic Math Operators
OperatorExampleDescription
+a = b + cAddition - adds two values together
-a = b - cSubtraction - subtracts the second value from the first value
*a = b * cMultiplication - multiplies two values together
/a = b / cDivision - divides the first value by the second value
\a = b \ cInteger division - similar to division, but rounds the result down to the nearest integer
Moda = b Mod cModulus - returns the remainder of dividing the value to the left of the Mod operator by the value to its right
^a = b ^ cExponentiation - raises the value to the left of the exponentiation operator to the power of the value on its right
+=a += bAddition shortcut - adds the value of two variables together and assigns the result to the first variable
-=a -= bSubtraction shortcut - subtracts the value of the second variable from the value of the first variable and assigns the result to the first variable
*=a *= bMultiplication shortcut - multiplies the values of two variables together and assigns the result to the first variable
/=a /= bDivision shortcut - divides the value of the first variable by the value of the second variable and assigns the result to the first variable
\=a \= bInteger 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 ^= bExponentiation 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, Visual Basic 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 Visual Basic carries out the various arithmetical operations in a predetermined order, as follows:

1. exponentiation
2. multiplication and division (working from left to right)
3. integer division
4. modulus
5. addition and subtration

The result of the above expression (i.e. the value of Result) is therefore 317. Visual Basic 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. Visual Basic 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 to compute the subtraction first:

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