Programming in Dart: Fundamentals

Apr 26 2022 · Dart 2.15, DartPad, DartPad

Part 1: Fundamentals

03. Understand 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. Meet Dartpad & Write Comments Next episode: 04. Challenge: Play with 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.

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 one, two, and three. Doubles, which represent decimal numbers, like 3.14. And strings, which represent text, like, hi there. In this exercise, you'll learn about another type in Dart, Booleans. Booleans or bools, for short, can have two possible values, true or false. You might not realize it, but you've already used Booleans if you built the Bullseye app in the previous course. You'll see what I mean before the end of this video. Let's try using Booleans in Dart. Start off in DartPad by creating who Boolean constants. Name the first one, yes and set it to true. Note that here it looks like I'm assigning a string value but true is a special term known as a reserved keyword in Dart that means what it says. It simply represents something that is true. Name the second one, no, and set it to false. False again is a reserved keyword in Dart. Like with the other Dart 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. That's an example of setting a Boolean value directly. You could also define Booleans using expressions. 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 an if statement that said, if difference equals equals zero, the part that says difference equals equals zero is what's known as an expression. You can think of an expression as a unit of code that resolves to a value. In this case, the expression resolves to either true or false. Either the difference is exactly equal to zero which means the expression is true or the difference is not equal to zero, which means the expression is false. Effectively, the result of the difference equals equals zero's expression is a temporary Boolean value. If that expression evaluates to true, the part inside the if statement will execute, if it evaluates to false, then the part inside the if statement will not execute. To see this in action, you'll set up a simple scenario with some students with different grades and you'll set up Boolean variables depending on who passed and who failed. First, delete the existing code. Next, set up a constant, name it passinGrade and set it to 50. Then create another constant, name it studentGrade and set it to say 74. Now, how can you determine if the student has passed using Booleans? Just as you did in the Bullseye 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. Create another constant, name it studentPassed and set its value to studentGrade is greater than passingGrade. When you run the code, you'll see a true value that yes, this student passed since 74 is greater than 50. It sets the value of students passed to true. You can also test the reverse situation to see if a student has failed. That is if the student's grade is less than the passing grade. Create another constant name it studentFailed and set its value to studentGrade less than passingGrade. And there you see the value of studentGrade 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 declare the constants and comment out the one where you set the student grade to 74, below that re declare that constant and set it to 50. 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 student grade is exactly equal to the passing grade. In English, to determine if someone passed you'd say, if student grade more than or equal to passing grade and Dart provides an operator for just that. Comment out this line 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. And 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 whenever you need. Now, you 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 and declare another constant and give our star student Sam a 99. You've probably been used to thinking of the equal sign as this, but in Dart, like many other languages, that's used to assign values, which is why I've tried not to say things like, Chris grade equals 99 into this course. It's actually known as an assignment operator in programming not the equal sign. To see if two things are equal in Dart you use the equality operator instead which are two equal signs together. Let's see this in action. Is samGrade equal to chrisGrade? No, of course they're not. Dart 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? Dart has an operator for that as well unsurprisingly called the inequality operator, which is an exclamation mark followed by an equal sign. Let's check if samGrade is not equal to chrisGrade. Yes, of course they're not equal. It's good to get into the habit of reading this as not equal to, since as you'll see in later episodes the exclamation mark actually has another use where you'll read it as not. Dart can also compare things besides numbers including strings. Declare two constants, catName which takes the value of Ozma and dogName which takes the value of Cosmos. You can use those same equality and inequality operators on strings. Use catName equal to dogName? No, they're not the same. Dart compares the contents of each string to see if they are the same or in this case they're different.