TTS Swift Notes: Basic Operators Flashcards

1
Q

What is an operator?

A

A special symbol or phrase that one uses to check, change, or combine values. EX: +, &&, ++i, etc

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

What are the arithmetic operators?

A

+, -, *, /, % (modulo)

  • 1 + 2 // equals 3
  • 5 - 3 // equals 2
  • 2 * 3 // equals 6
    10. 0 / 2.5 // equals 4.0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the two range operators?

A

a..<b></b>

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

What are the three kinds of operators?

A

Unary, binary, and ternary.

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

What do unary operators operate on?

A

A single target (EX: -a, !b, i++)

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

What do binary operators operate on?

A

Two targets (EX: 2 + 3)

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

What does the ternary operator operate on?

A

Three targets. (EX: a ? b : c)

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

What is an operand?

A

A value that an operator affects.

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

What does an assignment operator do?

A

The assignment operators (a = b) initializes or updates the value of a with the value of b.

  • let b = 10
  • var a = 5
  • a = b
  • // a is now equal to 10
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  • What is the symbol for the increment operator?
A

++

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

What is the symbol for the decrement operator?

A

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

The increment and decrement operators change the value by how many?

A

One

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  • ++i is shorthand for what?
A

i = i + 1

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

–i is shorthand for what?

A

i = i - 1

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

If the operator is written before the variable, it increments the variable _______ returning its value.

A

Before

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

If the operator is written after the variable, it increments the variable_______ returning its value.

A

After

17
Q

What is a unary minus operator?

A

The (-) prepended directly before the value it operators on, without any white space.

18
Q

What are examples of compound assignment operators?

A

+=, -=, =

19
Q

The expression “a += 2” is shorthand for what?

A

a = a + 2

The addition and assignment are combined into one operator that performs both tasks at the same time.

20
Q

Does the compound assignment operator return a value?

A

No.

21
Q

What are the six comparison operators?

A
Equal to (a == b)
     Not equal to (a != b)
     Greater than (a > b)
     Less than (a = b)
     Less than or equal to (a
22
Q

What are the two identity operators?

A

===

!==

23
Q

What do you use identity operators to test?

A

Whether two object references both refer to the same object instance.

24
Q

What do comparison operators return?

A

A Bool value.

25
Q

What are the three parts of the ternary conditional operator?

A

question ? answer1 : answer 2

26
Q

In the case of (a ?? b), what does a nil coalescing operator do?

A

It unwraps an optional a if it contains a value, or returns a default value b if a is nil.

27
Q

The nil coalescing operator (a ?? b) is shorthand for what?

A

a != nil ? a! : b

28
Q

In the case of a nil coalescing operator, if the value of a is non-nil, the value of b is not what?

A

Evaluated.

29
Q

What does a closed range operator look like?

A

(a…b)

30
Q

What does a half-open range operator look like?

A

(a..<b></b>

31
Q

A closed range operator includes what values?

A

A through B (i.e., all values)

32
Q

A half-open range operator does not include what value?

A

B (ie, all values but the last one)

33
Q

What are the three logical operators?

A
Logical NOT (!a)
      Logical AND (a &amp;&amp; b)
      Logical OR (a || b)
34
Q

What do logical operators do?

A

They modify or combine the Boolean logic values true and false.

35
Q

What does the logical NOT operator do?

A

It inverts a Boolean value so that true becomes false and vice versa.

36
Q

What does the logical AND operator do?

A

It creates logical expressions where both values must be true for the overall expression to also be true.

37
Q

What does the logical OR operator do?

A

It creates logical expressions in which only one of the two values has to be true for the overall expression to be true.

38
Q

Can you combine logical operators?

A

Yes.

  • if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword {
  • print(“Welcome!”)
  • } else {
  • print(“ACCESS DENIED”)
  • }

// prints “Welcome!”