Notes Flashcards
Constant Boolean
It’s smart to use constants in general because they don’t allocate as much space and you don’t have to keep track of them but it does seem like a Boolean would need to have the ability to change.
One good use of a constant Boolean is in a situation where you have to calculate something and only care about if it meets a requirement.
Ex. I want to know if today is a Sunday and if the temperature is greater than 70 degrees. You could then use that constant Boolean in other places. I could probably find some examples
Ternary Operations
Let’s make it simple:
a ? b : c - you can read as “ if (?) a is true, then b, else (:) c “
or in some other case,
(a<5) ? b : c - you can read as “ if (?) a is less then 5, then b, else (:) c “
an example is name=age>12?”teen”:”child” which sets name to “teen” if age is >12 and “child” otherwise. they can be string together, meaning a=b?c:d?e:f is valid.
!
”!” reads “not” and inverts a Boolean value, so if it was true it makes it false and if it was false it makes it true
i++
i++ is the same as i+=1 or i=I+1
a && b
Both have to be true
a || b
One has to be true
Optionals
In Swift if you want to create a variable without assigning an initial value (when it is declared), it has to be created as “optional”. code, its value automatically defaults to nil, unless an initial default value is provided.
Switch
Use the switch statement as an alternative to the if statement for multiple potential states.
switch distance { case 0: print("not a valid distance") case 1,2,3,4,5: print("near") default: print("too far") }
Every switch statement must be exhaustive, i.e. take every possible value into consideration
ou can define a default catch-all case to cover any values that are not explicitly addressed. Indicate the catch-all case by using the keyword default. This always appears last.
Repeat-While
The repeat-while loop is the alternate while loop. It first makes a single pass through the loop block, then considers the loop’s condition, and repeats the loop until the condition shows as false.
repeat {
x -= 1
} while x > 0
The for-in Loop
Use the for-in loop to iterate over a sequence, such as ranges of numbers, items in an array, or characters in a string.
The following example prints the first few entries in the five-times-table:
for index in 1...5 { print("\(index) times 5 is \(index * 5)") } // 1 times 5 is 5 // 2 times 5 is 10 // 3 times 5 is 15 // 4 times 5 is 20 // 5 times 5 is 25
Control Transfer
Control transfer statements alter the code execution by transferring control from one piece of code to another. Swift’s four control transfer statements are continue, break, fallthrough, and return (which will be discussed in the upcoming lessons
Continue
The continue statement stops the loop, then restarts it at the beginning of its next cycle.
The example below shows how to use the continue statement to skip over even numbers.
for num in 1...10 { if num%2 == 0 { continue } print(num) }
Break
Use the break statement to immediately end the execution of an entire control flow statement. Also, the break statement is used within a switch statement or a loop statement to terminate its execution sooner than would otherwise be the case.
Break in a Loop Statement
When a break statement is used within a loop statement, the loop’s execution immediately stops. Control transfers to the first line of code following the loop’s closing brace (}). The current iteration’s remaining code is skipped, and no further iterations of the loop are initiated.
For example, you can have a loop that breaks out when the value of a becomes less than that of b:
var b = 7 var a = 10 while a > 0 { if(a < b) { break } a-=1 }
Break in a Switch Statement
A break causes a switch statement to end its execution immediately, and transfers control to the first line of code that follows the switch statement’s closing brace (}).
var a = 5 var letter = "X" switch a { case 1: letter = "A" case 2: letter = "B" default: break }