English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn the rules for calculating expressions with operators and operands.
Operator precedence is a set of rules used to calculate a given mathematical expression. When multiple operators are used in a single expression, each part is calculated in a specific order called operator precedence. Some operators have a higher precedence than others, which affects the way the expression is calculated.
The following table lists theOperatorThe higher it is displayed in the table, the higher its precedence is.
Operator groups | Example |
Bit change precedence | >> &<< &>> >> |
Multiplication operator precedence | &* % & * / |
Addition precedence | | &+ &- + – ^ |
Range formation precedence | ..< … |
Type conversion precedence | is as |
Empty union operator precedence | ?? |
Comparison precedence | != > < >= <= === === |
Logical associativity precedence | && |
Logical Disjunction Precedence | || |
Default Precedence | ~> |
Ternary Operator Precedence | ?: |
Arrow Function Precedence | ( ) |
Assignment Precedence | |= %= /= &<<= &>>= &= *= >>= <<= ^= += -= |
let x = 4 + 5 * 5 print(x)
When you run the above program, the output will be:
29
In the above example, if the expression is read from left to right, it may be expected that the output will be45. However, the precedence of the multiplication operator is higher than that of the addition operator, so the expression4 + 5 * 5Is calculated to be4 + (5 * 5). Therefore, the output printed on the screen is29.
Step | Operator | Expression | The value returned from the expression |
---|---|---|---|
1 | * | 5 * 5 | 25 |
2 | + | 4 + 25 | 29 |
3 | = | print(x) | 29 |
var y = 15 y += 10 - 2 * 3 print(y)
When you run the above program, the output will be:
19
In the above example, the expression of the variable y var y = 15Is assigned a value15.
In the next expression y + = 10-2 * 3In which, the precedence of the multiplication operator is higher than that of the subtraction compound assignment operator(+ =)
Therefore, the expression y + = 10-2 * 3 Is calculated as y = y + (10-(2 * 3))
Step | Operator | Expression | The value returned by the expression |
1 | = | var y = 15 | 15 |
2 | * | 2 * 3 | 6 |
3 | -- | 10-6 | 4 |
4 | + = | 15 + 4 | 19 |
Although there are predefined rules defined by operator precedence to evaluate expressions, you may wonder what happens if there are multiple operators with the same precedence. Operator associativity defines how operators with the same precedence are combined.
In Swift, operators can be left-associative, right-associative, or non-associative. When used in an expression in order, left-associative operators group operands from the left side of the statement, right-associative operators and non-associated operators do not have a defined behavior.
The following table shows the associativity and precedence of Swift operators.
Operator groups | Example | Associativity |
---|---|---|
Bitwise shift precedence | >>&<<&>> >> | 无 |
Multiplication operator precedence | &*%&* / | Left Associativity |
Addition precedence | | &+&-+-^ | Left Associativity |
Range formation precedence | .. <... | 无 |
Type conversion precedence | is a | 无 |
Empty union operator precedence | ?? | Right Associativity |
Comparison precedence | !=> <> = <= === === | 无 |
Logical Conjunction Precedence | && | Left Associativity |
Logical Disjunction Precedence | || | Left Associativity |
Default Precedence | ~> | 无 |
Ternary Operator Precedence | ?: | Right Associativity |
Arrow Function Precedence | ()) | Right Associativity |
Assignment Precedence | | =%= / =&<< =&>> =&= * = >> = << = ^ = | Right Associativity |
let x = 40 / 2 * 10 print(x)
When you run the above program, the output will be:
200
In the above program, the expression is calculated from left to right because the operators belong to the multiplication precedence group and have left associativity. Therefore, the division operation is executed early, resulting in200. If you want to perform the multiplication operation first, what should you do? You need to put2 * 10Expressions wrapped in parentheses, as shown below:
let x = 40 / (2 * 10) print(x)
When you run the above program, the output will be:
2
You do not need to remember the precedence and associativity table. In most cases, the precedence and associativity of operators are quite meaningful in themselves. If you have any doubts, you can always use this table as a reference. It is also best to use parentheses to make the code easier to understand.