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