To get started, open your Swift Playgrounds app and create a New Book. You’ll be working with comparison operators and then putting them to work with if statements. Start with the following:
let doesOneEqualTwo = 1 == 2
let doesOneNotEqualTwo = 1 != 2
You should be able to guess the values of those two variables. Click the Run my Code button. This time, we aren’t printing anything out to the console, but notices the values along the right side of the playground. You can click them to expand them. To get rid of them, double click the False square, and select Remove.
Okay, those were easy, how about these?
let isOneGreaterThanTwo = 1 > 2
let isOneLessThanTwo = 1 < 2
Now run the code. Did you get them right?
Okay, often times you want to combine conditions. For instance, I may want to check if the user’s first name and last name match a particular name. For this, you have two compound operators known as And and Or. With And, both conditions must be true. For example:
let firstName = "Brian"
let lastName = "Moakley"
let and = firstName == "Brian" && lastName == "Moakley"
Run the code. It evaluates to true. Notice the double ampersand. This indicates that both conditions must be true. If the first condition is false, the second condition won’t even be evaluated. The Or indicates that just one condition must be true.
let or = firstName == "Jim" || lastName == "Moakley"
In this case, you are using double pipes. Run the code. The condition still evaluates to true even though the first condition is false. You can actually add more compound operators but it gets incredibly confusing. For example
let currency = 100
let confusing = firstName == "Jim" || lastName == "Moakley" && currency > 100
Take a moment to look at this. Can you guess the result? Don’t worry if you can’t. It’s confusing. You can add parenthesis to help.
let confusing = firstName == "Jim" || (lastName == "Moakley" && currency > 100)
In this case, my lastName equals Moakley, but my currency is not greater than 100. Let’s say, I wanted this to evaluate to true for 100 and more. You might want lower it to 99, but you can use a greater-than equals operator.
let confusing = firstName == "Jim" || (lastName == "Moakley" && currency >= 100)
And now, it evaluates to true. Okay, now for the actual if statement. We’re going to create an if statement that reviews animals. If should determine whether an animal is a house pet. Let’s set up an animal.
let animal = "Zebra"
Now for the actual if statement.
if animal == "Cat" || animal == "Dog" {
print("Animal is a house pet.")
}
Okay, this will print out that the animal is a house pet. You can also print out a message indicating that an animal is not a house pet. For that, you can use the else keyword.
if animal == "Cat" || animal == "Dog" {
print("Animal is a house pet.")
} else {
print("Animal is not a house pet.")
}
Now run the code. And we get the message we expect. What about animals that exist in a grey area like a snake. Update the code to the following:
if animal == "Cat" || animal == "Dog" {
print("Animal is a house pet.")
} else if animal == "Snake" {
print("Animals could be a house pet.")
} else {
print("Animal is not a house pet.")
}
Notice you are check another condition. You can create a whole series of animals. Also note, there can be only one else statement. For example,
if animal == "Cat" || animal == "Dog" {
print("Animal is a house pet.")
} else if animal == "Snake" {
print("Animals could be a house pet.")
} else {
print("Animal is not a house pet.")
} else {
print("Animal is not an animal")
}
This will cause an error. Delete the last else clause. If statements go deep. Get comfortable using them because you’ll be using them throughout your code.