7.8 Using if ... else … Statements
The next variation of the if statement allows us to also specify some code to perform if the expression in the if statement evaluates to false. The syntax for this construct is as follows:
if boolean expression {
// Code to be executed if expression is true
} else {
// Code to be executed if expression is false
}
Using the above syntax, we can now extend our previous example to display a different message if the comparison expression evaluates to be false:
let x = 10
if x > 9 {
print("x is greater than 9!")
} else {
print("x is less than 9!")
}
In this case, the second print statement would execute if the value of x was less than 9.