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
the moon is leaving
If you applied a coat of paint to the bottom of your shoes every single day, one coat on top of the other, every morning, you would leave Earth just as quickly as our moon is leaving us. Every day, the moon moves about a tenth of a millimeter away from Ea…
On the Hunt: Crossing the Beaver Dams | Alaska: The Next Generation
If I didn’t go about teaching my children tradition and culture, it would be a whole gap and we might not be able to give back. Then my family would be lost in tradition and culture. That little spot back here, just there, Beaver Dam blocking it but ther…
TIL: You Might Be Related to Genghis Khan | Today I Learned
[Music] [Applause] [Music] So you probably heard of the name Genghis Khan or Jengus Khan, but you might not realize that something like one in 200 men in the world were genetically related to Genghis Khan. So he was obviously very, um, prolific. Yeah, bu…
109-Year-Old Veteran and His Secrets to Life Will Make You Smile | Short Film Showcase
[Music] Yeah, a lot of people say God kept you here to help others, but I don’t know why he kept me here. I can’t tell you. I ain’t talk to him; he ain’t talk to me. My name is Richard Ain Overton. I am 109 years old! [Music] I still walk, I still talk, a…
Kevin Hale and Adora Cheung - Startup School 2019 by the Numbers
Hello Berlin! We’ve made it to week ten. This is the last stop on the startup school tour. We have something kind of special today. So Adora and I are doing a presentation together, and it’s all based on numbers from the last nine weeks of startup school.…
Introducing Khanmigo Teacher Mode
This right over here is an exercise about the Spanish-American War and AP American history on Khan Academy. We start off in student mode and notice if the student asks for an explanation, it doesn’t just give the answer. It does what a good tutor would do…