Expressions Flashcards

(62 cards)

1
Q

What kinds of expressions are there in Swift?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does evaluating an expression do?

A

Evaluating an expression returns a value, causes a side effect, or both.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Name the prefix operators defined by the Swift standard library.

A
\++ Increment
-- Decrement
! Logical NOT
~ Bitwise NOT
\+ Unary plus
- Unary minus
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What other prefix operator is defined by the Swift language?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What exponentiative binary operators does the Swift standard library provide?

A

&laquo_space;Bitwise left shift
» Bitwise right shift

(No associativity, precedence level 160)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What multiplicative binary operators does the Swift standard library provide?

A
* Multiply
/ Divide
% Remainder
&* Multiply, ignoring overflow
&/ Divide, ignoring overflow
&% Remainder, ignoring overflow
& Bitwise AND

(Left associative, precedence level 150)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What additive binary operators does the Swift standard library provide?

A
\+ Add
- Subtract
&+ Add with overflow
&- Subtract with overflow
| Bitwise OR
^ Bitwise XOR

(Left associative, precedence level 140)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What range binary operators does the Swift standard library provide?

A

.. Half-closed range
… Closed range

(No associativity, precedence level 135)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What cast binary operators does the Swift standard library provide?

A

is Type check
as Type cast

(No associativity, precedence level 132)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What comparative binary operators does the Swift standard library provide?

A
< Less than
 Greater than
>= Greater than or equal
== Equal
!= Not equal
=== Identical
!== Not identical
~= Pattern match

(No associativity, precedence level 130)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What conjunctive binary operator does the Swift standard library provide?

A

&& Logical AND

Left associative, precedence level 120

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What disjunctive binary operator does the Swift standard library provide?

A

|| Logical OR

Left associative, precedence level 110

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What assignment binary operators does the Swift standard library provide?

A
= Assign
*= Multiply and assign
/= Divide and assign
%= Remainder and assign
\+= Add and assign
-= Subtract and assign
<>= Right bit shift and assign
&amp;= Bitwise AND and assign
^= Bitwise XOR and assign
|= Bitwise OR and assign
&amp;&amp;= Logical AND and assign
||= Logical OR and assign

(Right associative, precedence level 90)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What ternary operator does the Swift standard library provide?

A

?: Ternary conditional

Right associative, precedence level 100

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How are binary operator precedence rules applied?

A

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)).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which binary operator is this?

~=

A

Pattern match

Category: Comparative
No associativity
Precedence level 130

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Which binary operator is this?

||

A

Logical OR

Category: Disjunctive
Left associative
Precedence level 110

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Which binary operator is this?

*=

A

Multiply and assign

Category: Assignment
Right associative
Precedence level 90

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Which binary operator is this?

&-

A

Subtract with overflow

Category: Additive
Left associative
Precedence level 140

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Which binary operator is this?

> =

A

Greater than or equal

Category: Comparative
No associativity
Precedence level 130

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Which binary operator is this?

&=

A

Bitwise AND and assign

Category: Assignment
Right associative
Precedence level 90

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Which binary operator is this?

/=

A

Divide and assign

Category: Assignment
Right associative
Precedence level 90

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Which binary operator is this?

^=

A

Bitwise XOR and assign

Category: Assignment
Right associative
Precedence level 90

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Which binary operator is this?

-=

A

Subtract and assign

Category: Assignment
Right associative
Precedence level 90

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Which binary operator is this? ==
Equal Category: Comparative No associativity Precedence level 130
26
Which binary operator is this? &*
Multiply, ignoring overflow Category: Multiplicative Left associative Precedence level 150
27
Which binary operator is this? !==
Not identical Category: Comparative No associativity Precedence level 130
28
Which binary operator is this? is
Type check Category: Cast No associativity Precedence level 132
29
Which binary operator is this? >>
Bitwise right shift Category: Exponentiative No associativity Precedence level 160
30
Which binary operator is this? ..
Half-closed range Category: Range No associativity Precedence level 135
31
Which binary operator is this? %
Remainder Category: Multiplicative Left associative Precedence level 150
32
Which binary operator is this? !=
Not Equal Category: Comparative No associativity Precedence level 130
33
Which binary operator is this? *
Multiply Category: Multiplicative Left associative Precedence level 150
34
Which binary operator is this? >>=
Right bit shift and assign Category: Assignment Right associative Precedence level 90
35
Which binary operator is this? <<=
Left bit shift and assign Category: Assignment Right associative Precedence level 90
36
Which binary operator is this? -
Subtract Category: Additive Left associative Precedence level 140
37
Which binary operator is this? /
Divide Category: Multiplicative Left associative Precedence level 150
38
Which binary operator is this?
Less than Category: Comparative No associativity Precedence level 130
39
Which binary operator is this? +
Add Category: Additive Left associative Precedence level 140
40
Which binary operator is this? ===
Identical Category: Comparative No associativity Precedence level 130
41
Which binary operator is this? ^
Bitwise XOR Category: Additive Left associative Precedence level 140
42
Which binary operator is this? <=
Less than or equal Category: Comparative No associativity Precedence level 130
43
Which binary operator is this? &&=
Logical AND and assign Category: Assignment Right associative Precedence level 90
44
Which binary operator is this? &+
Add with overflow Category: Additive Left associative Precedence level 140
45
Which binary operator is this? %=
Remainder and assign Category: Assignment Right associative Precedence level 90
46
Which binary operator is this? |=
Bitwise OR and assign Category: Assignment Right associative Precedence level 90
47
Which binary operator is this? &/
Divide, ignoring overflow Category: Multiplicative Left associative Precedence level 150
48
Which binary operator is this? &
Bitwise AND Category: Multiplicative Left associative Precedence level 150
49
Which binary operator is this? ...
Closed range Category: Range No associativity Precedence level 135
50
Which binary operator is this? <
Bitwise left shift Category: Exponentiative No associativity Precedence level 160
51
Which binary operator is this? >
Greater than Category: Comparative No associativity Precedence level 130
52
Which binary operator is this? |
Bitwise OR Category: Additive Left associative Precedence level 140
53
Which binary operator is this? &%
Remainder, ignoring overflow Category: Multiplicative Left associative Precedence level 150
54
Which binary operator is this? =
Assign Category: Assignment Right associative Precedence level 90
55
Which binary operator is this? &&
Logical AND Category: Conjunctive Left associative Precedence level 120
56
Which binary operator is this? as
Type cast Category: Cast No associativity Precedence level 132
57
Which binary operator is this? ||=
Logical OR and assign Category: Assignment Right associative Precedence level 90
58
Which binary operator is this? +=
Add and assign Category: Assignment Right associative Precedence level 90
59
What are the rules for the assignment operator? =
The assigment operator sets a new value for a given expression. It has the following form: expression = value The value of the expression is set to the value obtained by evaluating the value. If the expression is a tuple, the value must be a tuple with the same number of elements. (Nested tuples are allowed.) Assignment is performed from each part of the value to the corresponding part of the expression. For example: ``` (a, _, (b, c)) = ("test", 9.45, (12, 3)) //a is "test", b is 12, c is 3, and 9.45 is ignored ``` The assignment operator does not return any value.
60
What are the rules for the ternary operator? ? :
The ternary conditional operator evaluates to one of two given values based on the value of a condition. It has the following form: condition ? a : b If the condition evaluates to true, the conditional operator evaluates the first expression and returns its value. Otherwise, it evaluates the second expression and returns its value. The unused expression is not evaluated.
61
What are the rules for the typecasting operator? as
If conversion to the specified type is guaranteed to succeed, the value of the expression is returned as an instance of the specified type. If conversion to the specified type is guaranteed to fail, a compile-time error is raised. Otherwise, if it’s not known at compile time whether the conversion will succeed, the type of the cast expresion is an optional of the specified type. At runtime, if the cast succeeds, the value of expression is wrapped in an optional and returned; otherwise, the value returned is nil.
62
What are the rules for the type-testing operator? is
The is operator checks at runtime to see whether the expression is of the specified type. If so, it returns true; otherwise, it returns false. The check must not be known to be true or false at compile time.