yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Evaluating compound boolean expressions | Intro to CS - Python | Khan Academy


4m read
·Nov 10, 2024

How does the computer evaluate expressions with the logical operators and, or, and not to find out? Let's explore the order of operations for compound Boolean expressions. Imagine we're working on a program to check if a specific song matches the filters for a specific playlist. This expression asks: are the song's beats per minute both greater than or equal to 150 and less than or equal to 180?

Logical operators like this and come last in the order of operations, so the computer evaluates the two sides separately first. Let's say the variable BPM contains the value 200. The comparison operators greater than or equal to and less than or equal to have the same level of precedence, so the computer evaluates left to right.

We substitute in that 200 on the left-hand side first and simplify. 200 is greater than or equal to 150, so this evaluates to true. Then we jump to the right-hand side: 200 is not less than or equal to 180, so this evaluates to false. Now that we've simplified both sides down to a Boolean value, we evaluate the and. An expression with the and operator only evaluates to true if both sides are true. So, a true and a false evaluates to false, and boom, the computer has its answer.

Let's try an expression where there are multiple logical operators. This asks the question: is the genre any of the spellings of Loi? Let's say the variable genre contains the string l-fi. We evaluate the expressions around the logical operators first and then apply the ors. We start with the leftmost expression: these strings are not equal, so this evaluates to false. We jump to the second expression: these strings are equal, so this evaluates to true. And then the third part: these strings are not equal, so this evaluates to false.

Now that we're all simplified, we take a look at the ors. An expression with the or operator evaluates to true if at least one of the sides is true. False or true evaluates to true, and then true or false evaluates to true. Now, you may be thinking, "Whoa, whoa! Did I even need to evaluate that last part?" Once I knew that second expression evaluated to true, I knew that my final answer was going to be true.

As it's evaluating, the computer makes the same optimization. We call this short-circuit evaluation or lazy evaluation. In its laziness, the computer stops evaluating as soon as it knows the final answer. With the or operator, the computer stops evaluating as soon as it finds a side that evaluates to true. Because no matter what's on the other side, the expression will always evaluate to true. True or true evaluates to true, and true or false also evaluates to true.

With the and operator, we have the opposite. The computer stops as soon as it finds a side that evaluates to false. No matter what's on the other side, the whole expression will evaluate to false because false and true evaluates to false, and false and false also evaluates to false.

Okay, so the computer's saving itself some work. Why do I care? Consider this Boolean expression: it looks pretty sensible, but what if the variable group size contains the value zero? The computer can't divide by zero, so this gives a runtime error. Well, we could just not do that, but we might not control the value of group size; maybe it's set by user input.

To solve this, we can check that group size doesn't equal zero first. If group size is equal to zero, the left side will evaluate to false, and the computer will short-circuit. It'll jump to the conclusion that the whole expression must evaluate to false and won't bother evaluating the right side, thus avoiding the division by zero.

We can use this pattern across our programs, taking advantage of short-circuit evaluation to check for preconditions that allow us to avoid possible errors.

Last bit: what about the not operator? The not operator takes precedence over the and and or operators. That means this expression really asks: is the genre not equal to rock and is the BPM greater than 130? Now, we want to be careful about overusing the not operator when we don't need to because it tends to make things more confusing. For example, this expression is just equivalent to genre not equals rock and BPM greater than 130.

If instead I put parentheses here, I would be negating the whole expression. This asks: is it not both a rock song and a fast song? That's equivalent to the expression: is the genre not equal to rock or is the BPM less than or equal to 130? If either of these conditions is true, then this and expression would evaluate to false, which means not it would evaluate to true.

Now you're probably starting to understand why I said to use the not operator sparingly. If you do need to negate a compound Boolean expression, it's often easier to understand if you break it down into multiple parts. Otherwise, the not operator tends to make your program a bit less readable—just like if I had said something like, I prefer not blue colors or I'm going to not. Not in this video now.

More Articles

View All
How rapid hair loss changed my life
So I’ve had a lot of comments on the channel over the years being like, “Joey, you should share your hair loss journey,” as if I have some epic tale, a dramatic story arc infused with life lessons, a coming-of-age tale. But in reality, it’s not something …
Ask Sal Anything! Homeroom Tuesday, August 11
Hi everyone! Sal here. Welcome to the, I guess, Homeroom with Sal, uh, live stream. The name keeps evolving a little bit. A couple of quick announcements. First of all, uh, we were hoping to have Lester Holt today, uh, but him being in the news industry,…
My Competitive Weapon In Business | Yahoo Finance
Dyslexia, however, to me, is a competitive weapon. You have to take this like a superpower that’s unconstrained and focus it. You have to use it as a tool. It’s the out-of-the-box thinkers that make companies competitive—the crazy ones, the dyslexic ones.…
Top 6 SCARIEST Online Games: DONG #1
Vsauce! Happy almost Halloween! To celebrate, I’m going to show you my top six favorite flash games to get me into the Halloween spirit, and you can play them too. I’ve got links in the description, so without further ado, let’s turn down the lights and g…
How To Get Rich According To Steve Jobs
There are a million ways to make a million dollars, and in this video, we’re looking at one of them. If Steve Jobs were alive today, he would be among the top 10 richest people on the planet. Jobs was known to be a non-conformist, a man focused on buildin…
The Infinite Pattern That Never Repeats
A portion of this video was sponsored by LastPass. This video is about a pattern people thought was impossible and a material that wasn’t supposed to exist. The story begins over 400 years ago in Prague. I’m now in Prague and the Czech Republic, which is …