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
Find Someone's IP With iChat and Terminal
Hey guys, this is Mack Heads 101, and today I’m gonna be showing you how to use Terminal and iChat to get someone’s IP address. Now, what is someone’s IP address? You might already know this, but it’s a unique number that indicates their network. With th…
Overview of the Roman Empire | World History | Khan Academy
When you hear of Ancient Rome or the Roman Empire, the Roman Republic, immediately images of the Roman legions come to mind. These conquering armies conquered much of the Mediterranean. You might have images of the Roman Senate; names like Julius Caesar a…
Reflecting & compressing functions | Mathematics III | High School Math | Khan Academy
[Voiceover] So we’re told g of x is a transformation of f of x. The graph below shows f of x as a solid blue line. So this is the graph of y is equal to f of x, and the g of x is a dotted red line. So that’s the graph of y is equal to g of x. What is g of…
The Middle colonies | Period 2: 1607-1754 | AP US History | Khan Academy
Over the course of the 1600s, the English continued to settle along the eastern seaboard of North America. Now, we’ve already talked about the settlements at Virginia and those of Massachusetts, and a little bit about the settlement of New York, which was…
Is science about to end? | Sabine Hossenfelder
Starting in the 1970s and 1980s, a lot of physicists became very optimistic that we’re pretty close to finding a theory of everything that would explain all the interactions in nature in one coherent whole. And string theory was one of the biggest candida…
Heat transfer and thermal equilibrium | Thermodynamics | AP Chemistry | Khan Academy
Let’s see. We have two samples of helium gas. One sample of helium gas is at temperature t1, and the other sample of helium gas is at temperature t2. If t2 is greater than t1, that means, on average, the particles of helium gas in the second box are movin…