7.9 Using if ... else if ... Statements
So far we have looked at if statements which make decisions based on the result of a single logical expression. Sometimes it becomes necessary to make decisions based on a number of different criteria. For this purpose, we can use the if ... else if ... construct, an example of which is as follows:
let x = 9;
if x == 10 {
print("x is 10")
} else if x == 9 {
print("x is 9")
} else if x == 8 {
print("x is 8")
}
This approach works well for a moderate number of comparisons but can become cumbersome for a larger volume of expression evaluations. For such situations, the Swift switch statement provides a more flexible and efficient solution. For more details on using the switch statement refer to the next chapter entitled “The Swift...