Swift Operators Precedence and Associativity

This tutorial belongs to the Swift series

Think about this expression:
let amount = 1 + 2 * 3
The value of amount could drastically change depending if 1 + 2 is calculated before 2 * 3.

The order of calculation is determined by the operato…


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

This tutorial belongs to the Swift series

Think about this expression:

let amount = 1 + 2 * 3

The value of amount could drastically change depending if 1 + 2 is calculated before 2 * 3.

The order of calculation is determined by the operator precedence. From higher precedence to lower precedence, as for the most popular operators we have:

  • Multiplication (*), division (/), remainder (%)
  • Add (+), subtract (-)
  • Comparisons (==, !=, <, >, <=, >=)
  • Logical AND (&&) and OR (||)
  • Ternary conditional (?:)
  • Assignment and compound assignment operators (=, += and so on)

This means that the above expression is resolved first calculating the multiplication, and then the sum:

let amount = 1 + 2 * 3 // = 7

The full table of precedence, more complicated, is available at https://developer.apple.com/documentation/swift/swift_standard_library/operator_declarations.

When inside an expression you have multiple operators with the same precedence, we make use of the operator associativity. Associativity is a property we use to determine which operation has priority when the precedence is the same.

For example, consider this:

let amount = 4 / 2 * 5

Depending if we first execute 4 / 2 or 2 * 5, the result could be 10 or 0,4.

Associativity solves this. Multiplication is left associative, so we must first execute the expression on the left. Parentheses help us figure this out:

let amount = (4 / 2) * 5

Multiplication (*), division (/), remainder (%), add (+), subtract (-), logical AND (&&), logical OR (||) are left associative

Assignment and compound assignment operators (=, += and so on) and the ternary conditional (?:) are right associative

Comparisons (==, !=, <, >, <=, >=) don’t have associativity.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-06-13T05:00:00+00:00) Swift Operators Precedence and Associativity. Retrieved from https://www.scien.cx/2021/06/13/swift-operators-precedence-and-associativity/

MLA
" » Swift Operators Precedence and Associativity." flaviocopes.com | Sciencx - Sunday June 13, 2021, https://www.scien.cx/2021/06/13/swift-operators-precedence-and-associativity/
HARVARD
flaviocopes.com | Sciencx Sunday June 13, 2021 » Swift Operators Precedence and Associativity., viewed ,<https://www.scien.cx/2021/06/13/swift-operators-precedence-and-associativity/>
VANCOUVER
flaviocopes.com | Sciencx - » Swift Operators Precedence and Associativity. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/13/swift-operators-precedence-and-associativity/
CHICAGO
" » Swift Operators Precedence and Associativity." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/06/13/swift-operators-precedence-and-associativity/
IEEE
" » Swift Operators Precedence and Associativity." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/06/13/swift-operators-precedence-and-associativity/. [Accessed: ]
rf:citation
» Swift Operators Precedence and Associativity | flaviocopes.com | Sciencx | https://www.scien.cx/2021/06/13/swift-operators-precedence-and-associativity/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.