Swift Operator Precedence

In this tutorial you will learn about the Swift Operator Precedence and its application with practical example.

Swift Operator Precedence

Operator precedence defines the order in which given mathematical expression is evaluated. When an expression includes multiple operators then each of the single part of given expression is evaluated in a certain order following some rules defined as per operator precedence.Operator with higher precedence is evaluated first and operator with lowest precedence is evaluated at last.

Following is swift operator precedence table –

Swift Operator Precedence (Highest to Lowest)
Operator Groups Examples
Bitwise shift precedence >> &<< &>> >>
Multiplication precedence &* % & * /
Addition precedence | &+ &- + – ^
Range Formation Precedence ..< …
Casting Precedence is as
Nil-Coalescing Precedence ??
Comparison Precedence != > < >= <= === ==
Logical Conjunction Precedence &&
Logical Disjunction Precedence ||
Default Precedence ~>
Ternary Precedence ?:
Function Arrow precedence ( )
Assignment Precedence |= %= /= &<<= &>>= &= *= >>= <<= ^= += -=

Operator Associativity

Operators with same precedence follows operator associativity defined for its operator group. In Swift, operators can either follow left-associative, right-associative or have no associativity. Operators with left-associative are evaluated from the left to right, operators with right-associative are evaluated from right to the left and operators with no associativity, does not follow any predefined order.

Swift Operator Associativity Table (Highest to Lowest)
Operator Groups Examples Associativity
Bitwise shift precedence >> &<< &>> >> none
Multiplication precedence &* % & * / left
Addition precedence | &+ &- + – ^ left
Range Formation Precedence ..< … none
Casting Precedence is as none
Nil-Coalescing Precedence ?? right
Comparison Precedence != > < >= <= === == none
Logical Conjunction Precedence && left
Logical Disjunction Precedence || left
Default Precedence ~> none
Ternary Precedence ?: right
Function Arrow precedence ( ) right
Assignment Precedence |= %= /= &<<= &>>= &= *= >>= <<= ^= right
In this tutorial we have learn about the Swift Operator Precedence and its application with practical example. I hope you will like this tutorial.