Swift Tutorial Swift References

Swift - Operators Precedence



Swift Operators Precedence

Operator precedence (order of operations) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.

For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 52 = 28 and 3 × 52 = 75.

Operator Associativity

Operator associativity is the direction in which an expression is evaluated. For example:

var x = 10
var y = 20

//associativity of = operator is
//Left to Right, hence x will become 20
x = y

As the associativity of = is left to right. Hence x is assigned the value of y.

Example:

The example below illustrates the operator precedence in Swift.

//evaluates 5 * 2 first
var retval1 = 15 - 5 * 2;

//above expression is equivalent to
var retval2 = 15 - (5 * 2);

//forcing compiler to evaluate 15 - 5 first
var retval3 = (15 - 5) * 2;

print("15 - 5 * 2 = \(retval1)")
print("15 - (5 * 2) = \(retval2)")
print("(15 - 5) * 2 = \(retval3)")

The output of the above code will be:

15 - 5 * 2 = 5
15 - (5 * 2) = 5
(15 - 5) * 2 = 20

Swift Operators Precedence Table

Swift has predefined - prefix, postfix and infix operators. The prefix operators are as follows:

  • ! Logical NOT
  • .! Pointwise logical NOT
  • ~ Bitwise NOT
  • + Unary plus
  • - Unary minus
  • ..< Half-open range
  • ... Closed range

The postfix operators are as follows:

  • ... Range

The following table lists the precedence and associativity of Swift infix operators. Operators are listed top to bottom, in descending precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. When operators have the same precedence, associativity of the operators determines the order in which the operations are performed.

PrecedenceOperatorDescriptionAssociativity
16<<  >>Bitwise left shift and right shiftnon-associative
&<<  &>>Bitwise left shift and right shift, ignoring overflow
15*  /  %Multiplication, Division, RemainderLeft to Right
&Bitwise AND
&*Multiply with overflow
14+  -Addition, Subtraction
|  ^Bitwise OR, Bitwise XOR
&+  &-Add with overflow, Subtract with overflow
13..<  ...Half-open range, Closed rangenon-associative
12isType checkLeft to Right
as  as?  as!Type cast
11??Nil coalescing operatorRight to Left
10<  <=  >  >=Less than, Less than or equal, Greater than, and Greater than or equalnon-associative
9==  !=Equality and Inequality
8===  !==Identical and Not identical
7~=Pattern match
6.<  .<=  .>  .>=Pointwise less than, Pointwise less than or equal, Pointwise greater than, and Pointwise greater than or equal
5.==  .!=Pointwise equality and Pointwise inequality
4&&  .&Logical AND, Pointwise bitwise ANDLeft to Right
3||  .|  .^Logical OR, Pointwise bitwise OR, Pointwise bitwise XOR
2a?b:cConditional (ternary) operatorRight to Left
1=Direct assignment
+=  -=  *=  /=  %=Compound assignment by sum, difference, product, quotient and remainder
<<=  >>=Compound assignment by Bitwise left shift and right shift
&=  ^=  |=Compound assignment by Bitwise AND, XOR and OR
&+=  &-=  &*=Add, Subtract and Multiply with overflow and assign
&<<=  &>>=Assign by Left bit shift and Right bit shift ignoring overflow
.&=  .|=  .^=Assign by Pointwise bitwise AND, Pointwise bitwise OR, Pointwise bitwise XOR

❮ Swift - Operators