Concept

# Operator Precedence

<div class="rst-class">

forsearch

</div>

Operator

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

This section describes the order in which operators are executed in an expression that has two or more operators.

## Notes

Operator Precedence determines the order in which operators execute when there is more than one operator in an expression. For example, the expression:

``` xojo
5 + 2 / 3
```

contains both the division and addition operators. It matters whether the division or the addition takes place first. You can force a particular precedence via parentheses. By default, division has precedence over addition, so this expression would evaluate as 5 plus 2/3. You can force it to do the addition first by writing:

``` xojo
(5 + 2) / 3
```

It is easy to be confused (especially if you have a mathematical background). The following produces what might be an unexpected result:

``` xojo
-4 ^ 2 = -16
```

To get the expected result use:

``` xojo
(-4) ^ 2 = 16
```

The order from highest precedence to lowest is shown in the following table:

| Operator                                                                                                                                                                                                                                                                                                                                           | Description                                                             |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------|
| .                                                                                                                                                                                                                                                                                                                                                  | Dot operator                                                            |
| `AddressOf</api/language/addressof>`                                                                                                                                                                                                                                                                                                               | `Delegate</api/data_types/additional_types/delegate>` creation operator |
| `IsA</api/language/operators/comparison/isa>`, `Is</api/language/operators/comparison/is>`                                                                                                                                                                                                                                                         | Object comparison operators                                             |
| `^</api/language/operators/mathematical/^>`                                                                                                                                                                                                                                                                                                        | Exponentiation operator                                                 |
| `-</api/language/operators/mathematical/->`                                                                                                                                                                                                                                                                                                        | Negation or the unary minus operator                                    |
| `Not</api/language/operators/bitwise_logical/not>`                                                                                                                                                                                                                                                                                                 | Logical not operator                                                    |
| `*</api/language/operators/mathematical/x>` `Division</api/language/operators/mathematical/division>` `Integer Division</api/language/operators/mathematical/integer_division>` `Mod</api/language/operators/mathematical/mod>`                                                                                                                    | Multiplication and division arithmetic operators                        |
| `-</api/language/operators/mathematical/->` `+</api/language/operators/mathematical/+>`                                                                                                                                                                                                                                                            | Subtraction and addition arithmetic operators                           |
| `=</api/language/operators/comparison/less_than_or_equal>` `\></api/language/operators/comparison/greater_than>` `<</api/language/operators/comparison/less_than>` `\>=</api/language/operators/comparison/greater_than_or_equal>` `\<=</api/language/operators/comparison/less_than_or_equal>` `<></api/language/operators/comparison/not_equal>` | Comparison operators                                                    |
| `And</api/language/operators/bitwise_logical/and>`                                                                                                                                                                                                                                                                                                 | Bitwise and logical operator                                            |
| `Or</api/language/operators/bitwise_logical/or>` `Xor</api/language/operators/bitwise_logical/xor>`                                                                                                                                                                                                                                                | Bitwise and logical operator                                            |
| `:</api/language/operators/pair_operator>`                                                                                                                                                                                                                                                                                                         | Pair creation operator                                                  |

If there is more than one operator in an expression with the same precedence, they are evaluated in left-to-right order (left associative) in the expression. For example:

``` xojo
Var number As Integer
number = 5 / 100 * 255
' number = 12
```

All operators are left-associative except for pairs (`:</api/language/pair>`) and exponentiation (`^</api/language/operators/mathematical/^>`).

Left associative means that (foo or bar or baz) will evaluate like ((foo or bar) or baz) instead of (foo or (bar or baz)). Conversely, right associative means that (foo : bar : baz) will evaluate like (foo : (bar : baz)) instead of ((foo : bar) : baz).

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### Short-circuit evaluation

When used within an `If..Then</api/language/if...then...else>` statement, once the first expression results to `False</api/language/false>`, none of the subsequent expressions are evaluated. In this example, the IsValid method is not called because the first expression is `False</api/language/false>`. This is referred to as short-circuit evaluation:

``` xojo
Var b As Boolean = False

If b And IsValid("value") Then ' IsValid does not get called
  ...
End If
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`-</api/language/operators/mathematical/->`, `*</api/language/operators/mathematical/x>`, `+</api/language/operators/mathematical/+>`, `/</api/language/operators/mathematical/division>`, `:</api/language/operators/pair_operator>`, `\></api/language/operators/comparison/greater_than>`, `\>=</api/language/operators/comparison/greater_than_or_equal>`, `<</api/language/operators/comparison/less_than>`, `\<=</api/language/operators/comparison/less_than_or_equal>`, `=</api/language/operators/comparison/equals_operator>`, `<></api/language/operators/comparison/not_equal>`, `\\</api/language/operators/mathematical/integer_division>`, `^</api/language/operators/mathematical/^>`, `AddressOf</api/language/addressof>`, `And</api/language/operators/bitwise_logical/and>`, `If</api/language/if>`, `IsA</api/language/operators/comparison/isa>`, `Mod</api/language/operators/mathematical/mod>`, `Or</api/language/operators/bitwise_logical/or>`, `Xor</api/language/operators/bitwise_logical/xor>` operators; `If...Then...Else</api/language/if...then...else>` command

</div>
