WEBVTT 00:00:00.150 --> 00:00:01.620 - [Instructor] We can use Boolean expressions 00:00:01.620 --> 00:00:03.390 to ask questions in our programs, 00:00:03.390 --> 00:00:07.020 but how can we branch control flow based on the answer? 00:00:07.020 --> 00:00:09.751 To do that, we need conditionals. 00:00:09.751 --> 00:00:12.480 Conditionals form the basis of selection. 00:00:12.480 --> 00:00:14.010 They allow the computer to decide 00:00:14.010 --> 00:00:16.650 which code to run depending on whether the answer 00:00:16.650 --> 00:00:20.940 to a specific question or condition is true or false. 00:00:20.940 --> 00:00:24.000 The first piece of a conditional is the IF statement. 00:00:24.000 --> 00:00:26.640 We start an IF statement with the keyword If. 00:00:26.640 --> 00:00:28.595 Then we add our condition, 00:00:28.595 --> 00:00:29.880 which can be any boolean expression. 00:00:29.880 --> 00:00:32.738 We always end this line with a colon 00:00:32.738 --> 00:00:34.320 because an IF statement comes in two parts. 00:00:34.320 --> 00:00:36.360 This first part, which is the condition 00:00:36.360 --> 00:00:39.060 and the second part, which is the instructions to run 00:00:39.060 --> 00:00:41.580 if that condition evaluates to true. 00:00:41.580 --> 00:00:45.210 In Python, we indicate those instructions using indentation. 00:00:45.210 --> 00:00:47.670 Any lines of code immediately following that condition 00:00:47.670 --> 00:00:49.530 that are indented one level in 00:00:49.530 --> 00:00:52.200 are considered inside that IF statement, 00:00:52.200 --> 00:00:54.090 if the condition evaluates to true, 00:00:54.090 --> 00:00:56.850 the computer will go on to execute all of the instructions 00:00:56.850 --> 00:00:59.256 indented inside the IF statement in order. 00:00:59.256 --> 00:01:01.230 When it's done, it'll go on to execute 00:01:01.230 --> 00:01:03.180 the rest of the program as normal. 00:01:03.180 --> 00:01:04.890 If the condition evaluates to false, 00:01:04.890 --> 00:01:07.852 the computer will skip all the lines of code 00:01:07.852 --> 00:01:09.180 that are indented inside of that IF statement. 00:01:09.180 --> 00:01:12.211 Instead, it'll jump directly to the next line of code 00:01:12.211 --> 00:01:13.140 that's outside of the IF statement. 00:01:13.140 --> 00:01:14.520 That means the first line of code 00:01:14.520 --> 00:01:16.350 that is indented at the same level 00:01:16.350 --> 00:01:18.210 as that initial if. 00:01:18.210 --> 00:01:19.620 Let's try an example. 00:01:19.620 --> 00:01:22.177 Here we have the Boolean expression, 00:01:22.177 --> 00:01:24.480 num_orders equals equals 10, which asks the question, 00:01:24.480 --> 00:01:26.310 is this the customer's 10th order? 00:01:26.310 --> 00:01:29.430 We can then use this Boolean value in our IF statement. 00:01:29.430 --> 00:01:31.230 We'll indent one print function call 00:01:31.230 --> 00:01:33.960 inside of the IF statement and one outside. 00:01:33.960 --> 00:01:37.590 The one inside will only execute if it is the 10th order, 00:01:37.590 --> 00:01:39.870 that is if the variable is 10th order 00:01:39.870 --> 00:01:42.090 contains the Boolean value True. 00:01:42.090 --> 00:01:44.915 We can also write this without the variable 00:01:44.915 --> 00:01:47.014 where we just inline that whole Boolean expression 00:01:47.014 --> 00:01:48.456 in the IF statement. 00:01:48.456 --> 00:01:50.190 Let's trace how the computer executes this. 00:01:50.190 --> 00:01:51.360 It starts with the first line 00:01:51.360 --> 00:01:54.360 and assigns a value 10 to the variable num_orders. 00:01:54.360 --> 00:01:55.920 Then it hits this IF statement, 00:01:55.920 --> 00:01:58.890 so first it evaluates the Boolean expression. 00:01:58.890 --> 00:02:02.880 10 is equal to 10, so this evaluates to true. 00:02:02.880 --> 00:02:04.020 Because it's true, 00:02:04.020 --> 00:02:06.750 the computer will execute the lines of code 00:02:06.750 --> 00:02:08.280 indented inside of the IF statement. 00:02:08.280 --> 00:02:11.415 First, it'll print, "Your order is free!" 00:02:11.415 --> 00:02:12.660 And then it'll assign the value zero 00:02:12.660 --> 00:02:14.430 to the variable num_orders. 00:02:14.430 --> 00:02:16.552 Then it just keeps moving through 00:02:16.552 --> 00:02:17.853 the rest of the program in order, 00:02:17.853 --> 00:02:20.935 so it'll print the value of num_orders, which is now zero. 00:02:20.935 --> 00:02:22.973 Now let's say instead num_orders 00:02:22.973 --> 00:02:24.215 was originally set to three. 00:02:24.215 --> 00:02:25.290 When the computer executes this program, 00:02:25.290 --> 00:02:27.420 it'll assign the value three to num_orders, 00:02:27.420 --> 00:02:29.070 evaluate the Boolean expression. 00:02:29.070 --> 00:02:31.500 Three is not equal to 10, so this is false, 00:02:31.500 --> 00:02:33.000 and then because it's false, 00:02:33.000 --> 00:02:34.740 we'll skip the lines of code indented 00:02:34.740 --> 00:02:36.893 inside the IF statement. 00:02:36.893 --> 00:02:38.797 It'll jump to the next line of code outside, 00:02:38.797 --> 00:02:40.997 which is print num_orders, so it'll print three. 00:02:40.997 --> 00:02:43.650 Note that if we didn't indent the line of code 00:02:43.650 --> 00:02:46.290 num_orders equal zero, it would be considered 00:02:46.290 --> 00:02:49.410 outside of the IF statement, so in the false case, 00:02:49.410 --> 00:02:51.733 num_orders would be set to zero, 00:02:51.733 --> 00:02:53.316 and then it would print zero. 00:02:53.316 --> 00:02:54.990 In the true case, we would print, "Your order is free." 00:02:54.990 --> 00:02:56.550 Then we would set num_orders to zero, 00:02:56.550 --> 00:02:58.680 and then we would print zero. 00:02:58.680 --> 00:03:01.080 You can write IF statements with any Boolean expressions 00:03:01.080 --> 00:03:02.850 as conditions, and you can put any 00:03:02.850 --> 00:03:05.820 and as many instructions as you want inside of them. 00:03:05.820 --> 00:03:08.196 With this conditional, I could make sure 00:03:08.196 --> 00:03:09.732 to only ask the follow-up question, 00:03:09.732 --> 00:03:11.610 chicken or tofu, if the user ordered pad Thai, 00:03:11.610 --> 00:03:13.440 because if they order the papaya salad, 00:03:13.440 --> 00:03:15.120 that question doesn't make sense. 00:03:15.120 --> 00:03:18.570 Note that in most IDEs we indent using the tab key. 00:03:18.570 --> 00:03:19.870 Our standard lines of code 00:03:20.718 --> 00:03:21.690 should line up directly with the left margin. 00:03:21.690 --> 00:03:24.540 That is, there should be no spaces or indents before them. 00:03:24.540 --> 00:03:26.790 Lines of code indented inside of an IF statement 00:03:26.790 --> 00:03:29.040 should be one tab key over. 00:03:29.040 --> 00:03:31.812 Getting the indentation right is crucial 00:03:31.812 --> 00:03:32.880 because this is the only way we can tell the computer 00:03:32.880 --> 00:03:34.740 which lines of code are inside the IF statement 00:03:34.740 --> 00:03:36.630 and which are outside. 00:03:36.630 --> 00:03:37.950 We need to be careful here though 00:03:37.950 --> 00:03:39.660 because now our control flow branches. 00:03:39.660 --> 00:03:43.523 If the user orders pad Thai, this program works fine, 00:03:43.523 --> 00:03:46.080 but if the user orders something else, we get a name error. 00:03:46.080 --> 00:03:48.180 The variable protein is only defined 00:03:48.180 --> 00:03:49.710 inside the IF statement. 00:03:49.710 --> 00:03:51.000 If the condition is false, 00:03:51.000 --> 00:03:53.010 this assignment statement doesn't execute, 00:03:53.010 --> 00:03:56.010 so the computer doesn't know a variable protein. 00:03:56.010 --> 00:03:57.960 To fix this, we either need to make sure 00:03:57.960 --> 00:03:59.850 we're only accessing the variable protein 00:03:59.850 --> 00:04:03.000 inside the IF statement where we're guaranteed it exists, 00:04:03.000 --> 00:04:05.400 or we need to initialize the variable protein 00:04:05.400 --> 00:04:07.503 before the IF statement, 00:04:07.503 --> 00:04:08.850 which guarantees that it's always defined. 00:04:08.850 --> 00:04:10.680 It's common in situations like this 00:04:10.680 --> 00:04:11.790 to initialize the variable 00:04:11.790 --> 00:04:14.301 to an empty or placeholder value 00:04:14.301 --> 00:04:16.380 like the empty string or no protein. 00:04:16.380 --> 00:04:19.020 Now there's no error on either possible path. 00:04:19.020 --> 00:04:22.080 If the order is equal to pad Thai or if it's not. 00:04:22.080 --> 00:04:25.425 Now that our control flow branches, when we run a program, 00:04:25.425 --> 00:04:26.543 we're only testing one 00:04:26.543 --> 00:04:29.010 of perhaps many possible passive execution through the code. 00:04:29.010 --> 00:04:30.450 Just because it works for one case 00:04:30.450 --> 00:04:31.860 doesn't mean it works for the other, 00:04:31.860 --> 00:04:33.540 and it's up to us to test for that. 00:04:33.540 --> 00:04:35.610 Otherwise, it'll be our users who suffer 00:04:35.610 --> 00:04:37.810 when they're the first ones to find the bug.