Watch this episode for free — or access the full course as a raywenderlich.com Professional Subscriber. Learn more here
Programming in Dart: Fundamentals
Part 1: Fundamentals Use Logical Operators
— Flutter & Dart

Lesson Complete

Play Next Lesson
Next

Use Logical Operators

This video was last updated on Apr 26 2022

Learn how to use various logical and comparison operators in Dart to create Booleans.

Contributors

In the previous set of exercises, you saw how you can work with Boolean values, and how you could use comparison operators such as less than, greater than, equal to and not equal to to compare values to each other.

In this video, you’ll learn about another set of operators that are designed for working with Boolean values and expressions: these are known as “logical operators”.

Logical operators let you do things like check if at least one Boolean value in a set is true, if all Boolean values in a set are true, or even to check if a Boolean value is not true. Sound strange? That’s OK - you’ll learn how these work as you work through the demo.

Open up DartPad and lets create some new variables.

const passingGrade = 50;
const studentGrade = 50;
const chrisGrade = 49;
const samGrade = 99;

Here, lets define some Booleans (through type inference) that check whether or not our illustrious students passed. You’ll use these values as you work through some exercises about logical operators.

const studentPassed = studentGrade >= passingGrade;
const chrisPassed = chrisGrade >= passingGrade;
const samPassed = samGrade >= passingGrade;

The first logical operator is simple: the not operator. It’s represented in Dart as a single exclamation mark, placed before the constant or variable name. If I want to check the opposite, or inverse, of whether Sam passed, I simply write “not samPassed”:

print(!samPassed);

And this tells me that this is false, because Sam did pass. And I can also check to see if Chris did not pass, by doing the same thing here: “not chrisPassed”:

print(!chrisPassed);

And in this case, it’s true, because it’s true that Chris didn’t pass. It may seem strange to check the inverse of a Boolean, but when you start writing more complicated logic later on, you’ll often want to run a particular block of code only when a particular value or set of values isn’t true. So to avoid writing weird code like this:

print(chrisPassed == false);

You can simply use the not operator to make the meaning more clear. Now, this not operator only applies to Booleans. What do you think happens if you try to use it on something like a String value? Let me set up a constant catName and set it to “Jaspurr”:

const catName = 'Jaspurr';

If I try to say “not catName”,

print(!catName);

Dart really has no idea what the inverse of Jaspurr is, so it throws an error: Logical operations are really only useful for Booleans. Let’s comment that out, since that code doesn’t make sense.

// print(!catName);

The next logical operator is the AND operator. It’s pretty similar to they way you’d think about the word “AND” in English; is this true, AND is that true? Do you want ice cream AND sprinkles? You represent AND in Dart with two ampersand symbols - that’s right, the AND sign!

// AND Operator
// &&

We can use the AND operator to see if both Chris and Sam passed. Let me define a constant bothPassed, and I want this to be true if both chrisPassed and samPassed are true:

const bothPassed = chrisPassed && samPassed;
print(bothPassed);

And you can see that no, both Sam and Chris didn’t pass. This expression will only be true if both values on either side of the AND operator are true. In this case, we know that chrisPassed is false, so this entire expression can’t be true.

Now, what if you just want to know if one or the other has passed? That is, you don’t care who passed, just that one of Sam or Chris passed?

That brings you to the next logical operator, OR. And again, the OR operator is somewhat analogous to the word OR in English. Are you having pie or cake for dessert?

The one small difference is that in English, we usually mean “or” as exclusive; in that you want either pie or cake. But the OR operator simply means “if any one of these things is true, then this whole expression is true”.

The OR operator is two vertical lines, or pipes; you can usually find this character in the very top row of your keyboard.

// OR Operator
// ||

Lets define a constant called eitherPassed and set that to chrisPassed OR samPassed:

const eitherPassed = chrisPassed || samPassed;
print(eitherPassed);

And in this case, you see that at least one of those two passed, so the result is true. Even if both of these students had passed, the result would still be true. And if not a single person in that group had passed, then the result would be false. You aren’t limited to just two values for OR and AND operators; you can string as many together as you like. For instance, I have this anonymous student who has a grade and a passed value. What if I want to know is, if any one of these three people passed?

Let me create a constant, anyonePassed, and I’ll set it to chrisPassed OR samPassed OR studentPassed, like so:

const anyonePassed = chrisPassed || samPassed || studentPassed;
print(anyonePassed);

Here, we know at least one person passed, so this entire expression is true. What if you wanted to know if everyone passed; that is, are all of the values in a statement true? You can do that with multiple AND operators, just like you did with multiple OR operators. Let’s declare a constant, everyonePassed, and we’ll set it to chrisPassed AND samPassed AND studentPassed:

const anyonePassed = chrisPassed && samPassed && studentPassed;
print(anyonePassed);

And you see that no, not everyone passed. Because just one of these values is false, chrisPassed, the entire expression becomes false. What’s nice about building up these Boolean expressions this way is that you can mix and match them with the comparison operators you learned about in the previous video. So you can use things like greater than or less than operators along with these new AND and OR operators.

To see this in action, let’s assume that any student who has perfect attendance AND has a mark over 90, gets a special merit award. Lets capture that in two constants: one, the meritAwardGrade of 90:

const meritAwardGrade = 90;

…and let’s capture Sam’s perfect attendance in a constant, samHasPerfectAttendance:

const samHasPerfectAttendance = true;

Must be nice to be perfect, Sam! So, we need to build up an expression that determines (a) if Sam has perfect attendance, and (b) if Sam has a mark over 90. Lets create another constant to hold the result of this: samIsMeritStudent, and I’ll set that to samHasPerfectAttendance AND samGrade is greater than meritAwardGrade:

const samIsMeritStudent = samHasPerfectAttendance && samGrade > meritAwardGrade;
print(samIsMeritStudent);

And we see that yes, Sam has perfect attendance AND his mark is over 90. Swift first calculates the value of samGrade greater than merit AwardGrade to be true, and then it goes on to evaluate the other Boolean values in the expression. Since both are true, then the entire expression is true as well. Congratulations, Sam!

Reviews

Comments