4.1. If statements

if statements are the simplest type of conditional. They allow us to specify what should happen if a condition is true. For example, the following code prints "It is 5!" if the variable myNum is equal to 5:

let myNum = 5
//condition myNum==5 returns either true or false...
if myNum == 5 {
    //This code block executes if true...
    print("It is 5!")
}

Last updated

Was this helpful?