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