Swift Operator precedence and associativity: In this tutorial you will learn about the standards used to evaluate an expression with operators and operands.
operator precedence defines the order wherein a given mathematical expression is evaluated. At the point when an expression incorporates numerous operators then every one of the single pieces of the given expression is evaluated in a specific order keeping a few rules defined according to operator precedence. The operator with higher precedence is evaluated first and the operator with the lowest precedence is evaluated finally.
Operator precedence is a set of decides that figures out which operator is executed first.
Before you learn about operator precedence, make sure to know about Swift operators.
Let’s take an example, suppose there is more than one operator in an expression.
var num = 8 + 5 * 4 // 28
Here,
if + is executed first, the value of num will be 52
if * is executed first, the value of num will be 28
For this situation, operator precedence is used to identify which operator is executed first. The operator precedence of * is higher than + so multiplication is executed first.
In this article, you will learn-
Operator precedence table
The table underneath lists the precedence of Swift operators. The higher it appears in the table, the higher its precedence.
Operators | Examples |
Bitwise shift | >> << |
Multiplicative | % * / |
Additive | | – + – ^ |
Range | ..< … |
Casting | is as |
Nil-Coalescing | ?? |
Comparison | != > < >= <= === == |
Logical AND | && |
Logical OR | || |
Ternary Operator | ? : |
Assignment Precedence | |= %= /= *= >>= <<= ^= += -= |
Example: Operator precedence with the complex assignment operator
var num = 15 num += 10 - 2 * 3 print(num)
Output
19
In the above example, we have created a variable num with the worth 15. Notice the statement
num += 10 – 2 * 3
Here, the precedence order from higher to lower is *, -, and +=. Hence, the statement is executed as num += 10 – (2 * 3).
Swift Operator Associativity
On the off chance that an expression has two operators with comparative precedence, the expression is evaluated by its associativity (either left to right, or right to left)). For instance,
print(6 * 4 / 3) // 8
Here, operators* and/have similar precedence. Furthermore, their associativity is from left to right. Subsequently, 6 * 4 is executed first.
Note: If we need to execute the division first, we can use parentheses as print(6 * (4/3)).
Operator Associativity Table
In the event that an expression has two operators with comparative precedence, the expression is by its associativity.
- Left Associativity – operators are evaluated from left to right
- Right Associativity – operator are evaluated from right to left
- Non-associativity – operators have no defined conduct
For instance,
print (6 * 4 / 3)
Here, operators* and/have a similar precedence. Notwithstanding, their associativity is left. Thus, 6 * 4 is executed first.
The table underneath shows the associativity of Swift operators.
Operators | Associativity |
Bitwise Shift: >> << | none |
Multiplicative: % * / | left |
Additive: | + – ^ | left |
Range: ..< … | none |
Casting: is as | none |
Nil-Coalescing: ?? | right |
Comparison: != > < >= <= === == | none |
Logical AND: && | left |
Logical OR: || | left |
Ternary Operator: ? : | right |
Assignment: |= %= /= *= >>= <<= ^= | right |
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.