Expressions Flashcards
What kinds of expressions are there in Swift?
In Swift, there are four kinds of expressions: prefix expressions, binary expressions, primary expressions, and postfix expressions.
Prefix and binary expressions let you apply operators to smaller expressions.
Primary expressions are conceptually the simplest kind of expression, and they provide a way to access values.
Postfix expressions, like prefix and binary expressions, let you build up more complex expressions using postfixes such as function calls and member access.
What does evaluating an expression do?
Evaluating an expression returns a value, causes a side effect, or both.
Name the prefix operators defined by the Swift standard library.
\++ Increment -- Decrement ! Logical NOT ~ Bitwise NOT \+ Unary plus - Unary minus
What other prefix operator is defined by the Swift language?
In addition to the standard library operators listed above, you use & immediately before the name of a variable that’s being passed as an in-out argument to a function call expression.
What exponentiative binary operators does the Swift standard library provide?
«_space;Bitwise left shift
» Bitwise right shift
(No associativity, precedence level 160)
What multiplicative binary operators does the Swift standard library provide?
* Multiply / Divide % Remainder &* Multiply, ignoring overflow &/ Divide, ignoring overflow &% Remainder, ignoring overflow & Bitwise AND
(Left associative, precedence level 150)
What additive binary operators does the Swift standard library provide?
\+ Add - Subtract &+ Add with overflow &- Subtract with overflow | Bitwise OR ^ Bitwise XOR
(Left associative, precedence level 140)
What range binary operators does the Swift standard library provide?
.. Half-closed range
… Closed range
(No associativity, precedence level 135)
What cast binary operators does the Swift standard library provide?
is Type check
as Type cast
(No associativity, precedence level 132)
What comparative binary operators does the Swift standard library provide?
< Less than Greater than >= Greater than or equal == Equal != Not equal === Identical !== Not identical ~= Pattern match
(No associativity, precedence level 130)
What conjunctive binary operator does the Swift standard library provide?
&& Logical AND
Left associative, precedence level 120
What disjunctive binary operator does the Swift standard library provide?
|| Logical OR
Left associative, precedence level 110
What assignment binary operators does the Swift standard library provide?
= Assign *= Multiply and assign /= Divide and assign %= Remainder and assign \+= Add and assign -= Subtract and assign <>= Right bit shift and assign &= Bitwise AND and assign ^= Bitwise XOR and assign |= Bitwise OR and assign &&= Logical AND and assign ||= Logical OR and assign
(Right associative, precedence level 90)
What ternary operator does the Swift standard library provide?
?: Ternary conditional
Right associative, precedence level 100
How are binary operator precedence rules applied?
At parse time, an expression made up of binary operators is represented as a flat list. This list is transformed into a tree by applying operator precedence
For example, the expression 2 + 3 * 5 is initially understood as a flat list of five items, 2, +, 3, *, and 5.
This process transforms it into the tree (2 + (3 * 5)).
Which binary operator is this?
~=
Pattern match
Category: Comparative
No associativity
Precedence level 130
Which binary operator is this?
||
Logical OR
Category: Disjunctive
Left associative
Precedence level 110
Which binary operator is this?
*=
Multiply and assign
Category: Assignment
Right associative
Precedence level 90
Which binary operator is this?
&-
Subtract with overflow
Category: Additive
Left associative
Precedence level 140
Which binary operator is this?
> =
Greater than or equal
Category: Comparative
No associativity
Precedence level 130
Which binary operator is this?
&=
Bitwise AND and assign
Category: Assignment
Right associative
Precedence level 90
Which binary operator is this?
/=
Divide and assign
Category: Assignment
Right associative
Precedence level 90
Which binary operator is this?
^=
Bitwise XOR and assign
Category: Assignment
Right associative
Precedence level 90
Which binary operator is this?
-=
Subtract and assign
Category: Assignment
Right associative
Precedence level 90