1 00:00:00,150 --> 00:00:01,620 - [Instructor] We can use Boolean expressions 2 00:00:01,620 --> 00:00:03,390 to ask questions in our programs, 3 00:00:03,390 --> 00:00:07,020 but how can we branch control flow based on the answer? 4 00:00:07,020 --> 00:00:09,751 To do that, we need conditionals. 5 00:00:09,751 --> 00:00:12,480 Conditionals form the basis of selection. 6 00:00:12,480 --> 00:00:14,010 They allow the computer to decide 7 00:00:14,010 --> 00:00:16,650 which code to run depending on whether the answer 8 00:00:16,650 --> 00:00:20,940 to a specific question or condition is true or false. 9 00:00:20,940 --> 00:00:24,000 The first piece of a conditional is the IF statement. 10 00:00:24,000 --> 00:00:26,640 We start an IF statement with the keyword If. 11 00:00:26,640 --> 00:00:28,595 Then we add our condition, 12 00:00:28,595 --> 00:00:29,880 which can be any boolean expression. 13 00:00:29,880 --> 00:00:32,738 We always end this line with a colon 14 00:00:32,738 --> 00:00:34,320 because an IF statement comes in two parts. 15 00:00:34,320 --> 00:00:36,360 This first part, which is the condition 16 00:00:36,360 --> 00:00:39,060 and the second part, which is the instructions to run 17 00:00:39,060 --> 00:00:41,580 if that condition evaluates to true. 18 00:00:41,580 --> 00:00:45,210 In Python, we indicate those instructions using indentation. 19 00:00:45,210 --> 00:00:47,670 Any lines of code immediately following that condition 20 00:00:47,670 --> 00:00:49,530 that are indented one level in 21 00:00:49,530 --> 00:00:52,200 are considered inside that IF statement, 22 00:00:52,200 --> 00:00:54,090 if the condition evaluates to true, 23 00:00:54,090 --> 00:00:56,850 the computer will go on to execute all of the instructions 24 00:00:56,850 --> 00:00:59,256 indented inside the IF statement in order. 25 00:00:59,256 --> 00:01:01,230 When it's done, it'll go on to execute 26 00:01:01,230 --> 00:01:03,180 the rest of the program as normal. 27 00:01:03,180 --> 00:01:04,890 If the condition evaluates to false, 28 00:01:04,890 --> 00:01:07,852 the computer will skip all the lines of code 29 00:01:07,852 --> 00:01:09,180 that are indented inside of that IF statement. 30 00:01:09,180 --> 00:01:12,211 Instead, it'll jump directly to the next line of code 31 00:01:12,211 --> 00:01:13,140 that's outside of the IF statement. 32 00:01:13,140 --> 00:01:14,520 That means the first line of code 33 00:01:14,520 --> 00:01:16,350 that is indented at the same level 34 00:01:16,350 --> 00:01:18,210 as that initial if. 35 00:01:18,210 --> 00:01:19,620 Let's try an example. 36 00:01:19,620 --> 00:01:22,177 Here we have the Boolean expression, 37 00:01:22,177 --> 00:01:24,480 num_orders equals equals 10, which asks the question, 38 00:01:24,480 --> 00:01:26,310 is this the customer's 10th order? 39 00:01:26,310 --> 00:01:29,430 We can then use this Boolean value in our IF statement. 40 00:01:29,430 --> 00:01:31,230 We'll indent one print function call 41 00:01:31,230 --> 00:01:33,960 inside of the IF statement and one outside. 42 00:01:33,960 --> 00:01:37,590 The one inside will only execute if it is the 10th order, 43 00:01:37,590 --> 00:01:39,870 that is if the variable is 10th order 44 00:01:39,870 --> 00:01:42,090 contains the Boolean value True. 45 00:01:42,090 --> 00:01:44,915 We can also write this without the variable 46 00:01:44,915 --> 00:01:47,014 where we just inline that whole Boolean expression 47 00:01:47,014 --> 00:01:48,456 in the IF statement. 48 00:01:48,456 --> 00:01:50,190 Let's trace how the computer executes this. 49 00:01:50,190 --> 00:01:51,360 It starts with the first line 50 00:01:51,360 --> 00:01:54,360 and assigns a value 10 to the variable num_orders. 51 00:01:54,360 --> 00:01:55,920 Then it hits this IF statement, 52 00:01:55,920 --> 00:01:58,890 so first it evaluates the Boolean expression. 53 00:01:58,890 --> 00:02:02,880 10 is equal to 10, so this evaluates to true. 54 00:02:02,880 --> 00:02:04,020 Because it's true, 55 00:02:04,020 --> 00:02:06,750 the computer will execute the lines of code 56 00:02:06,750 --> 00:02:08,280 indented inside of the IF statement. 57 00:02:08,280 --> 00:02:11,415 First, it'll print, "Your order is free!" 58 00:02:11,415 --> 00:02:12,660 And then it'll assign the value zero 59 00:02:12,660 --> 00:02:14,430 to the variable num_orders. 60 00:02:14,430 --> 00:02:16,552 Then it just keeps moving through 61 00:02:16,552 --> 00:02:17,853 the rest of the program in order, 62 00:02:17,853 --> 00:02:20,935 so it'll print the value of num_orders, which is now zero. 63 00:02:20,935 --> 00:02:22,973 Now let's say instead num_orders 64 00:02:22,973 --> 00:02:24,215 was originally set to three. 65 00:02:24,215 --> 00:02:25,290 When the computer executes this program, 66 00:02:25,290 --> 00:02:27,420 it'll assign the value three to num_orders, 67 00:02:27,420 --> 00:02:29,070 evaluate the Boolean expression. 68 00:02:29,070 --> 00:02:31,500 Three is not equal to 10, so this is false, 69 00:02:31,500 --> 00:02:33,000 and then because it's false, 70 00:02:33,000 --> 00:02:34,740 we'll skip the lines of code indented 71 00:02:34,740 --> 00:02:36,893 inside the IF statement. 72 00:02:36,893 --> 00:02:38,797 It'll jump to the next line of code outside, 73 00:02:38,797 --> 00:02:40,997 which is print num_orders, so it'll print three. 74 00:02:40,997 --> 00:02:43,650 Note that if we didn't indent the line of code 75 00:02:43,650 --> 00:02:46,290 num_orders equal zero, it would be considered 76 00:02:46,290 --> 00:02:49,410 outside of the IF statement, so in the false case, 77 00:02:49,410 --> 00:02:51,733 num_orders would be set to zero, 78 00:02:51,733 --> 00:02:53,316 and then it would print zero. 79 00:02:53,316 --> 00:02:54,990 In the true case, we would print, "Your order is free." 80 00:02:54,990 --> 00:02:56,550 Then we would set num_orders to zero, 81 00:02:56,550 --> 00:02:58,680 and then we would print zero. 82 00:02:58,680 --> 00:03:01,080 You can write IF statements with any Boolean expressions 83 00:03:01,080 --> 00:03:02,850 as conditions, and you can put any 84 00:03:02,850 --> 00:03:05,820 and as many instructions as you want inside of them. 85 00:03:05,820 --> 00:03:08,196 With this conditional, I could make sure 86 00:03:08,196 --> 00:03:09,732 to only ask the follow-up question, 87 00:03:09,732 --> 00:03:11,610 chicken or tofu, if the user ordered pad Thai, 88 00:03:11,610 --> 00:03:13,440 because if they order the papaya salad, 89 00:03:13,440 --> 00:03:15,120 that question doesn't make sense. 90 00:03:15,120 --> 00:03:18,570 Note that in most IDEs we indent using the tab key. 91 00:03:18,570 --> 00:03:19,870 Our standard lines of code 92 00:03:20,718 --> 00:03:21,690 should line up directly with the left margin. 93 00:03:21,690 --> 00:03:24,540 That is, there should be no spaces or indents before them. 94 00:03:24,540 --> 00:03:26,790 Lines of code indented inside of an IF statement 95 00:03:26,790 --> 00:03:29,040 should be one tab key over. 96 00:03:29,040 --> 00:03:31,812 Getting the indentation right is crucial 97 00:03:31,812 --> 00:03:32,880 because this is the only way we can tell the computer 98 00:03:32,880 --> 00:03:34,740 which lines of code are inside the IF statement 99 00:03:34,740 --> 00:03:36,630 and which are outside. 100 00:03:36,630 --> 00:03:37,950 We need to be careful here though 101 00:03:37,950 --> 00:03:39,660 because now our control flow branches. 102 00:03:39,660 --> 00:03:43,523 If the user orders pad Thai, this program works fine, 103 00:03:43,523 --> 00:03:46,080 but if the user orders something else, we get a name error. 104 00:03:46,080 --> 00:03:48,180 The variable protein is only defined 105 00:03:48,180 --> 00:03:49,710 inside the IF statement. 106 00:03:49,710 --> 00:03:51,000 If the condition is false, 107 00:03:51,000 --> 00:03:53,010 this assignment statement doesn't execute, 108 00:03:53,010 --> 00:03:56,010 so the computer doesn't know a variable protein. 109 00:03:56,010 --> 00:03:57,960 To fix this, we either need to make sure 110 00:03:57,960 --> 00:03:59,850 we're only accessing the variable protein 111 00:03:59,850 --> 00:04:03,000 inside the IF statement where we're guaranteed it exists, 112 00:04:03,000 --> 00:04:05,400 or we need to initialize the variable protein 113 00:04:05,400 --> 00:04:07,503 before the IF statement, 114 00:04:07,503 --> 00:04:08,850 which guarantees that it's always defined. 115 00:04:08,850 --> 00:04:10,680 It's common in situations like this 116 00:04:10,680 --> 00:04:11,790 to initialize the variable 117 00:04:11,790 --> 00:04:14,301 to an empty or placeholder value 118 00:04:14,301 --> 00:04:16,380 like the empty string or no protein. 119 00:04:16,380 --> 00:04:19,020 Now there's no error on either possible path. 120 00:04:19,020 --> 00:04:22,080 If the order is equal to pad Thai or if it's not. 121 00:04:22,080 --> 00:04:25,425 Now that our control flow branches, when we run a program, 122 00:04:25,425 --> 00:04:26,543 we're only testing one 123 00:04:26,543 --> 00:04:29,010 of perhaps many possible passive execution through the code. 124 00:04:29,010 --> 00:04:30,450 Just because it works for one case 125 00:04:30,450 --> 00:04:31,860 doesn't mean it works for the other, 126 00:04:31,860 --> 00:04:33,540 and it's up to us to test for that. 127 00:04:33,540 --> 00:04:35,610 Otherwise, it'll be our users who suffer 128 00:04:35,610 --> 00:04:37,810 when they're the first ones to find the bug.