Python Institute PCAP – Control Flow
- Control Flow: If & Else Statements
So far in the course, we’ve been going line by line, right? We’ve been writing code one line after the next. And the interpreter basically executes everything, all of the lines of code that we have, right? If we have ten lines of code, it runs through all of them. But let’s say we want to control the flow of our program and say that if this happens, then do this, otherwise do this. That decision making process of which line of code needs to run is known as control flow. And we can write programs so that they can perform various tasks.
And if a certain task doesn’t happen, then something else happens. And if we run into some kind of an error, then do this and hopefully you get the idea. So let’s go over an example of this. So the way we deal with control flow is through conditions, okay? And we talked a little bit about conditions earlier when we were talking about comparers and operators. When you need to compare one thing with the, with the other, you’re essentially forming a condition. So let’s go over an example. If, if I write a statement like if now this is a keyword, notice that the text editor highlighted this. If five is less than six, then print. Yes, five is less than six.
Now this will always run. If I run this, it’s of course always going to print. Five is less than six, keep running this. This line of code will always run. Now if I have an else here, because an if else is used to run the alternative. So in case five is not less than six, what should be done? And so I can say in this situation, this will never be printed. Okay? So this particular line of code will never ever be printed based on this given condition. So what is the condition? This is the condition. We’re using a comparison operator in the middle where we do five is less than six.
Now five is of course always going to be less than six. So this condition will always evaluate to true. And so this line will always be printed. And this will never be printed as the code currently lies. All right? So let’s run this. And of course you’ll never see this printed. And the text editor is smart enough to know that, hey, this line will never be printed. Notice it’s highlighting this entire line. If you hover your mouse on this, it’s saying this code is unreachable. Right? Now if I have a variable, for example, we can say elephant is less than hippo. Let’s say we’re comparing weights and we give the elephant the weight £800 and hippo the weight £400 or whatever. I’m not sure how these animals weigh, probably a lot more than what I’ve typed here. But it doesn’t matter for the sake of this example.
Now this is variable, we’re involving variables here. And so the editor is not going to go the extra mile and try to figure out what our variables are and highlight the line that will never be reachable. But in this case, we know that elephant is greater than hippo, it’s heavier than hippo. So this particular comparison, what is this going to evaluate to this condition is saying elephant is less than hippo. So this is in this case going to evaluate to false. And so this line will be printed. And I can change this. Let me, let me change the code here. We can say elephant. Elephant is heavier than hippo. All right? And in this top portion, if elephant is less than hippo, we can say elephant is less than hippo or weighs ways less than hippo. So we know that this line will never be run. If we leave the code as it is right now. Elephant is £800, hippo is 400. So this condition right here will always evaluate to false with the current values that we have assigned. And so this is going to be printed. So if we run this notice, we run it over and over again, it’s always going to say elephant is heavier than hippo. Now, if I change these values and change this to for example, well, let’s make hippo £900. So elephant actually now weighs less than hippo. So now if I run this notice, this line is going to be executed.
And this part of the code will never be run based on the current values that I’ve assigned. So in this case, this condition evaluates to true. So hopefully you get the general idea that after the if keyword, you have a condition, all right? And this condition needs to evaluate to either a true or a false. And then you can have an alternative case using the else clause. Right here we have else. And so if this condition evaluates to false, only in that case this line will run. Now, we talked about Boolean expression. So this essentially evaluates to a Boolean expression. I can prove it to you. Let me just get rid of this and say value. And we can say elephant is less than hippo. And let’s print whatever value is. Make sure to surround that with parentheses.
And of course it’s going to say true, because in this case, elephant is less than hippo. So this value is a Boolean data type and we’re printing that it’s true. So you might as of all, you can have a straight if statement where you say true, if true print, this will always be printed, all right? You can keep running this. It’s always going to be printed. And I can have an else here and say this will never be printed. So right off the bat, the interpreter knows that, hey, we’ve got a condition that’s true. So this is always going to be true. This is always going to run. This is unreachable code.
Hover your mouse over here. This code is unreachable. This will never be run. Now, what some people do is surround their conditions with parentheses. And I like that approach. Personally, I like to separate various comparisons using the parentheses. So I can very well wrap this in a parentheses. And this code will work exactly the same way. When we have a condition where we can say elephant is greater than hippo, and we can have another, for example, expression where we have an and clause and we inside of parentheses can say three is greater than two. Now we’ve got two expressions here. Now together this is going to evaluate to either true or false. And so with the order of operations we’ve got parentheses, these parentheses are going to be evaluated at first. So three is in fact greater than two. So this is true. But then elephant is less than hippo. This is also true with the current values that have been assigned. So the overall value of this is going to be true.
And so in this case, we’ll say this line is printed. Now, since we’re learning here, I’m keeping things very simple and we’re just printing, this line is printed and this will never be printed. This is obviously we’re going to get into more comprehensive examples where we get into more advanced coding. But for now, that’s what I’ve been typing, right? So don’t worry if this is too basic or too advanced, you’re going to catch up regardless of where you are. But let me make this a bit easier to understand. Instead of saying this line is printed, I could just say that the if statement evaluated to true, just to be more specific. And we can say if statement evaluated to false. Okay? That’s probably a bit more specific and easier to understand when you run the code. Now again, we’ve got these parentheses. And for simple conditions, when you have just one or two conditions, you don’t really need these parentheses.
But if you have a really long set of criteria or conditions that you’re checking, it’s probably helpful to wrap those conditions in parentheses like this. But in our example, if I just leave elephant is less than hippo. And let’s just do that, we don’t need the parentheses, okay? And this is easier to read. Now understand, in each of these if statement or else statement, we can have further lines of instructions, right? In the LS clause, we can have comments, we can have other if statements and else statements. We can have loops and all types of coding structures that we’re going to get into later. So if I was to, for example, add another if statement inside of this if and say that five is less than seven, for example, right? This is another condition inside of this if statement and then we’re going to print, you know, five is less than seven. Okay? Now in this case, if I run this notice, it’s going to print this first part, the if statement of values are true. And then it’s also going to print five is less than seven. But if I was to change this around and say that elephant is greater than hippo, then this evaluation, this will evaluate to false, wouldn’t it? So do you think any of the top portion is going to run? Of course it’s not, right? This if statement will not even be run. All of these lines of code will be skipped. The interpreter is not even going to go through this code.
The interpreter is going to see if this evaluates to true. If it doesn’t, it’s going to jump. It’s going to skip all these lines and jump to the else clause, okay? And so that’s how control flow works. You skip lines. The interpreter is going to skip lines that don’t need to be evaluated. And later on we’re going to get into loops, which means running a line of code or an instruction over and over and over again. All right? So that’s how you control the flow of the program. We’re going to continue to build on this decision making if else statements. There’s also something called L if to provide further alternatives. And so we’re going to look at that in the next lesson.
So let me wrap it up here. Practice with this concept, change the values around, create your own variables and play around with nesting your own if else statements and so on. And by the way, we have this if clause, we can also, of course have an else clause, okay? And this would be printed and it could be anything. Now, again, the interpreter is smart enough to know that since five is less than seven, this will never be printed. That’s why it’s highlighting this. But understand that as part of this if statement, all of this code right here falls under the umbrella of this condition right here. Elephant is greater than hippo, and this falls under the umbrella of this condition.
So hopefully you’re beginning to see the different layers of if L statements we can have. Now, you can have ten or 15 different layers of nested if L statements, and that would be a very poor design. Typically I don’t like to go in too much with nesting if L statements. If I need to go to level three, meaning you have an if inside of an if inside of an if, that’s complicated enough, okay? I don’t even like that. Two layers is good enough. But if you start to pass three or four layers of nesting, chances are you’re writing really bad code and you want to try to avoid that and reorganize and refactor your code so that you don’t have this kind of nesting of conditions happening. Okay? So that’s just a code design advice for you. And I’ll continue to sprinkle some of this advice as.
- Control Flow: Elif Statements
I talked about the if statement and the else statement. And so that’s typically used together. Well, you can use an if alone, but the else must always be used with an if clause. We’re going to get into some more examples if that’s confusing, so don’t worry. But the idea was that after the if clause, you have a condition. And if that condition is met, then the code that’s within that if section is going to be run. Otherwise the alternative would be run, which is the code that’s in the else section. So in this lecture, we’re going to talk about how we can incorporate multiple alternatives. So if something takes place, then do this. If something else takes place, then do that. If something else, then do that. And then finally, if none of those alternatives are met, then finally do something else, right? And that’s typically an if. So let me show you how we could do that in code. And when you see the code, it’ll make plenty of sense. So we’ll start by defining a variable called animal.
Let’s say that we have a cow, all right? Now I can have if statements. So let’s define an if statement. We can say if the animal is equal to cal. So now this is a comparison operator, the equality operator that is not using less than or greater than we’ve talked about this operator before. So this is to check if both things are equal. So if animals equal to cow, then we’ll say that it prints. We can say that it eats grass. Now what if the animal is something else? We can have a variety of animals. So I’m going to have another condition and that’s used with L if, not else, but L if. This is a new keyword that I’m introducing to you in this lecture. And in this case, I can present another alternative and say, hey, if animal is equal to bird, then do this.
And we can say that the bird eats it eats seeds. And then we can yet again have another alternative. We could say, Elif, if the animal is a monkey, make sure to surround that with quotes is a monkey. Then we can say that it eats bananas, right? Eats bananas. And then finally to where’s the end we can say else. And this is the final condition, else we can say that we don’t know what the animal eats. Meaning the animal that the user may have assigned to this animal variable does not have any conditions. And so in that case, the end all be all print statement will be run. All right? So let’s run this. And of course it’s going to say eats grass. The cow eats grass. If I say monkey, it’s going to say that it eats bananas. Whoops. I think I misspelled Monkey.
I actually typed money. That’s not good. So let’s change that to monkey. And so now if we run this notice it says eats bananas, right? And then you saw that if I was to give something here that does not exist in any of these conditions, it’s going to print this, right? So let’s say if we chose alligator and we run, we don’t have a section for alligator. So of course it’s going to say we don’t know what the animal eats. Now, the text editor is smart enough to know that we made a spelling mistake here. That’s why it’s underlining that alligator has two LS. But that’s besides the point. Now, just like in the if statement, we can have multiple conditions. We can have multiple conditions in each of these L If blocks as well. So we can say if the animal is a monkey or the animal is an ape.
If the animal is an ape or a monkey, it eats bananas. So if we were to choose ape now, then this condition will also be met eats bananas. If we type either monkey or ape, it’s going to eat bananas. Now, we can’t use and here, right, we talked about this in the section of comparison operators, because an animal cannot both be a monkey as well as an ape. An animal is an animal, either it’s a monkey or an ape. So in this situation, if I leave this ann clause here in Rhondas, it’s going to say we don’t know what the animal eats because it doesn’t make sense for an animal to be an ape as well as a monkey. It’s not like you could do this. It’s not like in the section where you’re assigning, you can say monkey. Even if you do this, let’s run it, it still won’t know what animal eats. And this is essentially you’re defining a tuple here. If you don’t put the parentheses around it, it by default treats it as a tuple. And you’ve seen tuples before. If we were to print animal before any of these clauses execute, let’s run this notice. It actually ends up being a tuple. So that’s why this wouldn’t really make sense.
You can’t have an animal be an ape as well as a monkey to fulfill any of these conditions. That’s why we need an or clause here, okay? So we have an Ore clause, and we change this to either ape or monkey. And now we’ll see that it runs, eats bananas. And just like before, we can have less than, greater than, less than, or equal to, greater than, or equal to operators being used in these conditions, all right? And you can have multiple conditions. And you can also wrap these in parentheses like I showed you in the previous lecture, if you choose to in this situation, we have very simple conditions, so we don’t really need to wrap it with parentheses, okay?