Programming in Dart: Fundamentals

Apr 26 2022 · Dart 2.15, DartPad, DartPad

Part 1: Fundamentals

05. Use Logical 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: 04. Challenge: Play with Booleans Next episode: 06. Set Conditional Values

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.

In the previous set of exercises, you saw how you can work with boolean values, and how you can use comparison operators such as less than, greater than, equal to, and not equal to to compare values to each other. In this episode, 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. Sounds strange? That's okay. You'll learn how these things work as you work through the demo. Open up DartPad, and let's create some variables. Here, let's define some booleans through type inference that check whether or not our illustrious students have passed. You'll use these values as you work through some exercises about logical operators. 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, !samPassed. In 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, !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. 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. If I tried to say, !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 the code doesn't make sense. The next logical operator is the "and" operator. It's pretty similar to the way you'd think about the word, and in English. Is the is 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. 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. 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 has 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 a 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 those things are 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. Let's define a constant called, eitherPassed, and set that to chrisPassed or samPassed. And in this case, you can see that at least one of those two passed. So the result is true. Even if both 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 I want to know is, if any one of these three people passed. Let's create a constant, anyonePassed, and I'll set it to chrisPassed or samPassed or studentPassed, like so. 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. You can see that, no, not everyone passed, because just one of those 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 episode. 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 the perfect attendance and has a mark over 90 gets a special merit award. Let's capture that in two constants. One, the merit award grade of 90, and let's capture Sam perfect attendance in a constant, samHasPerfectAttendance. It 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. Let's create another constant to hold the result of this, samIsMeritStudent, and I'll set that to samHasPerfectAttendance, and samGrade is greater than meritAwardGrade. And we see that, yes, Sam has perfect attendance and his mark is over 90. Dart first calculates the award of Sam grade greater than merit award grade 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.