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
15 Signs You’re Burned Out, Not Lazy
Over 70% of professionals feel burnt out at some point, yet many dismiss it as mere laziness. But the great news about burnout is that it’s solvable. The bad news about laziness is that, in this video, we’re about to call you out. Here are 15 signs you’re…
10 science based habits i'm trying to build to improve my life🍃
Habits are building blocks of your life. If you want to change your life, or if you want to change who you are, the first step always should be looking at your current habits and determining which of them are bad and which of them are good. Changing those…
GameStop's Final Blow to the Short Sellers
So as we’ve been following over the last couple of months, Gamestop shares have literally been flying all over the place in the aftermath of the Reddit-fueled short squeeze that happened at the end of January. Over the past couple of months, myself and Ha…
What is the BEST Stock Market Investing Strategy?
Well guys, it’s day four of the new money advent calendar and I’m already struggling. I’m recording this at 9:30 at night. I am in my pajamas; I’m like the classic news anchor right now. You know, got my good shirt on up top and then just wearing my pajam…
The biggest habit building mistake
If you have an addiction that brings you great shame, or just a nasty, nasty bad habit that you for some reason can’t stop doing, or even if you have something that is a good thing that you want to start doing—maybe it’s going to the gym. Maybe you want t…
The Law of Productivity
The Increasing Demand for Productivity The world is more demanding now than it’s ever been. The cost of living and competition for jobs is increasing, with AI outright replacing some jobs. At the same time, wages don’t go as far as they did in previous g…