1 00:00:00,000 --> 00:00:10,391 [MUSIC] In this video, we're going to talk about changing the flow of control of your 2 00:00:10,391 --> 00:00:13,432 program. So far, we've been talking about programs 3 00:00:13,432 --> 00:00:16,249 that execute statements one after another after another. 4 00:00:16,249 --> 00:00:20,375 So, you can just take a look at the code and you can scan down the page and you're 5 00:00:20,375 --> 00:00:23,846 going to see how the program executes. Now, that's not quiet true, we have 6 00:00:23,846 --> 00:00:27,418 introduced functions and functions change the flow of control slightly. 7 00:00:27,418 --> 00:00:30,839 When you get to a function call, you actually go off, you execute the 8 00:00:30,839 --> 00:00:35,065 statements inside the function one by one, then you come back and you keep executing 9 00:00:35,065 --> 00:00:35,820 one by one. Now, 10 00:00:35,820 --> 00:00:40,508 This makes your programs a little bit more interesting but I still can't change the 11 00:00:40,508 --> 00:00:43,912 flow of control of my program based on values in the program. 12 00:00:43,912 --> 00:00:48,321 I might want to do something different based on data that I have available to me 13 00:00:48,321 --> 00:00:51,670 inside the program, okay? So, let me illustrate this with a very 14 00:00:51,670 --> 00:00:54,740 silly example, okay? Imagine that I'm a computer and I'm 15 00:00:54,740 --> 00:00:59,484 walking around running a computer program and that computer program tells me to give 16 00:00:59,484 --> 00:01:02,665 away $twenty, alright? So, I walk up to you, I say, hey, here's 17 00:01:02,665 --> 00:01:06,064 $twenty, okay, and then I walk to the next person, hey, here's $twenty. 18 00:01:06,070 --> 00:01:08,570 I walk to the next person, Hey, 19 00:01:08,570 --> 00:01:10,070 Here's $twenty. Now, 20 00:01:10,070 --> 00:01:13,305 If I run this program, I'm going to go broke really quickly, 21 00:01:13,305 --> 00:01:16,021 Okay? So, this is not a very intelligent 22 00:01:16,021 --> 00:01:18,795 program. I'd like to behave differently based on 23 00:01:18,795 --> 00:01:21,857 who I'm talking to. So perhaps I want to come up to you, 24 00:01:21,857 --> 00:01:24,978 You're my friend. I know you need $twenty I say, here, go 25 00:01:24,978 --> 00:01:28,271 ahead, have $twenty, Okay? Then, I walk up to the next person. 26 00:01:28,271 --> 00:01:30,409 They're a stranger. I don't know them. 27 00:01:30,409 --> 00:01:33,934 I don't want to give them $twenty so I don't give them $twenty. 28 00:01:33,934 --> 00:01:36,477 Okay? I want to change my behavior be, based on 29 00:01:36,477 --> 00:01:39,944 who I'm talking to. Now, this is what conditionals accomplish 30 00:01:39,944 --> 00:01:43,565 in computer programs. You are checking the value in the program, 31 00:01:43,565 --> 00:01:47,915 in this case, it would be the person that I'm talking to in my silly analogy, okay 32 00:01:47,915 --> 00:01:51,120 Based on that value, you execute different lines of code. 33 00:01:51,120 --> 00:01:56,774 So, let's see how that works Alright. To illustrate conditionals, I want to use my 34 00:01:56,774 --> 00:02:00,140 silly giveaway $twenty program. So, you can see here I have a function 35 00:02:00,140 --> 00:02:03,945 called Greet that takes two arguments. One is going to be a Boolean, whether or 36 00:02:03,945 --> 00:02:06,774 not you're my friend. So, this is going to be true or false. 37 00:02:06,774 --> 00:02:10,725 The other is the amount of money I have, okay And then, I'm going to do whatever I 38 00:02:10,725 --> 00:02:14,628 do when I greet you and I'm going to return the amount of money I have when I'm 39 00:02:14,628 --> 00:02:18,579 done and so, you can see down here, I have three calls to greet and the way I call 40 00:02:18,579 --> 00:02:20,970 greet is I pass true or false, so this first call, 41 00:02:20,970 --> 00:02:24,022 I'm greeting my friend. And I have, however much money I have. 42 00:02:24,022 --> 00:02:26,856 When I'm done, I update my money, and then I print it, 43 00:02:26,856 --> 00:02:28,981 Okay? Then, I would greet someone who's not my 44 00:02:28,981 --> 00:02:30,834 friend over here. So, this is false. 45 00:02:30,834 --> 00:02:33,123 And then, I greet someone who is my friend. 46 00:02:33,123 --> 00:02:34,213 Okay. So, I start out, 47 00:02:34,213 --> 00:02:38,519 Let's say, I start out with $fifteen and I walk down the street, and I meet three 48 00:02:38,519 --> 00:02:39,609 people. What happens? 49 00:02:39,609 --> 00:02:41,898 Well, I go into debt. At least I'm friendly. 50 00:02:41,898 --> 00:02:45,277 I say hi to everyone. And I'm in debt for $five and then I'm in 51 00:02:45,277 --> 00:02:47,457 debt for 25, And then I'm in debt for 45. 52 00:02:47,457 --> 00:02:50,023 Okay. I'm not a very good money manager 53 00:02:50,023 --> 00:02:53,464 obviously, right. So, let's improve this a little bit. 54 00:02:53,464 --> 00:02:57,511 Let's make me slightly more intelligent when I greet people. 55 00:02:57,511 --> 00:03:02,991 I'm going to check if you are my friend. And if so, I will do these things, okay? 56 00:03:02,991 --> 00:03:08,921 So, in Python, the way that we can alter the flow of control based on the value of 57 00:03:08,921 --> 00:03:14,484 data is to use an if statement, alright? And as you would expect from the English 58 00:03:14,484 --> 00:03:18,809 word if, if this is true, then I execute the code in this block. 59 00:03:18,809 --> 00:03:24,398 Like all other Python sort of constructs here, I end this with a colon, and then, I 60 00:03:24,398 --> 00:03:29,021 have a block of indented code. That block of indented code executes, 61 00:03:29,021 --> 00:03:33,783 alright, if the predicate, which in this case, is friend, is true, okay? 62 00:03:33,783 --> 00:03:38,889 So, I have a, the word if, then I have some Boolean logic expression, in this 63 00:03:38,889 --> 00:03:44,547 case, it's just a Boolean variable, then I have a colon, then I have a block of code 64 00:03:44,547 --> 00:03:47,773 that executes. Now let's try to run a program again. 65 00:03:47,773 --> 00:03:50,360 What happens when I run it now? Well. 66 00:03:50,360 --> 00:03:53,971 If you're my friend, I would say hi to you and give you $twenty and go into debt, but 67 00:03:53,971 --> 00:03:57,910 I meet someone who's not my friend I don't even say hi, I'm not very friendly to the 68 00:03:57,910 --> 00:04:01,100 strangers then I say hi to my friend again and go further into debt. 69 00:04:01,100 --> 00:04:03,206 Okay. That program is getting better. 70 00:04:03,206 --> 00:04:06,396 Now, I don't have to just put a Boolean variable here. 71 00:04:06,396 --> 00:04:09,105 Okay. We talked about Boolean logic last time, 72 00:04:09,105 --> 00:04:13,318 and there's a reason for that. I can put an arbitrary logic expression 73 00:04:13,318 --> 00:04:16,086 here, so let's be even smarter about our money. 74 00:04:16,086 --> 00:04:20,721 Let's not just give it away to friends. Let's only give it away if we have it. 75 00:04:20,721 --> 00:04:25,416 So, if my, if I have more than $twenty, alright,, I will say hi to you, and I will 76 00:04:25,416 --> 00:04:29,501 give you some money, Alright? Well, let's try this. 77 00:04:29,501 --> 00:04:31,850 Hm. I am no longer very friendly, right? 78 00:04:31,850 --> 00:04:33,734 I didn't give anybody money. Why? 79 00:04:33,734 --> 00:04:37,443 Cuz I never have $twenty. So, this statement always ends up being 80 00:04:37,443 --> 00:04:38,149 false, Right? 81 00:04:38,149 --> 00:04:42,565 The friend may be true or false but because it's in an and, if money is not 82 00:04:42,565 --> 00:04:47,215 greater than, true than twenty then, this is going to be false, always making the 83 00:04:47,215 --> 00:04:50,924 and expression false. Alright, this isn't really what I wanted. 84 00:04:50,924 --> 00:04:53,158 So, I can put. Something else, right? 85 00:04:53,158 --> 00:04:58,684 I want to do, I know what I want to do if you're my friend and I have more than 86 00:04:58,684 --> 00:05:04,000 $twenty, Butif you aren't my friend or I don't have more than $twenty, maybe I 87 00:05:04,000 --> 00:05:07,941 still talk to you. Don't have the exclamation point. 88 00:05:07,941 --> 00:05:10,423 I'm not as happy. I don't have as much money, 89 00:05:10,423 --> 00:05:15,162 Alright? I'm going to do this otherwise so else is a Python construct with a colon 90 00:05:15,162 --> 00:05:19,336 after it again, right, and after the colon, I can have an arbitrary block of 91 00:05:19,336 --> 00:05:22,495 code that can be as many statements in here as I want. 92 00:05:22,495 --> 00:05:25,485 But else is what you do if the predicate is not true. 93 00:05:25,485 --> 00:05:27,967 So, if the predicate is true, I run this code. 94 00:05:27,967 --> 00:05:30,505 If the predicate is not true, I run that code. 95 00:05:30,505 --> 00:05:35,964 Okay So now, what's going to happen? Well, at least I say hello to everybody. 96 00:05:35,964 --> 00:05:39,235 But I'm still not giving anybody any money, right? 97 00:05:39,235 --> 00:05:43,842 I even say hello to strangers, you know, Should I really be doing that? 98 00:05:43,842 --> 00:05:47,915 I don't, I don't really think I should say hello to strangers. 99 00:05:47,915 --> 00:05:52,857 So, let's structure this even a little bit, slightly more different, alright? 100 00:05:52,857 --> 00:05:56,596 There's another construct I can use, and that's called elif, 101 00:05:56,796 --> 00:05:58,799 Alright? Let's say, elif friend, 102 00:05:58,799 --> 00:06:03,340 Then I talk to you, I say hello. Otherwise, you're not my friend. 103 00:06:03,760 --> 00:06:07,234 Well, I've been friendly throughout this. 104 00:06:07,234 --> 00:06:10,270 Let's be mean. I'm going to print uh-huh,, and I'm going 105 00:06:10,270 --> 00:06:12,759 to mug you. No, I would never do this, okay? 106 00:06:12,759 --> 00:06:16,644 But in the program, let's just pretend I'm going to take your money. 107 00:06:16,644 --> 00:06:18,708 Alright? So now, what happened here? 108 00:06:18,708 --> 00:06:20,347 What did this elif do? Okay. 109 00:06:20,347 --> 00:06:24,657 So, first, we execute the if statement. We check if the predicate is true. 110 00:06:24,657 --> 00:06:29,332 If the predicate is true, then we execute the block of code underneath the if. 111 00:06:29,332 --> 00:06:32,550 And that's it. Then, we skip down here to return money, 112 00:06:32,550 --> 00:06:37,660 Okay? When the predicate is false in the if statement, we look at the next one, 113 00:06:37,660 --> 00:06:42,729 elif, and this is a shorthand for else if. So, else, if this predicate, which is just 114 00:06:42,729 --> 00:06:45,643 friend is true, then I execute this block of code. 115 00:06:45,643 --> 00:06:50,459 And I can have as many elifs as I want. I can only have one if and one else but I 116 00:06:50,459 --> 00:06:53,075 can have as many elifs in between if I want. 117 00:06:53,075 --> 00:06:57,475 And I don't have to have any elifs and I don't have to have an else, right? 118 00:06:57,475 --> 00:07:00,983 So, I print hello here. If this one, this predicate was false, 119 00:07:00,983 --> 00:07:03,540 then I go down here, else, else always works. 120 00:07:03,540 --> 00:07:05,800 So no matter what, okay, So this means, 121 00:07:06,160 --> 00:07:09,888 If you're, if I don't have more than $twenty and you're my friend or if you're 122 00:07:09,888 --> 00:07:14,293 not just my friend, then I'm going to take your money, alright? That can be very nice 123 00:07:14,293 --> 00:07:16,666 alright? Now, let's run this program, what happens 124 00:07:16,666 --> 00:07:19,629 now? Okay, first person I walk up to is my 125 00:07:19,629 --> 00:07:24,046 friend, I say, hello, I'm a little bit sad, I don't have a lot of money so I keep 126 00:07:24,046 --> 00:07:27,648 my $fifteen dollars then. Second person, [laugh] I'm going to take 127 00:07:27,648 --> 00:07:31,367 your money Now, I have $25. Last person, hey, I'm pretty happy Now, I 128 00:07:31,367 --> 00:07:34,218 have $25, I say, hi and I give you my money alright? 129 00:07:34,218 --> 00:07:39,078 So, you can see that I can alter the flow of control of the program based on values 130 00:07:39,078 --> 00:07:42,591 that are in the program. So based on the value of friend, and 131 00:07:42,591 --> 00:07:45,752 money, in this case. And it doesn't matter that there are 132 00:07:45,752 --> 00:07:48,621 arguments in the function. These could be anywhere. 133 00:07:48,621 --> 00:07:52,193 They could be local variables, they could be global variables. 134 00:07:52,193 --> 00:07:56,980 I can change what code gets executed. This is a very powerful concept. 135 00:07:57,300 --> 00:08:01,193 I want to show you another example of conditionals to help you get the hang of 136 00:08:01,193 --> 00:08:03,520 them. Now, in this program, I basically am going 137 00:08:03,520 --> 00:08:05,745 to test if a particular year is a leap year. 138 00:08:05,745 --> 00:08:08,071 And so, I wrote a function called isleapyear.. 139 00:08:08,071 --> 00:08:12,066 That's a programmer's convention when you're just testing a predicate with a 140 00:08:12,066 --> 00:08:14,797 function, you start the function name with the word is. 141 00:08:14,797 --> 00:08:19,158 Basically, I'm testing year and returning true or false based on whether or not it 142 00:08:19,158 --> 00:08:22,461 is a leap year, okay? And this function is just one conditional 143 00:08:22,461 --> 00:08:26,251 that has several cases, alright? I'm testing three different things and 144 00:08:26,251 --> 00:08:29,500 returning true or false based on the results of those tests. 145 00:08:29,500 --> 00:08:33,615 Now, leap year test is actually pretty simple if you ignore the corner cases. 146 00:08:33,615 --> 00:08:37,675 I need to just check is the year a multiple of four, if so, it's a leap year 147 00:08:37,675 --> 00:08:39,950 otherwise, it's not. So, you can see up here. 148 00:08:39,950 --> 00:08:44,567 I test year modular four could equal zero. That means if I divide the year by four 149 00:08:44,567 --> 00:08:49,409 and there's no remainder, well then, it's a leap year so I return true and if it's 150 00:08:49,409 --> 00:08:52,000 not, I exceed the else clause and return false, 151 00:08:52,000 --> 00:08:54,399 Okay? However, there are these two extra cases 152 00:08:54,399 --> 00:08:58,719 that if the year is a multiple of hundred, then it's not a leap year unless it's also 153 00:08:58,719 --> 00:08:59,999 a multiple of 400, Right? 154 00:08:59,999 --> 00:09:03,625 Now, you'll notice that I have to execute these checks in this order, 155 00:09:03,625 --> 00:09:07,464 I will not get the right answer. I have to first check 400, then 100, then 156 00:09:07,464 --> 00:09:11,837 four, and then do the else on. I invite you to experiment and move these around 157 00:09:11,837 --> 00:09:14,664 and find out whether or not you get the right answer, 158 00:09:14,664 --> 00:09:17,170 Right? You know that you won't, cuz I told you 159 00:09:17,170 --> 00:09:20,690 but you should at least convince yourself that you understand why. 160 00:09:20,690 --> 00:09:26,434 I also have a conditional down here that shows, allows us to print out a nice 161 00:09:26,434 --> 00:09:30,537 message based on the result of this leap year function. 162 00:09:30,537 --> 00:09:35,311 Okay, so let's run this. Is the year 2100 going to be a leap year? 163 00:09:35,311 --> 00:09:39,340 No, it is not. Is the year 2000 going to be a leap year? 164 00:09:39,340 --> 00:09:44,830 Yes, it is. Is the year 2012 a leap year? 165 00:09:44,830 --> 00:09:49,420 Yes, it is. Is the year 2009 was that a leap year? 166 00:09:49,420 --> 00:09:52,146 No it's not. Okay. And you can play around with this 167 00:09:52,146 --> 00:09:56,477 and see that it does catch all the cases by executing these conditional in order 168 00:09:56,477 --> 00:10:00,060 the if, elif, else, you're going to get the right answer in all cases. 169 00:10:00,060 --> 00:10:04,580 There are very few interesting programs that execute directly in a straight line. 170 00:10:04,580 --> 00:10:08,932 So, the concept of conditional control flow that I've introduced in this video 171 00:10:08,932 --> 00:10:12,448 are critically important for pretty much all computer programs. 172 00:10:12,448 --> 00:10:16,019 You don't want to give away $twenty to everybody you meet, do you? 173 00:10:16,019 --> 00:10:17,749 [laugh] I certainly don't. Okay. 174 00:10:17,749 --> 00:10:22,437 So, I need to be able to have the ability to change what I do based on the values of 175 00:10:22,437 --> 00:10:26,120 data in the program, okay? And the ability to use arbitrary Boolean 176 00:10:26,120 --> 00:10:30,250 logic and predicates gives you a powerful and flexible mechanism to do so. 177 00:10:30,250 --> 00:10:34,471 So, you're going to be using conditionals to change the code that you execute in 178 00:10:34,471 --> 00:10:38,858 your programs throughout the rest of this course and throughout your programing