Programming in Swift: Fundamentals
Oct 19 2021 Swift 5.5, iOS 15, Xcode 13
Part 1: Core Concepts
3. Booleans & Comparison Operators
Update Notes: The student materials have been reviewed and are updated as of October 2021.
So far in this course, you’ve learned about a few types of variables:
In this exercise, you’ll learn about a another type in Swift: Booleans.
You might not realize it, but already you’ve used booleans if you built the Bull’s Eye app. You’ll see what I mean before the end of this video.
Let’s try using Booleans in Swift!
Start off by creating two Boolean constants; name the first one “yes”, and set it to “true”. Note here that it looks like I’m assigning a String value here, but “true” is a special term known as a reserved keyword in Swift that means what it says; it simply represents something that is true.
let yes: Bool = true
Name the second one “no” and set it to “false”. “False”, again, is a reserved keyword in Swift.
let no: Bool = false
Like with the other Swift types you’ve learned about, you can use type inference instead of declaring the type. If the value is “True” or “False”, the compiler will know it’s a Boolean.
let yes = true let no = false
You can see the results on the right stay the same, and if you Option-Click…
You can see that this is definitely still a bool.
That’s an example of setting a Boolean value, directly. You can also define Booleans using expressions!
Think back to when you were working on the Bull’s Eye app, where you wrote the code to calculate the difference between the slider value and the target value.
You can think of an expression as a unit of code that resolves to a value.
Either the difference is exactly equal to 0 - which means the expression is true, or the difference is not equal to 0, which means the expression is false.
Effectively, the result of the “difference == 0” expression is a temporary boolean value. If that expression evaluates to true, the part inside the if statement will execute. It it evaluates to false, then the part inside the if statement won’t execute.
To see this in action, you’ll set up a simple scenario with some students with different grades, and you’ll set up some Boolean variables depending on who passed — and who failed.
First, set up a constant, name it passingGrade, and set it to 50:
let passingGrade = 50
Next, create another constant, name it studentGrade, and set it to, say, 74:
let studentGrade = 74
Now, how can you determine if the student has passed, using Booleans?
Just as you did in the Bulls Eye app with other values, you can compare those two values to see if they are greater than or less than each other.
In this case, if a student’s mark is greater than the passing grade, then we consider that they have passed. So let’s set up an expression to represent that.
So, create another constant, name it studentPassed, and set its value to studentGrade is greater than passingGrade:
let studentPassed = studentGrade > passingGrade
Xcode tells you in the sidebar that “yes – this student passed”, since 74 is greater than 50, so it sets the value of studentPassed to “true”.
You can also test the reverse situation to see if the student failed; that is, if the students grade is less than the passing grade.
Create another constant, name it studentFailed, and set its value to studentGrade less than passingGrade:
let studentFailed = studentGrade < passingGrade
And there you see that the value of “studentFailed” is “false” - which means, no, this student didn’t fail.
But there’s a small narrow case here that we haven’t covered; what if the student got exactly 50?
Go back up to where you declared the constants and comment out the one where you set the student grade to 74. Below that, redeclare that constant and set it to 50.
let studentGrade = 74
Take a look now at your two constants studentPassed and studentFailed. They’re both false. Well, that can’t be right!
What you haven’t taken into account here is what happens when the studentGrade is exactly equal to the passing grade. In English, to determine if someone passed, you’d say “if studentGrade is more than or equal to passingGrade”, and Swift provides an operator just for that.
Comment out this line:
//let studentPassed = studentGrade > passingGrade
…and replace it with this slightly different line, where we declare a constant “studentPassed” and set its value to “studentGrade is less than or equal to passingGrade”:
let studentPassed = studentGrade >= passingGrade
Ah, there you go – now, if the student squeaks by with a 50, they still pass. Good for them!
There’s also an operator for “greater than or equal to”; you can use them wherever you need.
Now, you can also simply compare whether something is equal or not equal to each other. Let’s take the case of Sam and Chris, our two shiny new students, and give them each a grade.
Declare the constant chrisGrade and give Chris a 49:
let chrisGrade = 49
And declare another constant and give our star student, Sam, a 99:
let samGrade = 99
You’ve probably been used to thinking of the equals sign as this: “=”
But in Swift, like many other languages, that’s used to assign values, which is why I’ve tried not to say things like “chrisGrade equals 99” in this course. It’s actually known as the “assignment operator” in programming, not “the equals sign”.
To see if two things are equal in Swift, you use the “equality” operator instead, which is two equal signs together.
Let’s see this in action. Is samGrade equal to chrisGrade?
samGrade == chrisGrade
No, of course they’re not. Swift looks at the two values on either side of the equality operator, decides they are not equivalent, and returns “false” in this case.
Now, what if you wanted to check whether two things are not equal, or inequal? Swift has an operator for that as well: unsurprisingly called, the “inequality” operator, which is an exclamation mark, followed by an equals sign.
Let’s check if samGrade is not equal to chrisGrade.
samGrade != chrisGrade
Yes, of course they aren’t equal. It’s good to get in the habit of reading this as “not equal to” since, as you’ll see in later videos, the exclamation mark actually has another use where you’ll read it as “not”.
Swift can also compare things beside numbers – including Strings.
Declare two constants: catName, which takes the value of “Ozma”, and dogName, which takes the value of “Mango”.
let catName = "Ozma" let dogName = "Mango"
You can use those same equality and inequality operators on Strings.
Is catName equal to dogName?
catName == dogName
No! They’re not the same. Swift compares the contents of each String to see if they are the same or different, and in this case, they’re different.
Now, what about those greater than or less than operators? Yes, you can use those on Strings, too.
Here’s an example: check if catName is greater than dogName:
catName > dogName
When comparing Strings with less than or greater than operators, Swift checks “Does this string come before or after another String?” In this case, Ozma comes after Mango, alphabetically speaking, so this statement is true: Ozma is greater than Mango.
Comparison operators are something you’ll use throughout your programming career, so it’s a good idea to understand them really well.
And to that end: I’ve got a round of challenges for you, where you can practice using Booleans and comparison operators!