[MUSIC] In this video, we're going to talk about changing the flow of control of your
program. So far, we've been talking about programs
that execute statements one after another after another.
So, you can just take a look at the code and you can scan down the page and you're
going to see how the program executes. Now, that's not quiet true, we have
introduced functions and functions change the flow of control slightly.
When you get to a function call, you actually go off, you execute the
statements inside the function one by one, then you come back and you keep executing
one by one. Now,
This makes your programs a little bit more interesting but I still can't change the
flow of control of my program based on values in the program.
I might want to do something different based on data that I have available to me
inside the program, okay? So, let me illustrate this with a very
silly example, okay? Imagine that I'm a computer and I'm
walking around running a computer program and that computer program tells me to give
away $twenty, alright? So, I walk up to you, I say, hey, here's
$twenty, okay, and then I walk to the next person, hey, here's $twenty.
I walk to the next person, Hey,
Here's $twenty. Now,
If I run this program, I'm going to go broke really quickly,
Okay? So, this is not a very intelligent
program. I'd like to behave differently based on
who I'm talking to. So perhaps I want to come up to you,
You're my friend. I know you need $twenty I say, here, go
ahead, have $twenty, Okay? Then, I walk up to the next person.
They're a stranger. I don't know them.
I don't want to give them $twenty so I don't give them $twenty.
Okay? I want to change my behavior be, based on
who I'm talking to. Now, this is what conditionals accomplish
in computer programs. You are checking the value in the program,
in this case, it would be the person that I'm talking to in my silly analogy, okay
Based on that value, you execute different lines of code.
So, let's see how that works Alright. To illustrate conditionals, I want to use my
silly giveaway $twenty program. So, you can see here I have a function
called Greet that takes two arguments. One is going to be a Boolean, whether or
not you're my friend. So, this is going to be true or false.
The other is the amount of money I have, okay And then, I'm going to do whatever I
do when I greet you and I'm going to return the amount of money I have when I'm
done and so, you can see down here, I have three calls to greet and the way I call
greet is I pass true or false, so this first call,
I'm greeting my friend. And I have, however much money I have.
When I'm done, I update my money, and then I print it,
Okay? Then, I would greet someone who's not my
friend over here. So, this is false.
And then, I greet someone who is my friend.
Okay. So, I start out,
Let's say, I start out with $fifteen and I walk down the street, and I meet three
people. What happens?
Well, I go into debt. At least I'm friendly.
I say hi to everyone. And I'm in debt for $five and then I'm in
debt for 25, And then I'm in debt for 45.
Okay. I'm not a very good money manager
obviously, right. So, let's improve this a little bit.
Let's make me slightly more intelligent when I greet people.
I'm going to check if you are my friend. And if so, I will do these things, okay?
So, in Python, the way that we can alter the flow of control based on the value of
data is to use an if statement, alright? And as you would expect from the English
word if, if this is true, then I execute the code in this block.
Like all other Python sort of constructs here, I end this with a colon, and then, I
have a block of indented code. That block of indented code executes,
alright, if the predicate, which in this case, is friend, is true, okay?
So, I have a, the word if, then I have some Boolean logic expression, in this
case, it's just a Boolean variable, then I have a colon, then I have a block of code
that executes. Now let's try to run a program again.
What happens when I run it now? Well.
If you're my friend, I would say hi to you and give you $twenty and go into debt, but
I meet someone who's not my friend I don't even say hi, I'm not very friendly to the
strangers then I say hi to my friend again and go further into debt.
Okay. That program is getting better.
Now, I don't have to just put a Boolean variable here.
Okay. We talked about Boolean logic last time,
and there's a reason for that. I can put an arbitrary logic expression
here, so let's be even smarter about our money.
Let's not just give it away to friends. Let's only give it away if we have it.
So, if my, if I have more than $twenty, alright,, I will say hi to you, and I will
give you some money, Alright? Well, let's try this.
Hm. I am no longer very friendly, right?
I didn't give anybody money. Why?
Cuz I never have $twenty. So, this statement always ends up being
false, Right?
The friend may be true or false but because it's in an and, if money is not
greater than, true than twenty then, this is going to be false, always making the
and expression false. Alright, this isn't really what I wanted.
So, I can put. Something else, right?
I want to do, I know what I want to do if you're my friend and I have more than
$twenty, Butif you aren't my friend or I don't have more than $twenty, maybe I
still talk to you. Don't have the exclamation point.
I'm not as happy. I don't have as much money,
Alright? I'm going to do this otherwise so else is a Python construct with a colon
after it again, right, and after the colon, I can have an arbitrary block of
code that can be as many statements in here as I want.
But else is what you do if the predicate is not true.
So, if the predicate is true, I run this code.
If the predicate is not true, I run that code.
Okay So now, what's going to happen? Well, at least I say hello to everybody.
But I'm still not giving anybody any money, right?
I even say hello to strangers, you know, Should I really be doing that?
I don't, I don't really think I should say hello to strangers.
So, let's structure this even a little bit, slightly more different, alright?
There's another construct I can use, and that's called elif,
Alright? Let's say, elif friend,
Then I talk to you, I say hello. Otherwise, you're not my friend.
Well, I've been friendly throughout this.
Let's be mean. I'm going to print uh-huh,, and I'm going
to mug you. No, I would never do this, okay?
But in the program, let's just pretend I'm going to take your money.
Alright? So now, what happened here?
What did this elif do? Okay.
So, first, we execute the if statement. We check if the predicate is true.
If the predicate is true, then we execute the block of code underneath the if.
And that's it. Then, we skip down here to return money,
Okay? When the predicate is false in the if statement, we look at the next one,
elif, and this is a shorthand for else if. So, else, if this predicate, which is just
friend is true, then I execute this block of code.
And I can have as many elifs as I want. I can only have one if and one else but I
can have as many elifs in between if I want.
And I don't have to have any elifs and I don't have to have an else, right?
So, I print hello here. If this one, this predicate was false,
then I go down here, else, else always works.
So no matter what, okay, So this means,
If you're, if I don't have more than $twenty and you're my friend or if you're
not just my friend, then I'm going to take your money, alright? That can be very nice
alright? Now, let's run this program, what happens
now? Okay, first person I walk up to is my
friend, I say, hello, I'm a little bit sad, I don't have a lot of money so I keep
my $fifteen dollars then. Second person, [laugh] I'm going to take
your money Now, I have $25. Last person, hey, I'm pretty happy Now, I
have $25, I say, hi and I give you my money alright?
So, you can see that I can alter the flow of control of the program based on values
that are in the program. So based on the value of friend, and
money, in this case. And it doesn't matter that there are
arguments in the function. These could be anywhere.
They could be local variables, they could be global variables.
I can change what code gets executed. This is a very powerful concept.
I want to show you another example of conditionals to help you get the hang of
them. Now, in this program, I basically am going
to test if a particular year is a leap year.
And so, I wrote a function called isleapyear..
That's a programmer's convention when you're just testing a predicate with a
function, you start the function name with the word is.
Basically, I'm testing year and returning true or false based on whether or not it
is a leap year, okay? And this function is just one conditional
that has several cases, alright? I'm testing three different things and
returning true or false based on the results of those tests.
Now, leap year test is actually pretty simple if you ignore the corner cases.
I need to just check is the year a multiple of four, if so, it's a leap year
otherwise, it's not. So, you can see up here.
I test year modular four could equal zero. That means if I divide the year by four
and there's no remainder, well then, it's a leap year so I return true and if it's
not, I exceed the else clause and return false,
Okay? However, there are these two extra cases
that if the year is a multiple of hundred, then it's not a leap year unless it's also
a multiple of 400, Right?
Now, you'll notice that I have to execute these checks in this order,
I will not get the right answer. I have to first check 400, then 100, then
four, and then do the else on. I invite you to experiment and move these around
and find out whether or not you get the right answer,
Right? You know that you won't, cuz I told you
but you should at least convince yourself that you understand why.
I also have a conditional down here that shows, allows us to print out a nice
message based on the result of this leap year function.
Okay, so let's run this. Is the year 2100 going to be a leap year?
No, it is not. Is the year 2000 going to be a leap year?
Yes, it is. Is the year 2012 a leap year?
Yes, it is. Is the year 2009 was that a leap year?
No it's not. Okay. And you can play around with this
and see that it does catch all the cases by executing these conditional in order
the if, elif, else, you're going to get the right answer in all cases.
There are very few interesting programs that execute directly in a straight line.
So, the concept of conditional control flow that I've introduced in this video
are critically important for pretty much all computer programs.
You don't want to give away $twenty to everybody you meet, do you?
[laugh] I certainly don't. Okay.
So, I need to be able to have the ability to change what I do based on the values of
data in the program, okay? And the ability to use arbitrary Boolean
logic and predicates gives you a powerful and flexible mechanism to do so.
So, you're going to be using conditionals to change the code that you execute in
your programs throughout the rest of this course and throughout your programing