 Hello everybody. Today we're going to be taking a look at while loops in Python. The while loop in Python is used to iterate over a block of code as long as the test condition is true. Now the difference between a for loop and a while loop is that a for loop is going to iterate over the entire sequence regardless of a condition, but the while loop is only going to iterate over that sequence as long as a specific condition is met. Once that condition is not met, the code is going to stop and it's not going to iterate through the rest of the sequence. So if we take a look at this flow chart right here, we're going to enter this while loop and we have a test condition right here. The first time that this test condition comes back false, it's going to exit the while loop. So let's start actually writing out the code and see how this while loop works. So let's create a variable. We're just going to say number is equal to one and then we'll say while and now we need to write our condition that needs to be met in order for our block of code beneath this to run. So we're going to say while number is less than five and then we'll do colon enter and now this is our block of code. We're going to say print and then we'll say number. Now what we need to do is basically create a counter. We're going to say number equals number plus one. If you've never done something like this, it's kind of like a counter. Most people start it at zero. In fact, let's start at zero and then each time it runs through this while loop, it's going to add one to this number up here and then it's going to become a one, a two, a three each time it iterates through this while loop. Now once this number is no longer less than five, it'll break out of the while loop and it will no longer run. So let's run this really quick by hitting shift enter. So it starts at zero and it's going to say while the number is less than five print number. So the first time that it runs through it is zero and so it prints zero and then it adds one to number and then it continues that while loop right here and it keeps looping through this portion. It never goes back up here to this line of code. This is just our variable that we start with and then once this condition is no longer met, once it is false, then it's going to break out of that code. Now that we basically know how a while loop works, let's look at something called a break statement. So let's copy this right down here and what we're going to say is if number is equal to three, we're going to break. Now with the break statement, we can basically stop the loop even if the while condition is true. So while this number is less than five, it's going to continue to loop through. But now we have this break statement. So it's going to say if the number equals three, we're going to break out of this while loop. But if this is false, we're going to continue adding to that number just like normal. So let's execute this. So as you can see, it only went to three instead of four like before, because each time it was running through this while loop, it was checking if the number was equal to three. And once it got to three, this became true. And then we broke out of this while loop. The next thing that I want to look at and we'll copy this right down here is an else statement, much like an if statement, but we can use the else statement with a while loop, which runs the block of code and when that condition is no longer true, then it activates the else statement. So we'll go right down here. And we'll say else, and we'll do a colon and enter. And then we'll say print. And we'll say no longer less than five. Now, because this if statement is still in there, it will break. So let's say six. And then we'll run this. And so it's going to iterate through this block of code. And once this statement is no longer true, once we break out of it, we're going to go to our else statement. Now, as long as this statement is true, it's going to continue to iterate through. But once this condition is not met, then it will go to our else statement and we'll run that line of code. Now the else statement is only going to trigger if the while loop no longer is true. If we have something like this if statement that causes it to break out of the while loop, the else statement will no longer work. So let's say if the number is three, and we run this, the else statement is no longer going to trigger. So this body of code will not be run. Now the next thing that I want to look at is the continue statement. If the continue statement is triggered, it basically rejects all remaining statements in the current iteration of the loop. And then we'll go to the next iteration. Now to demonstrate this, I'm going to change this break into a continue. So before when we had the break, if the number was equal to three, it would stop all the code completely. But when we change this to continue, which we'll do right now, what it's going to do is it's no longer going to run through any of the subsequent code in this block of code. It's just going to go straight up to the beginning and restart our while loop. So what's going to happen when we run this is it's going to come to three, it's going to become three, and it's going to continue back into the while loop, but it's never going to have that number changed to be added to one to continue with the while loop. This will basically create an infinite loop. Let's try this really quickly. And as you can see, it's going to stay three forever. Eventually, this would time out, but I'm just going to stop the code really quick. So if we just change up the order of which we're doing things, we're going to say there, and we're going to put this down here. So what it's going to do now, instead of printing the number immediately, and then adding the number later, we're going to add the number right away. And then we're going to say if it is three, we're going to continue and it's going to print the number. So let's try executing this and see what happens. So as you can see, we no longer have the three in our output. What it did was when we got to the number three, it continued and didn't execute this right here, which prints off that number. So that really is the basics of the while loop. I hope that this was helpful. I hope that you learned something in this video. If you did, be sure to like and subscribe below and I'll see you in the next video.