TTS Swift Notes: Basic Operators Flashcards
What is an operator?
A special symbol or phrase that one uses to check, change, or combine values. EX: +, &&, ++i, etc
What are the arithmetic operators?
+, -, *, /, % (modulo)
- 1 + 2 // equals 3
- 5 - 3 // equals 2
- 2 * 3 // equals 6
10. 0 / 2.5 // equals 4.0
What are the two range operators?
a..<b></b>
What are the three kinds of operators?
Unary, binary, and ternary.
What do unary operators operate on?
A single target (EX: -a, !b, i++)
What do binary operators operate on?
Two targets (EX: 2 + 3)
What does the ternary operator operate on?
Three targets. (EX: a ? b : c)
What is an operand?
A value that an operator affects.
What does an assignment operator do?
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
- What is the symbol for the increment operator?
++
What is the symbol for the decrement operator?
–
The increment and decrement operators change the value by how many?
One
- ++i is shorthand for what?
i = i + 1
–i is shorthand for what?
i = i - 1
If the operator is written before the variable, it increments the variable _______ returning its value.
Before
If the operator is written after the variable, it increments the variable_______ returning its value.
After
What is a unary minus operator?
The (-) prepended directly before the value it operators on, without any white space.
What are examples of compound assignment operators?
+=, -=, =
The expression “a += 2” is shorthand for what?
a = a + 2
The addition and assignment are combined into one operator that performs both tasks at the same time.
Does the compound assignment operator return a value?
No.
What are the six comparison operators?
Equal to (a == b) Not equal to (a != b) Greater than (a > b) Less than (a = b) Less than or equal to (a
What are the two identity operators?
===
!==
What do you use identity operators to test?
Whether two object references both refer to the same object instance.
What do comparison operators return?
A Bool value.
What are the three parts of the ternary conditional operator?
question ? answer1 : answer 2
In the case of (a ?? b), what does a nil coalescing operator do?
It unwraps an optional a if it contains a value, or returns a default value b if a is nil.
The nil coalescing operator (a ?? b) is shorthand for what?
a != nil ? a! : b
In the case of a nil coalescing operator, if the value of a is non-nil, the value of b is not what?
Evaluated.
What does a closed range operator look like?
(a…b)
What does a half-open range operator look like?
(a..<b></b>
A closed range operator includes what values?
A through B (i.e., all values)
A half-open range operator does not include what value?
B (ie, all values but the last one)
What are the three logical operators?
Logical NOT (!a) Logical AND (a && b) Logical OR (a || b)
What do logical operators do?
They modify or combine the Boolean logic values true and false.
What does the logical NOT operator do?
It inverts a Boolean value so that true becomes false and vice versa.
What does the logical AND operator do?
It creates logical expressions where both values must be true for the overall expression to also be true.
What does the logical OR operator do?
It creates logical expressions in which only one of the two values has to be true for the overall expression to be true.
Can you combine logical operators?
Yes.
- if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword {
- print(“Welcome!”)
- } else {
- print(“ACCESS DENIED”)
- }
// prints “Welcome!”