3.4 Looping With While
for loop will take you a long way in the world of software development, but there comes a time when you don't necessarily want to loop over a fixed range. You may want to continue to execute some block of statement until a certain condition is met. That's where the
while loop comes in!
1.Introduction2 lessons, 11:32
2.Python Building Blocks6 lessons, 1:08:07
3.Controlling the Flow7 lessons, 1:20:10
4.Common Data Structures4 lessons, 46:49
5.Application Structure7 lessons, 1:15:12
6.Collections7 lessons, 46:55
7.File I/O6 lessons, 48:51
8.Networking5 lessons, 43:48
9.Connecting to Network Services3 lessons, 34:27
10.Conclusion1 lesson, 02:08
3.4 Looping With While
In this lesson we're going to introduce another type of looping statement, as well as some other different interesting constructs that you can use within the world of loops in Python, and as well as in other different languages too. So the new loop we're going to start talking about in this lesson is the while loop. So if you recall in the for loop, we were iterating over a particular list of data, and it could be a list of names or a list of whatever you were working with, and you could start to go through the entire list or slice it in a certain way that you wanted to or you could refer to all of the values within that list by their indexes using the range function. So we saw how to do all those types of things. Now the next thing we want to start to talk about is the while loop. And a while loop is similar to a for loop in that it's going to do some sort of logic over and over and over again, but the difference is that now instead of iterating over something, we can start to loop through something and do a bunch of different logic and continue doing that logic until something happens that makes us want to end that loop. So let's see what this is going to look like. Let's start with something very simple. Let's just say that we have an integer value. And we'll call this number, and we'll set it equal to 0. So now we want to do some sort of logic and we want to continuously do that logic until something happens. So in this case we're just very simply say we want to continue doing something while number is less than 10. Right, so that's going to be our conditional statement, so we're going to say while. And we want to continue executing everything within this block while number is less than 10. So once again we're going to put in a colon. And we're going to drop down to the next line and indent, cuz remember this is a loop, this is a block of code that's going to execute through every iteration of this while loop. And now we are going to just say all, right I want to print number. Now, at the very beginning when you start doing this if you've never used while loops before you may just be tempted to do this or write in your logic or whatever it has do some math to whatever it is you wanna do. And then just go ahead and save this and run. So let's go ahead and call this while, like that. And now we'll go ahead and execute this. But I'm going to warn you, something bad is bound to happen. So we're going to say while, and we'll just go. And it looks like something is happening, but I just see a bunch of zeros. And it keeps flickering, which means this continues to run down so I'm gonna cancel out of this with Ctrl+C to do a keyboard interrupt. And something strange is happening here. So what exactly is happening? Well, we are going through this while, and we want to do this print while number is less than 10, but this is the age old issue of an infinite loop. So what does an infinite loop? Well that means. We have entered into this loop and we never get out. It's going to infinitely infinitely run here, and why is that? Well, because within a while loop, we have some sort of conditional check here. We want it to run while this is true, but the problem here is that nowhere in my while loop am I doing anything to ever make this not be true. So that means I am not incrementing number here, I'm just continuously running through this loop with number equal to zero and the value of number never changes. So let's make that little bit of a change. So we'll number. Plus equal to 1. So we're going to increment that by 1. So let's save that now and we'll go ahead and clear this whole, run our application again, and now you see okay. We started off with the number equal to 0. And then we wanted to print out the number which we did. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. And then after it printed we incremented it by one using the plus equal syntax which is the same as saying, number is equal to number plus one, so we just kind of shortened that to say plus equals one, so plus equals one. So we'll save that. So there you go. Now we've been able to get out of this loop, but we can do anything in here. We could do mathematics. We could print something out. We could talk to a database, talk to a web service, do whatever it is we want to do, and if this is going to continuously function, and every single time it's done iterating through this block of code, it will check to see if this condition is still true. And if it is, it'll keep going, and then ultimately it'll jump out to the very bottom and when we're done here, we can just say print while is done. And save. So if I were to run this again, you'll see we get all the way down to the point where number less than 10 is not true where number is now equal to 10. So it's going to jump out and execute the next line of code, which is print. Now, that is not the only way to necessarily get out of a loop, so you can run into these problems with infinite loops and all that sort of good stuff, in for and while loops. So it's always something you kind of have to be cognizant of in the back of your mind. But there's also another way to get out of loops. And there's actually a very interesting way that you can start to conditionally jump out of a loop, even if this while is not true. So let's say. We wanted to run this loop, and we also wanted to say, all right, while number is equal to ten, sure, but if some sort of condition happens, if we run into something inside the logic of our loop, we wanted to be able to jump out of it prematurely before maybe this exits out. So the way that we could do that is we can start to come in here and we can say all right well let's just say if number is equal to 5. At that point, maybe at that point we want to be able to jump out of this while loop, even though we haven't hit the end of the defined, conditional statement here at the top of the while loop. How can I do that? Well. That is where we get into a magical keyword called break. So break is going to jump you out of the closest loop once it hits that statement. So what do I mean by the closest loop? Well, let's say in this case I only have a single loop, this while loop. It's going to jump you out of the loop that is the closest. Now if I had another loop up above this and had this while nested in there, so if I had a while up above here or another for or something like that, this break statement is only going to jump you out of the closest while loop. So let's go ahead and save this and come down here and execute this and as you can see we made it up to 4. And in this case, after we printed 4, we added 1 to number, made it 5. So, the now this if statement fires because number is equal to 5 and now we break out of this loop. Okay, so that's one thing that we can do. So let's say at this point instead of printing at this point we'll go ahead and rearrange our logic a little bit. So let's say the first thing we want to do is we want to check to see if the number is equal to five. And if the number is equal to 5, we want to jump out of the loop. So we won't print it at that point. So let's see how this changes our logic. So let's go ahead and save this. We'll run our loop. And as you can see, nothing really seems to change. At least from the way we're looking at this code, but now let's say instead of jumping out of here I just wanted to skip the rest of the execution of this loop once the number is equal to 5. I still want to run these checks, this conditional statement for every iteration through this loop. But if the number equals 5 or some other condition happens I don't want to print and I don't want to do this. So what is that going to do? Well how do we handle something like that? Well, we have another magical keyword that is called continue. Now, continue is going to do a similar operation of break, but in this case, it's going to say not break out of the closest loop. It's going to say I want you to continue running to go up here to execute this loop again. So if I were to save that, and this time I will clear the screen out. And I'm going to run this and you're going to see once again that something is happening here. We're not hitting the end of this loop, and that's because we have kind of behind the scenes, and you can see my blinking cursor here. We're currently running within an infinite loop again because we hit 5. And now we're continuing, we're coming back up here but we're not incrementing here. So in order to handle that scenario, we're going to need to copy this, drop down with in this if. And I want to, within here, I want to increment. Now by making this one little change, by adding this incrementing of the number within here as well, when I continue I will have already incremented the number to be at this point, 6, it will jump back up here and continue to run. But, I would expect that we're gonna print out all of these numbers except for 5. So let's go ahead and see if that works. So we'll go ahead and clear this. Run our application again. And now we've started at 0. We have 0, 1, 2, 3, 4. And then we hit this if statement. To say if numbers equal to 5, and in this point it is, I increment number, and then I continue, which says, all right, I want to continue running this loop and skip whatever comes after it. And in that case, I did not print 5. And now numbers equal to 6. So this if block does not fire. And we come down and run our print statement, increment number and we finish off our loop here. Now finally the last thing that I would like to touch on very briefly when it comes to loops, especially with the for and while loops, is how do we find out when we've actually hit the end of the logic within that loop? Well, we can do something like this, we can do a print but we have an option as well that may be a little bit different than you're used to seeing when you do looping like fors and whiles, in other languages. In Python, the conditional statements or the looping statements also have the capability to run an else block. So what does this mean? So the else happens the first time, and only the first time, that whatever conditional piece of logic fails and before it jumps out into the rest of the logic in your application. So let's say down here in our little else at the end of our while loop, I put in a simple little print line here, and I say condition is false. So we come down here and save that. So let's see what this does. If I clear out and go ahead rerun my application, we see that we went through and did all of the logic within our application. And then we else fired after this condition failed. One time, condition is false, and then we stay here, the while is done. Okay, so that's fine. Now what about in other scenarios where instead of saying I want to continue in this case until while is false, maybe I wanted to break again at this point. So remember, break is going to jump us out of the closest loop, in this case this while loop here. What's going to happen? So let's go ahead and rerun this. Well as you can see here we got down to the point where we printed 0, 1, 2, 3, 4. We hit where number's equal to 5, we do our incrementing and then we break, which means that we immediately jump out of the execution of this while loop. Well and as you can see here we went straight down to while is done, and we skipped our else. So that's gonna show you that in this case, when we break out of the loop, we actually break out of everything associated with that loop, meaning this while or for if that were the case, and the else associated with it. So a break will jump us out of there entirely. And the else will not happen because this conditional statement here never failed. So that's just something to kind of keep in mind should you ever want to use that sort of functionality. And honestly typically I don't use it very much, but it's definitely something you can use in your tool belt. So now we've gone through the for loop, the while loop, we've understood how to continue processing our loop with the next numbers using the continue keyword as well as breaking out of the functionality of our loops with the break keyword. And then of course we've also talked about our conditional, our if, our else if, and our else statement, so now we've got a lot of very powerful constructs within the Python language that we can use to start formatting and creating these applications. Now before we jump into another little programming exercise, I want to introduce you to another topic that's really going to allow you to create some control and flow within your application, and that has to do with functions.