Welcome back, everyone, to our second discussion on control flow, which we'll be discussing if Elif
and else.
So as we begin to learn about control flow, we often only want certain pieces of code to execute when
a particular condition has been met.
For example, if my dog is hungry, which is a condition.
True or false, then I will feed the dog, which is some action so you can think of if elephant statements
as checking for a condition.
And if that condition is true, perform an action.
So to control this flow of logic, we used some keywords.
If Elif and else.
Control flow syntax makes use of colons and indentation, which we haven't actually explored too much
here in Python.
You'll notice that when you enter in your code editor, if it knows it's a Python file, it will sometimes
actually automatically begin to indent your code.
This indentation system of white space is crucial to Python, and it's what sets it apart from other
programming languages and what makes it really readable.
So we'll be discovering this as we talk about control flow.
In fact, let's talk about the syntax now.
So here's a syntax of a simple if statement you are going to say, if some condition, we could imagine
some condition is just a Boolean for right now.
True or false, then colon and then we execute some code.
And if else statement looks like this, we have if and else at the same indentation.
So we say if some condition happens to be true, colon execute some code, then we say else, meaning
if that condition was not true.
Go ahead and do something else.
We can say if the dog is hungry, feed the dog else, don't do anything.
Now we can check for as many conditions as we like before we actually perform else, we can say if we
have some condition, execute some code L, if we can say else, if there's some other condition, do
something different and so on and so on, you can stack as many lives as you want.
So we have.
If statements very, very simple, if else allows you to do something of that condition isn't true.
And if you want to check for multiple conditions before your statements you have, if and else, let's
see some examples of this in our code editor.
OK, here I am inside example that pie.
What I'm going to do is start off with just a simple if statement it's going to be the truth or the
simplest statement possible.
And I say if and notice that there's actually code here to help you out if you click, if it's telling
you, OK, you're starting an if statement, so say if true colon.
And note that once you type out a colon, if you were to hit enter, right now we are automatically
indented, meaning that the code editor understands, Hey, you have a block of code here that's going
to execute when this if statement happens to be true, so going say print.
True.
And I can add as much code here as I want inside this block, so we can say another line again, notice
that if something is true, we're going to print out or execute all the indented code here.
So I'm going to save that.
Let's go ahead and run this.
We can see true in other line.
Let's go ahead and change this to be false.
So if false?
Then if I run this again.
Notice, I don't get back anything, so this is only going to execute if it's actually true or false.
Now typically, you're not going to see the bullion themselves.
Instead, you're going to see some condition to check.
So what kind of conditions can we actually check?
Well, let's do the password.
Check again.
I'm going to say password.
It's just my password.
And let's check our, let's say, our database password or stored a password.
Is equal to my password, so I'm going to say, if we have some condition, I can say the condition
is going to be checking, Hey, is password.
Variable.
Equals to.
Stored password lecturer, we spell that right.
There we go.
No, the actual state of equal science here, I'm saying, equals equal.
So if my stored password is equal, isn't it quality to the password, then this will return a Boolean
that I will then do a variable assignment for some condition.
And then I can print here something like passwords match.
OK.
So again, have these two variables.
I'm checking to see if they're equal to each other.
I'm assigning that bullion to some condition and then I'm printing out if they're true print passwords
match.
So remember if statements only execute if the bullion is true?
So it looks like the passwords do match.
And in fact, I don't technically need to do this extra step of this variable assignment.
Instead, I can just grab this actual comparison operation.
And put it in the if statement itself.
And this is typically how you're going to see these sort of things.
So if password is equal to stored password print passwords, match and now I can run this and I see
passwords match.
If I were to edit one of these so that it matched, so just add some extra code there and then run this
again, you're not going to see any output because this condition was not true.
OK, so what happens if that condition is not true?
I don't actually see anything or report it, if I did want to see something, report it.
I can check with an else statement.
So if else means if this is not true.
Else, go ahead and do something.
Notice how the else doesn't have any sort of condition to be met.
Else only executes if all the if statements are elif statements above it happened to be false.
So then I can prints here.
No password match.
All right.
So again, I'm checking, hey, if the password matches my stored password, I'll print out the passwords
match else, I'll print out no password match and take a close look at the invitation because this is
absolutely key with Python.
Notice how, if and else are linked together.
And this white line here in the Visual Studio code actually kind of tells you that they're linked.
And it also tells you, Oh, by the way, you have these indented blocks of code here.
So now what I'm going to do is I'm going to go ahead and save this.
And let's run this.
The passwords currently do not match, so I should expect the result to say no password match.
OK, now let's say I'm some sort of admin user, so I can say admin.
Is equal to true.
And imagine if I'm an admin, I should be able to maybe override the password condition.
So let's add in yet another check.
So I'm going to say if the passwords match.
Go ahead and print passwords match, then add in another condition to check with Elif.
So this is LCF.
And then I do have to actually pass any condition here.
So then I'll say Elif, and then I can say something like admin.
No admin has actually.
It's going to be bullying itself.
Then I can print.
Passwords did not match, but.
Admin granted or whatever you want to say here.
And now take a look at the logic, I'm going to zoom out just a little bit so we can see more of this
logic code here.
All right.
So I have the password condition check, and then I also have this variable, which is a Boolean admin,
and you're going to see this a lot, this sort of if condition where it's just a single check on some
variable, it's Boolean.
So if admin, which is if true print passwords that match the admin granted.
And I can know that the passwords did not match because if Elephant L statements go in order from top
down.
So it's first going to check to the passwords match if that's not true.
Check if this condition is true.
Is are they an admin true or false?
If that's true, go ahead and print this and we can stop else.
If the passwords didn't match and they weren't an administrator.
Go ahead and print no password match.
And in this case, I can also report back and not in admin.
I know they're not in admin and I know the passwords in a match because if by the time we get to else
these have an executed, then I know those conditions weren't true.
So if I save this, what do I expect the result to be right now?
Well, the passwords don't match.
However, I do have admin set to true, so I should expect this to say passwords that Match had granted.
And there it is passwords of the admin grants.
It lets change admin to false.
Save that change.
And out says password, match or know, password match and not an admin.
And then finally, let's just double check and make the passwords match.
And we should expect the first if statement to execute.
There it is.
Passwords matched something to note here is if I were to actually change this to true, that means this
Elif statement, this admin.
That's also true.
However, if statements that are linked together, they're only going to execute the very first true
condition so they won't go ahead and keep printing out all of these.
That if statement is only linked to the very first true one.
So if I run this, I'm only going to see passwords matched, even though I am an equal to true.
That's just something to keep in mind that it's going in order from top down.
And if you have multiple stacked elif statements like this, so you can keep adding these as many as
you want for checking for different conditions, they can look like this if this is true, that this
else, if this is true, that continue and continue until you get to the very ELSE. with no condition
to check and then do something.
All right.
So that's it for if Elif and else statements, they're going to be really powerful once we're able to
connect them with loops, which is what we're going to discuss next.