[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.15,0:00:02.13,Default,,0000,0000,0000,,- [Presenter] How does the\Ncomputer evaluate expressions Dialogue: 0,0:00:02.13,0:00:05.22,Default,,0000,0000,0000,,with the logical operators and or and not? Dialogue: 0,0:00:05.22,0:00:07.80,Default,,0000,0000,0000,,To find out, let's explore\Nthe order of operations Dialogue: 0,0:00:07.80,0:00:09.90,Default,,0000,0000,0000,,for compound Boolean expressions. Dialogue: 0,0:00:09.90,0:00:11.73,Default,,0000,0000,0000,,Imagine we're working\Non a program to check Dialogue: 0,0:00:11.73,0:00:13.92,Default,,0000,0000,0000,,if a specific song matches the filters Dialogue: 0,0:00:13.92,0:00:15.87,Default,,0000,0000,0000,,for a specific playlist. Dialogue: 0,0:00:15.87,0:00:18.54,Default,,0000,0000,0000,,This expression asks, are\Nthe song's beats per minute Dialogue: 0,0:00:18.54,0:00:21.12,Default,,0000,0000,0000,,both greater than or equal to 150, Dialogue: 0,0:00:21.12,0:00:24.09,Default,,0000,0000,0000,,and less than or equal to 180? Dialogue: 0,0:00:24.09,0:00:25.77,Default,,0000,0000,0000,,Logical operators like this and Dialogue: 0,0:00:25.77,0:00:27.78,Default,,0000,0000,0000,,come last in the order of operations. Dialogue: 0,0:00:27.78,0:00:31.11,Default,,0000,0000,0000,,So the computer evaluates the\Ntwo sides separately first. Dialogue: 0,0:00:31.11,0:00:34.59,Default,,0000,0000,0000,,Let's say the variable BPM\Ncontains the value 200. Dialogue: 0,0:00:34.59,0:00:36.69,Default,,0000,0000,0000,,The comparison operators\Ngreater than or equal to Dialogue: 0,0:00:36.69,0:00:39.24,Default,,0000,0000,0000,,and less than or equal to have\Nthe same level of precedence, Dialogue: 0,0:00:39.24,0:00:41.58,Default,,0000,0000,0000,,so the computer evaluates left to right. Dialogue: 0,0:00:41.58,0:00:43.65,Default,,0000,0000,0000,,So we substitute in that 200 Dialogue: 0,0:00:43.65,0:00:46.29,Default,,0000,0000,0000,,on the left-hand side first and simplify. Dialogue: 0,0:00:46.29,0:00:48.90,Default,,0000,0000,0000,,200 is greater than or equal to 150, Dialogue: 0,0:00:48.90,0:00:51.15,Default,,0000,0000,0000,,so this evaluates to true. Dialogue: 0,0:00:51.15,0:00:53.40,Default,,0000,0000,0000,,Then we jump to the right-hand side. Dialogue: 0,0:00:53.40,0:00:56.46,Default,,0000,0000,0000,,200 is not less than or equal to 180, Dialogue: 0,0:00:56.46,0:00:58.62,Default,,0000,0000,0000,,so this evaluates to false. Dialogue: 0,0:00:58.62,0:01:00.30,Default,,0000,0000,0000,,Now that we've simplified both sides down Dialogue: 0,0:01:00.30,0:01:03.06,Default,,0000,0000,0000,,to a Boolean value, we evaluate the and. Dialogue: 0,0:01:03.06,0:01:05.94,Default,,0000,0000,0000,,An expression with the and\Noperator only evaluates to true Dialogue: 0,0:01:05.94,0:01:07.50,Default,,0000,0000,0000,,if both sides are true. Dialogue: 0,0:01:07.50,0:01:10.44,Default,,0000,0000,0000,,So a true and a false evaluates to false. Dialogue: 0,0:01:10.44,0:01:12.52,Default,,0000,0000,0000,,And boom, the computer has its answer. Dialogue: 0,0:01:12.52,0:01:13.92,Default,,0000,0000,0000,,Let's try an expression Dialogue: 0,0:01:13.92,0:01:15.99,Default,,0000,0000,0000,,where there are multiple\Nlogical operators. Dialogue: 0,0:01:15.99,0:01:17.13,Default,,0000,0000,0000,,This asks the question, Dialogue: 0,0:01:17.13,0:01:20.10,Default,,0000,0000,0000,,is the genre any of\Nthe spellings of lo-fi? Dialogue: 0,0:01:20.10,0:01:24.78,Default,,0000,0000,0000,,Let's say the variable genre\Ncontains the string lo-fi. Dialogue: 0,0:01:24.78,0:01:26.46,Default,,0000,0000,0000,,We evaluate the expressions around Dialogue: 0,0:01:26.46,0:01:29.34,Default,,0000,0000,0000,,the logical operators first\Nand then apply the ors. Dialogue: 0,0:01:29.34,0:01:31.14,Default,,0000,0000,0000,,We start with the leftmost expression. Dialogue: 0,0:01:31.14,0:01:34.23,Default,,0000,0000,0000,,These strings are not equal,\Nso this evaluates to false. Dialogue: 0,0:01:34.23,0:01:35.73,Default,,0000,0000,0000,,We jump to the second expression. Dialogue: 0,0:01:35.73,0:01:38.58,Default,,0000,0000,0000,,These strings are equal,\Nso this evaluates to true. Dialogue: 0,0:01:38.58,0:01:40.98,Default,,0000,0000,0000,,And then the third part,\Nthese strings are not equal, Dialogue: 0,0:01:40.98,0:01:42.96,Default,,0000,0000,0000,,so this evaluates to false. Dialogue: 0,0:01:42.96,0:01:45.66,Default,,0000,0000,0000,,Now that we're all simplified,\Nwe take a look at the ors. Dialogue: 0,0:01:45.66,0:01:48.15,Default,,0000,0000,0000,,An expression with the OR\Noperator evaluates to true Dialogue: 0,0:01:48.15,0:01:50.79,Default,,0000,0000,0000,,if at least one of the sides is true. Dialogue: 0,0:01:50.79,0:01:54.00,Default,,0000,0000,0000,,False or true evaluates to true and then Dialogue: 0,0:01:54.00,0:01:57.06,Default,,0000,0000,0000,,true or false evaluates to true. Dialogue: 0,0:01:57.06,0:01:58.38,Default,,0000,0000,0000,,Now you may be thinking, Dialogue: 0,0:01:58.38,0:02:01.35,Default,,0000,0000,0000,,"Whoa, whoa, did I even need\Nto evaluate that last part?" Dialogue: 0,0:02:01.35,0:02:03.99,Default,,0000,0000,0000,,Once I knew that second\Nexpression evaluated to true, Dialogue: 0,0:02:03.99,0:02:06.78,Default,,0000,0000,0000,,I knew that my final answer\Nwas going to be true. Dialogue: 0,0:02:06.78,0:02:07.86,Default,,0000,0000,0000,,As it's evaluating, Dialogue: 0,0:02:07.86,0:02:10.32,Default,,0000,0000,0000,,the computer makes the same optimization. Dialogue: 0,0:02:10.32,0:02:13.89,Default,,0000,0000,0000,,We called this short circuit\Nevaluation or lazy evaluation. Dialogue: 0,0:02:13.89,0:02:16.08,Default,,0000,0000,0000,,In its laziness, the\Ncomputer stops evaluating Dialogue: 0,0:02:16.08,0:02:18.21,Default,,0000,0000,0000,,as soon as it knows the final answer. Dialogue: 0,0:02:18.21,0:02:20.64,Default,,0000,0000,0000,,With the or operator the\Ncomputer stops evaluating Dialogue: 0,0:02:20.64,0:02:23.04,Default,,0000,0000,0000,,as soon as it finds a side\Nthat evaluates to true Dialogue: 0,0:02:23.04,0:02:25.14,Default,,0000,0000,0000,,because no matter what's\Non the other side, Dialogue: 0,0:02:25.14,0:02:27.54,Default,,0000,0000,0000,,the expression will\Nalways evaluate to true. Dialogue: 0,0:02:27.54,0:02:29.40,Default,,0000,0000,0000,,True or true evaluates to true Dialogue: 0,0:02:29.40,0:02:32.31,Default,,0000,0000,0000,,and true or false also evaluates to true. Dialogue: 0,0:02:32.31,0:02:34.14,Default,,0000,0000,0000,,With the and operator\Nwe have the opposite. Dialogue: 0,0:02:34.14,0:02:36.00,Default,,0000,0000,0000,,The computer stops as soon as it finds Dialogue: 0,0:02:36.00,0:02:38.16,Default,,0000,0000,0000,,a side that evaluates to false. Dialogue: 0,0:02:38.16,0:02:39.39,Default,,0000,0000,0000,,No matter what's on the other side Dialogue: 0,0:02:39.39,0:02:41.28,Default,,0000,0000,0000,,the whole expression\Nwill evaluate to false. Dialogue: 0,0:02:41.28,0:02:43.41,Default,,0000,0000,0000,,Because false and true evaluates to false Dialogue: 0,0:02:43.41,0:02:46.74,Default,,0000,0000,0000,,and false and false\Nalso evaluates to false. Dialogue: 0,0:02:46.74,0:02:48.84,Default,,0000,0000,0000,,Okay, so the computer's\Nsaving itself some work. Dialogue: 0,0:02:48.84,0:02:51.75,Default,,0000,0000,0000,,Why do I care? Consider\Nthis Boolean expression. Dialogue: 0,0:02:51.75,0:02:52.65,Default,,0000,0000,0000,,It looks pretty sensible, Dialogue: 0,0:02:52.65,0:02:55.98,Default,,0000,0000,0000,,but what if the variable group\Nsize contains the value zero? Dialogue: 0,0:02:55.98,0:02:57.72,Default,,0000,0000,0000,,The computer can't divide by zero. Dialogue: 0,0:02:57.72,0:02:59.40,Default,,0000,0000,0000,,So this gives a runtime error. Dialogue: 0,0:02:59.40,0:03:01.32,Default,,0000,0000,0000,,Well, we could just not do that, Dialogue: 0,0:03:01.32,0:03:03.66,Default,,0000,0000,0000,,but we might not control\Nthe value of group size. Dialogue: 0,0:03:03.66,0:03:05.61,Default,,0000,0000,0000,,Maybe it's set by user input. Dialogue: 0,0:03:05.61,0:03:07.56,Default,,0000,0000,0000,,To solve this, we can\Ncheck that group size Dialogue: 0,0:03:07.56,0:03:09.57,Default,,0000,0000,0000,,doesn't equal zero first. Dialogue: 0,0:03:09.57,0:03:11.82,Default,,0000,0000,0000,,If group size is equal to\Nzero, the left-hand side Dialogue: 0,0:03:11.82,0:03:14.43,Default,,0000,0000,0000,,will evaluate to false and the\Ncomputer will short circuit. Dialogue: 0,0:03:14.43,0:03:15.51,Default,,0000,0000,0000,,It'll jump to the conclusion Dialogue: 0,0:03:15.51,0:03:17.46,Default,,0000,0000,0000,,that the whole expression\Nmust evaluate to false Dialogue: 0,0:03:17.46,0:03:19.47,Default,,0000,0000,0000,,and won't bother evaluating\Nthe right-hand side, Dialogue: 0,0:03:19.47,0:03:21.48,Default,,0000,0000,0000,,thus avoiding the division by zero. Dialogue: 0,0:03:21.48,0:03:23.61,Default,,0000,0000,0000,,We can use this pattern\Nacross our programs, Dialogue: 0,0:03:23.61,0:03:26.19,Default,,0000,0000,0000,,taking advantage of short\Ncircuit evaluation to check Dialogue: 0,0:03:26.19,0:03:29.04,Default,,0000,0000,0000,,for preconditions that allow\Nus to avoid possible errors. Dialogue: 0,0:03:29.88,0:03:32.07,Default,,0000,0000,0000,,Last bit. What about the not operator? Dialogue: 0,0:03:32.07,0:03:33.75,Default,,0000,0000,0000,,The not operator takes precedence Dialogue: 0,0:03:33.75,0:03:36.00,Default,,0000,0000,0000,,over the and and or operators. Dialogue: 0,0:03:36.00,0:03:37.68,Default,,0000,0000,0000,,That means this expression really asks, Dialogue: 0,0:03:37.68,0:03:39.30,Default,,0000,0000,0000,,is the genre not equal to rock Dialogue: 0,0:03:39.30,0:03:41.97,Default,,0000,0000,0000,,and is the BPM greater than 130? Dialogue: 0,0:03:41.97,0:03:44.91,Default,,0000,0000,0000,,Now, we wanna be careful about\Noverusing the not operator Dialogue: 0,0:03:44.91,0:03:46.92,Default,,0000,0000,0000,,when we don't need to because it tends Dialogue: 0,0:03:46.92,0:03:48.60,Default,,0000,0000,0000,,to make things more confusing. Dialogue: 0,0:03:48.60,0:03:50.49,Default,,0000,0000,0000,,For example, this expression\Nis just equivalent Dialogue: 0,0:03:50.49,0:03:54.81,Default,,0000,0000,0000,,to genre not equals rock\Nand BPM greater than 130. Dialogue: 0,0:03:54.81,0:03:56.37,Default,,0000,0000,0000,,If instead I put parentheses here, Dialogue: 0,0:03:56.37,0:03:58.68,Default,,0000,0000,0000,,I would be negating the whole expression. Dialogue: 0,0:03:58.68,0:04:03.09,Default,,0000,0000,0000,,This asks, is it not both a\Nrock song and a fast song? Dialogue: 0,0:04:03.09,0:04:05.37,Default,,0000,0000,0000,,That's equivalent to the\Nexpression is the genre Dialogue: 0,0:04:05.37,0:04:09.30,Default,,0000,0000,0000,,not equal to rock or is the\NBPM less than or equal to 130? Dialogue: 0,0:04:09.30,0:04:11.46,Default,,0000,0000,0000,,If either of these conditions is true, Dialogue: 0,0:04:11.46,0:04:13.95,Default,,0000,0000,0000,,then this and expression\Nwould evaluate to false, Dialogue: 0,0:04:13.95,0:04:16.59,Default,,0000,0000,0000,,which means not it would evaluate to true. Dialogue: 0,0:04:16.59,0:04:18.24,Default,,0000,0000,0000,,Now you're probably starting to understand Dialogue: 0,0:04:18.24,0:04:21.06,Default,,0000,0000,0000,,why I said to use the\Nnot operator sparingly. Dialogue: 0,0:04:21.06,0:04:23.46,Default,,0000,0000,0000,,If you do need to negate a\Ncompound Boolean expression, Dialogue: 0,0:04:23.46,0:04:24.87,Default,,0000,0000,0000,,it's often easier to understand Dialogue: 0,0:04:24.87,0:04:27.00,Default,,0000,0000,0000,,if you break it down into multiple parts. Dialogue: 0,0:04:27.00,0:04:28.20,Default,,0000,0000,0000,,Otherwise, the not operator Dialogue: 0,0:04:28.20,0:04:30.48,Default,,0000,0000,0000,,tends to make your program\Na bit less readable. Dialogue: 0,0:04:30.48,0:04:33.51,Default,,0000,0000,0000,,Just like if I said something\Nlike "I prefer not blue colors Dialogue: 0,0:04:33.51,0:04:35.94,Default,,0000,0000,0000,,or I'm going to not\Nnot in this video now."