Programming in Swift: Fundamentals

Oct 19 2021 · Swift 5.5, iOS 15, Xcode 13

Part 1: Core Concepts

03. Booleans & Comparison Operators

Episode complete

Play next episode

About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 02. Swift Playgrounds & Comments Next episode: 04. Challenge: Booleans

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 03. Booleans & Comparison Operators

Update Notes: The student materials have been reviewed and are updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

So far in this course, you've learned about a few types of variables. Integers, which represent whole numbers, like 1 and 2 and 3. Doubles, which represent decimal numbers, like 3.14. And strings, which represent texts like "Hi there!" In this exercise, you'll learn about another type in Swift; booleans. So booleans, or bools for short, can only have two possible values: "true" or "false". You might not realize it, but you've already used booleans if you built the Bullseye app. You'll see what I mean before the end of this video. Now, let's try using booleans in Swift. So, let's start off by creating two boolean constants. We'll name the first one "yes" and we'll set it to "true". Note here that it looks like I'm assigning a string value, 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. (keyboard typing sounds) So name the second one "no" and set it to "false". False, again, is a reserved keyword in Swift. (keyboard typing sounds) Like the other Swift types you've learned about, you can use type inference instead of declaring the type. If the value is just "true' or "false", the compiler will know it's a boolean. (keyboard typing sounds) So you can see the results on the right stay the same and if you option click, (mouse clicks) you can see that this is definitely still a bool. That's an example of setting a boolean value directly, but you can also define booleans using expressions. So, think back to when you were working on the Bullseye app, where you wrote the code to calculate the difference between the slider value and the target value. You wrote and 'if' statement that said: 'if difference is equal to zero'. The part that says 'difference equal to zero', is what's known as an expression. Now, you can think of an expression as a unit of code that resolves to a single value. In this case, the expression either resolves to "true" or "false". So, either the difference is exactly equal to zero, which means it's "true", or the difference is not equal to zero, which means the expression is "false". So, effectively, the result of the 'difference equal to zero' expression is a temporary boolean value. So, if that expression evaluates to "true", the part inside the 'if' statement will execute. But if 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 who have different grades, and you'll set up some boolean variables, depending on who passed and who failed. So first, let's set up a constant. We'll name it "passingGrade" and set it to 50. (keyboard typing sounds) Next, create another constant. Name it "studentGrade" and set it to, say, 74. (keyboard typing sounds) Now, how can you determine if the student has passed using booleans? Now, just as you did in the Bullseye app with other values, you can compare those two values to see if they're 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've passed. So, let's set up an expression to represent that. We'll create another constant. We'll name it "studentPassed" and we'll set it's value to "studentGrade" is greater than "passingGrade". (keyboard typing sounds) So Xcode tell 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. So that is, if the student's grade is less than the passing grade. So, create another constant. Name it "studentFailed" and set its value to "studentGrade" less than "passingGrade". (keyboard typing sounds) And there, you see the value of "studentFailed" is "false". Which means, no, this student didn't fail. But there's a small narrow case here that you haven't covered. What if the student gets exactly 50? So, go back up to where you declared the constants and comment out the one where you set the student grade to 74. Below that, re-declare the constant and set it to 50. (keyboard typing sounds) Okay, now, take a look now at your two constants "studentPassed" and "studentFailed". They're both "false". Well, that can't be right. So, what you haven't taken into account is what happens when the student grade is exactly equal to the passing grade. So, in English, to determine if someone passed, you'd say: if "studentGrade" is more than or equal to the "passingGrade" and Swift provides an operator just for that. So comment out the line where we compare if "studentGrade" is greater than "passingGrade", (keyboard typing sounds) and replace it with a slightly different line, where we declare a constant "studentPassed", and set its value to "studentGrade" is greater than or equal to "passingGrade". (keyboard typing sounds) Oh, there you go. Now, if the student squeaks by with a 50, they still pass. Good for them! There's also an operator for 'less than' or 'equal to', and you can use them wherever you want. Now, you can also simply compare whether something is equal or not. Let's take the case of, Sam and Chris are two shiny new students and we'll give them each a grade. Declare the constant "chrisGrade" and give Chris a 49. (keyboard typing sounds) And declare another constant and give our star student, Sam, a 99. (keyboard typing sounds) So, you've probably been used to thinking of the equals sign as this: (keyboard typing sounds) 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 equals signs together. (keyboard typing sounds) So, let's see this in action. Is "samGrade" equal to "chrisGrade"? (keyboard typing sounds) No, of course they're not. And Swift looks at the two values on either side of the equality operator, decides they're not equivalent, and returns "false", in this case. Now, what if you wanted to check whether two things are non-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. So let's check if "samGrade" is not equal to "chrisGrade". (keyboard typing sounds) And 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'. Now, Swift can also compare things besides numbers, including strings. So, let's declare two constants: "catName", which takes the value of "Ozma" and "dogName", which takes the value of "Mango". (keyboard typing sounds) You can use those same equality and inequality operators on strings. Is "catName" equal to "dogName"? (keyboard typing sounds) No. They're not the same. Swift compares the contents of each string to see if they're the same or different. And in this case, they're different. Now, what about those 'greater than' or 'less than' operators? Yeah. You can use those on strings too. So here's an example: check if "catName" is greater than "dogName". (keyboard typing sounds) 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.