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.