if 5 > 6 {
print("5 is more than 6")
} else {
print("5 is not more than 6")
}
// Output: "5 is not more than 6"
5 > 6 ? print("5 is more than 6")
: print("5 is not more than 6")
// Output: "5 is not more than 6"
func greet(name: String?) {
if let unwrappedName = name {
print("Hello \(unwrappedName)!")
} else {
print("Hello guest!")
}
}
greet(name: "Asma")
greet(name: nil)
// Output:
// Hello Asma!
// Hello guest!
func greet(name: String?) {
let unwrappedName = name ?? "guest"
print("Hello \(unwrappedName)!")
}
greet(name: "Asma")
greet(name: nil)
// Output:
// Hello Asma!
// Hello guest!
Guard statements can be used to reduce indentation on the happy path. In a guard statement, the else branch must transfer control to exit the code block containing the guard statement. This can be done with return, break, continue, or throw.
func divide(x: Int, y: Int) -> Int? {
guard y != 0 else {
print("You cannot divide by 0.")
return nil
}
let answer = x / y
return answer
}
print(divide(x: 5, y: 0))
// Output:
// You cannot divide by 0.
// nil
func greet(name: String?) {
guard let unwrapped = name else {
print("Hello guest!")
return
}
print("Hello \(unwrapped)!")
}
greet(name: "Asma")
greet(name: nil)
// Output:
// Hello Asma!
// Hello guest!
The common for loop for (i = a; i < b; i++)
does not exist in Swift. Instead, the for-in can be modified to have an index using tuples and enumerated:
let birds = ["Owl", "Crane"]
for bird in birds {
print(bird)
}
// Output:
// Owl
// Crane
let birds = ["Owl", "Crane"]
for (i, bird) in birds.enumerated() {
print("[\(i)]: \(bird)")
}
// Output:
// [0]: Owl
// [1]: Crane
A while loop will run the code block each time the conditional is true. A repeat-while loop will run the block first without checking the conditional, then keep on running it as long as the conditional is true.
var steps = 2
while steps > 0 {
print("\(steps) steps left")
steps -= 1
}
// Output:
// 2 steps left
// 1 steps left
var steps = -999
while steps > 0 {
print("\(steps) steps left")
steps -= 1
}
// Output: nothing
var steps = 2
repeat {
print("\(steps) steps left")
steps -= 1
} while steps > 0
// Output:
// 2 steps left
// 1 steps left
var steps = -999
repeat {
print("\(steps) steps left")
steps -= 1
} while steps > 0
// Output: "-999 steps left"
A default
case is necessary when not all cases are covered.
func describe(animal: String) {
switch animal {
case "Owl", "Crane": print("A bird")
case "Lion": print("A feline")
case "Ant": print("An insect")
default: print("Something else")
}
}
describe(animal: "Owl")
describe(animal: "Giraffe")
// Output:
// "A bird"
// "Something else"
Here, all cases are covered, so a default
case is unnecessary.
func describe(point: (Int, Int)) {
switch point {
case (0, 0): print("At origin")
case (_, 0): print("On x-axis")
case (0, _): print("On y-axis")
case (_, _): print("Elsewhere")
}
}
describe(point: (5, 0))
describe(point: (11, 9))
// Output:
// "On x-axis"
// "Elsewhere"