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 songs 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 so 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 ores 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 ores 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 EV valate 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 shortcircuit 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 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 SI is equal to zero the left hand 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 hand 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