Conditions
How to check a condition is true or false
Basic syntax
if someCondition { print("Do something") }
Braces signal the start and end of a code block.
let score = 65 if score > 80 { // won't run print("Great job!") }
Another example
let speed = 88 let percentage = 85 let age = 18 if speed >= 88 { print("Where we're going, we don't need roads.") } if percentage < 85 { print("Sorry you failed the test.") } if age >= 18 { print("You're eligible to vote") }
Another example
let ourName = "Dave Lister" let friendName = "Arnold Rimmer" if ourName < friendName { print("It's \(ourName) vs \(friendName)") } if ourName > friendName { print("It's \(friendName) vs \(ourName)") }
Another example
var numbers = [1,2,3] numbers.append(4) if numbers.count > 3 { numbers.remove(at: 0) } print(numbers)
String comparison
== means is equal to != means is not equal to
Example
let country = "Canada" if country == "Australia" { print("G'day mate") } let name = "Taylor Swift" if name != "Anonymous" { print("Welcome \(name)") } var username = "@taylorswift123" if username == "" { username = "Anonymous" } print("Welcome, \(username)")
Other ways to check truthiness
var username = "@taylorswift123" if username.isEmtpy == true { username = "Anonymous" } // if username.isEmtpy { username = "Anonymous" } print("Welcome, \(username)")
Note:
With enum types order is important. so:
enum sizes: Comparable { case small case medium case large } let a = sizes.small let b = .large // This works because small comes before large in the enum list if a < b { print("a is smaller than b") }
How to check multiple conditions
We want to be able to control the program flow whether something is true or false.
We can do so w the else
keyword. Ex:
let age = 16 if age >= 18 { print("vote") } else { print("too young") }
We can use else if
for multiple branches of code. Ex:
if a { print("a") } else if b { print("b") } else { print("default answer") }
We can check multiple conditions with the &&
operator. Ex:
if a && b { print("a and b are true") }
We can check whether one or another condition are true with the ||
operator. Ex:
if a || b { print("a or b is true") }
Complex conditions! Ex:
enum TransportOption { case airplane, helicopter, bicycle, car, escooter } // set transport to a case in the TransportOption enum let transport = TransportOption.airplane // use shorthand if transport == .airplane || transport == .helicopter { print ("flying") } else if transport == .bicycle { print("riding") } else if transport == .car { print("driving") } else { print("riding a scooter") }
Thought:
Parentheses are optional but should be used to clarify complex conditionals. Complex meaning multiple conjunctions using
&&
or||
Scored 9/12 on Conditions. Botched first 2 questions and missed the 5th.
Scored 12/12 on combining conditions.
How to use the switch statements to check multiple conditions
We use switch statements to simplify if/else
logic. Ex:
// Given: enum Weather { case .sun case .wind case .rain case .snow } switch forecast { case .sun: print("its sunny") case .wind: print("its windy") case .rain: print("its rainy") case .snow: print("its snowing") case .unknown print("what's happening outside?") }
Caveats to using a switch:
- must have a case for each option, must be exhaustive
- Swift will exit the switch block early if a case is matched
- must provide a
default
case. Default case must be last because default run’s first and makes the switch statement pointless. - The default case is not needed in the case of checking enums
Using fallthrough
we can continue checking cases even if we have matched a case early. Ex:
let day = 5 switch day { case 5: print("5 golden rings") fallthrough case 4: print("4 calling birds") fallthrough case 3: print("3 french hens") fallthrough case 2: print("2 turtle oves") fallthrough default: print("And a partridge in a pear tree") }
Scored 5/6 on switch statements. Missed q5 re: not needing a default cases when using a switch on an enum
data type.
How to use the ternary operator for quick tests
Binary operators operate on 2 pieces of data. Ex:
2 + 5
Ternary operator works on 3 pieces of data. Ex:
let age = 18 let canVote = age >= 18 ? "Yes" : "No"
It is identical to an if/else
statement
Ternaries include
- what we are checking
- what to do if the condition is true
- what to do if the condition is false
Ex:
let hour = 23 print(hour < 12 ? "before noon" : "after noon") let names = ["a", "b", "c"] let crewCount = names.isEmpty ? "No one" : "\(names.count) people" enum Theme { case light, dark } let theme = Theme.dark let background = theme == .dark ? "black" : "white" print(background)
Why ternaries?
- they allow us to have an expression evaluate within methods
- conciseness
Scored 12/12 on Ternary