The if statement
The first and most common way of controlling the flow of a program is through the
use of an if statement, which allows the program to do something only if a certain condition is true. For example, consider the following:
if 2 > 1 {
print("Yes, 2 is greater than 1.")
}
This is a simple if statement. If the condition is true, then the statement will execute the code between the braces. If the condition is false, then the statement won’t execute the code between the braces. It’s as simple as that!
You can extend an if statement to provide code to run in case the condition turns out to be false. This is known as the else clause. Here’s an example:
let animal = "Fox"
if animal == "Cat" || animal == "Dog" { print("Animal is a house pet.")
} else {
print("Animal is not a house pet.")
}
Here, if animal equals either "Cat" or "Dog", then the statement will run the first block of code. If animal does not equal either "Cat" or "Dog", then the statement will run the block inside the else part of the if statement, printing the following to the debug area:
Animal is not a house pet.
But you can go even further than that with if statements. Sometimes you want to check one condition, then another. This is where else-if comes into play, nesting another if statement in the else clause of a previous if statement.
You can use it like so:
let hourOfDay = 12 let timeOfDay: String
if hourOfDay < 6 {
timeOfDay = "Early morning"
} else if hourOfDay < 12 { timeOfDay = "Morning"
} else if hourOfDay < 17 { timeOfDay = "Afternoon"
} else if hourOfDay < 20 { timeOfDay = "Evening"
} else if hourOfDay < 24 { timeOfDay = "Late evening"
} else {
timeOfDay = "INVALID HOUR!"
}
print(timeOfDay)
These nested if statements test multiple conditions one by one until a true condition is found. Only the code associated with that first true condition is executed, regardless of whether subsequent else-if conditions are true. In other words, the order of your conditions matters!
You can add an else clause at the end to handle the case where none of the conditions are true. This else clause is optional if you don’t need it; in this example you do need it, to ensure that timeOfDay has a valid value by the time you print it out.
In this example, the if statement takes a number representing an hour of the day and converts it to a string representing the part of the day to which the hour belongs. Working with a 24-hour clock, the statements are checked one-by-one in order:
- The first check is to see if the hour is less than 6. If so, that means it’s early morning.
- If the hour is not less than 6, the statement continues to the first else-if, where it checks the hour to see if it’s less than 12.
- Then in turn, as conditions prove false, the statement checks the hour to see if it’s less than 17, then less than 20, then less than 24.
- Finally, if the hour is out of range, the statement prints that information to the console.
In the code above, the hourOfDay constant is 12. Therefore, the code will print the following:
Afternoon
Notice that even though both the hourOfDay < 20 and hourOfDay < 24 conditions are also true, the statement only executes the first block whose condition is true; in this case, the block with the hourOfDay < 17 condition.