0:06
So far we've been talking about programs that execute effectively in a straight line.
I can read my program from top to bottom and know exactly what's going to happen, right?
Line one's gonna execute first, then line two,
then line three, then line four and so on.
In this video we're going to talk about changing the flow of control,
where I don't execute my code in a straight line.
Now, we've already seen this a little bit, right?
This is what functions do.
When I'm executing my code and I call a function,
I change the flow of control off into the function's body.
I execute those lines one by one and then I come back to where I was.
And I can call as many functions as I want and I can call the same functions
over and over again so you are changing the flow of control there.
However, in this video we're gonna find out,
how can we change the flow of control of a program based on
the value of something within the program as it's running.
So maybe the user input something or maybe
we're testing some other condition that's true or
false at any given time within the program and we
wanna do something different based on that check.
And so that's what we're going to learn about here.
Let's see what we can do?
So before we talk about Python,
I wanna motivate conditional statements with a little bit of an example here.
Let's imagine that I'm a computer and I'm executing a program.
And my program says whenever you meet someone,
hey say "hi" and give them $20.
Right? And that's my program.
So I walk around and I see a person,
and I say,"Hey, here's $20".
I see somebody else, "Hi, here's $20".
And I see the next person, "Hi,
here's $20". I just keep doing this.
All right. Now, that's kinda silly program.
Right? First of all,
I'm gonna run out of money at some point.
Right? This is gonna get pretty expensive pretty fast.
And I actually have no way of not giving you $20.
All right. So we'd like to, you know,
maybe say, hey let's be a little bit smarter about this.
Furthermore, maybe I only wanna say
"hi" if you're my friend and I know who you are. I'm not very friendly.
Okay. So, we wanna be able to change our behavior based on who it is that we see.
All right? Do I want to say hi to them or not?
Do I actually want to give them $20 or not?
Right. Do I have $20 to give?
Okay. So we need to start thinking about how
do we change the behavior of the program based
on what's going on inside of the program. All right.
So here's a very simple function that is called "greet" and it takes two inputs.
One is a Boolean which is friend.
Whether or not the person is my friend or not.
And the other is money,
which is how much money I have right now.
And based on those inputs I'm going to decide what I actually want to do.
Okay. Now, let's remember I'm antisocial,
so I only wanna say "hi" to you,
if I already know you, if you're already my friend.
Okay. And I can't help but give you $20.
Okay. I don't care who you are. I'm going to give you 20 dollars
but let's make sure that I actually have $20.
Okay. So if we look inside this function you can see that there are two "if statements".
Alright. And they're called "if statements"
because we use the keyword "if" to start them off.
Right. And after the keyword "if",
we have to have some sort of condition.
Now, the condition has to be an expression that evaluates to some Boolean value.
Now, we've learned about Boolean logic and we've learned
about arithmetic comparisons and other types of comparisons in Python.
This is exactly what we want to be able to use here.
Right? Wanna write an expression that's going to check
some condition and evaluate to either true or false.
And an 'if statement" reads like English.
If the condition is true,
then do what we're... what we have in the body of the "if statement".
Now, just like functions,
if statements end with a colon.
So we have "if", the condition and then a colon.
And then after that,
the body of the "if statement" is indented just
like functions and it's indented over four spaces,
just like functions are, by convention.
Right. And so everything that's indented is inside the body of the "if statement".
So, if the condition is true all of the indented statements will execute.
If the condition is false,
none of them will execute.
Right. So, we evaluate the condition.
If it's true we run that code and if it's false we just skip it all.
Okay. So, if we look at this piece of code,
we have these two "if statements".
The first one checks if friend is true.
Okay. So, if you're my friend then I'm going to print "hi".
Say "hi" to you. Okay. The second one checks
do I have enough money to be giving money away. All right.
So, if money is greater than 20 than I do I'm gonna
give you my $20 and I return how much money I have left.
Okay. So let's run this program. We start out with $25
and I walk by somebody who is not my friend.
Okay. So, you'll notice that in the output here,
it does not say "hi" because,
again, I'm pretty anti-social.
Right. But I did give away $20 so I only have five dollars left.
Okay. All right. Let's keep going.
Let's walk around some more.
Find the next person that we see.
Okay. This person is my friend.
All right. But I only have $5 so I do say "hi".
Okay. I'm friendly to my friends, at least.
But I didn't have $20 to give my friend so that's very sad.
Right. So I continued to have my $5 and I keep walking around, find somebody else.
All right. Another friend.
Okay. And, you know,
I say "hi" again but I don't give away my money.
All right. So we can appreciate here the value of conditionals.
Right. I had a single function.
Okay. The function did different things when I ran it with different inputs.
Okay. It checked if the first input was true or false.
Right. If the... If I'm a friend and decided whether or not to say "hi".
So, I didn't have to write two functions here.
I didn't have to write a greet friend function and a greet stranger function.
And even if I did write those two functions I have to figure out which one
to call so I'd still have to use some sort "if statement" somehow.
Right. This way, I can just write one function that takes friend as an input.
If it's true, I print "hi" if it's not, I don't.
And similarly, I don't have to check how much money I have outside.
Again, I'd have to use some sort "if statement" or some other variant of that, outside.
I don't have to basically hardwire into
my program how much money I have initially so that,
you know, this will work out.
Okay. If I change my money here to,
you know, I start out with a hundred dollars,
I should be able to give everybody $20.
Right. Whether they're my friend or not.
And you can see that's exactly what happens.
First person is not my friend but I still give him 20, so I have 80 left.
Okay, and the second person is my friend,
give him $20, so I have 60 left.
Third person, my friend, give him another 20.
Now I have $40 left.
Okay. So by using these "if statements",
I can change the flow of control of my program and allow
you to conditionally execute parts of the program based
on some conditional which is an expression that evaluates to either true or false.
And if it's true, I execute the code in the "if statement".
If it's false, I do not execute the body of the code in the "if statement".
One final thing I wanna make sure that you recognize is that once
the indentation ends the body of the "if
statement" is over and you are on to the next piece of code.
That code will execute no matter what.
Right. That code is after the "if statement".
It doesn't matter if the condition was true or false,
the next code that's indented at the same level as the original "if" will execute next.
Okay. This is what allows me to give away $20 whether you're my friend or not.
Okay. It doesn't matter if I say "hi" to you.
I will always execute the second "if
statement" and check if I have $20 and give it away if I do.
Conditionals allow my programs to get much more interesting.
I can actually check values while the program is
executing and decide whether or not I want to run certain pieces of code.
Right. The "if statement" here is the basic building block where I take
a condition or an expression that evaluates to the Boolean true or a Boolean false.
And if it is true then I execute the code in the body of the "if statement".
If it's false I don't.
That allows me to do much more interesting things with my program.
And, in particular, it allows me to stop giving away $20 when I don't have it.