Programming in Dart: Fundamentals
Apr 26 2022 · Dart 2.15, DartPad, DartPad
Part 1: Fundamentals
03. Understand Booleans & Comparison Operators
So far in this course, you’ve learned about a few types of variables:
Integers, which represent whole numbers like 1, 2, 3,
Doubles, which represent decimal numbers like 3.14,
and Strings, which represent text like “Hi There”. In this exercise, you’ll learn about a another type in Dart: Booleans.
Booleans, or “bools” for short, can only have two possible values:
…true, or false. 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 Dart!
Start off in DartPad 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 Dart that means what it says; it simply represents something that is true.
const bool yes = true;
Name the second one “no” and set it to “false”. “False”, again, is a reserved keyword in Dart.
const bool no = false;
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.
const yes = true; const no = false;
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 wrote an if statement that said “if difference == 0”. The part that says “difference == 0” 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 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, delete the existing code. Next set up a constant, name it passingGrade, and set it to 50:
const passingGrade = 50;
Next, create another constant, name it studentGrade, and set it to, say,
const 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
const studentPassed = studentGrade > passingGrade; print(studentPassed);
When you run the code, you’ll see a
true value 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:
const studentFailed = studentGrade < passingGrade; print(studentFailed);
And there you see that the value of
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
// const studentGrade = 74; const studentGrade = 50;
Take a look now at your two constants
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 Dart provides an operator just for that.
Comment out this line:
// const 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”:
const 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
const chrisGrade = 49;
And declare another constant and give our star student,
const samGrade = 99;
You’ve probably been used to thinking of the equals 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 “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 Dart, you use the “equality” operator instead, which are two equal signs together. Let’s see this in action. Is
samGrade equal to
print(samGrade == 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, or in equal? Dart 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
print(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”. Dart can also compare things beside numbers – including Strings.
Declare two constants:
catName, which takes the value of
dogName, which takes the value of
const catName = 'Ozma'; const dogName = 'Cosmos';
You can use those same equality and inequality operators on Strings. Is catName equal to dogName?
print(catName == dogName);
No! They’re not the same. Dart compares the contents of each String to see if they are the same or different, and in this case, they’re different.