 Welcome back everyone. My name is Brian. Let's continue our journey into Python 3. We're going to talk about the while loop. This is basic flow control and it's going to prove a point. Your computer is very stupid. And yes, I said what I said your computer is just very dumb and we're going to prove that. So a loop is just simply completing code over and over and over again until you tell it to stop because the computer isn't smart enough to know when to quit. Let's take a look. Okay, a little bit of theory here before we move on. We have the entrance of our program and we're going to jump down into some sort of loop. Let's call this our loop here. And it's going to evaluate this and run some type of code. Now it doesn't really matter what type of code it runs. Let's get rid of that little guy. Usually what you're doing is some sort of incremental function. For example, count something, increase the number, look for data on a socket or something like that. So it's going to say, okay, start the loop. I don't want to connect there. There we go. And do something. If you don't exit this loop, it just literally goes right back into the loop and your program ends up looking something like this. We call this an infinite loop where you will enter the program, go into a loop and then you just loop this code over and over and over again, until you tell it to, well, stop and do something else. You may be asking yourself, why do you even need a loop? Well, think about it. You enter your program, you come down into your loop and let's say you want to do something 100 times. You need to now increment some sort of counter. Once you've hit that counter, you jump out of this loop and you stop your code. There are some loops out there that are intentionally infinite. And what I mean is they look like this. The program starts you enter your loop and then it just loops forever. For example, when you in and do graphic user interface programming, you have what's called the user interface loop or the event loop, which is basically the program will run forever waiting for the user to like click a mouse or move something around. That loop just happens forever and ever until the program closes. All right, now that we understand what a loop is, let's take a look at it in practice. So I'm going to say x equals zero. And we're going to create our while loop. And this is going to look very similar to an if statement because it's going to evaluate it just like an if statement would, meaning this loop will execute as long as the condition we're about to give it is true. So let's say x is less than 10, then do something. And this is going to be a great example of how computers are dumb. They do what you tell them to as you tell them to do it. A little bit of bonus. Anybody see a problem with this statement? Well, what we're saying is x is zero while x is less than 10. But we're never incrementing x, it's always going to be zero. So this will always be true and it will just run forever. That's the infinite loop I was telling you about. See how it just says zero. And it's going to run forever and ever and that console is just going crazy. Somebody kill it. It will just keep going forever and ever until I turn the program off or until I shut the computer down. So we need to actually increment x. Once we do that, we have now incremented our counter. Eventually x will be 10 or greater, and this will exit out. See, one through 10 works as expected now. So loops can be very powerful and very easy to use, but you can very much screw up your application just by creating an infinite loop on accident. Now, in case you're wondering, let's go ahead and print this out here. And we're going to say that one is done. We want to know when this loop is done executing and we use, you guessed it, the indentations. This determines our scope or our block of code. So while this condition is true, execute this scope or this block of code. Then when we're done looping, we're going to jump right back up here. Let's see that action. I'm going to clear that out. Sure enough, one through 10 and then test one is done. So it works exactly the way you think it would. Let's take a quick detour and talk about pass. You're going to see this quite a bit in Python land and a lot of people don't really explain it. So let's just say while, and let's just take this little template here. And it's going to say while expression pass, but this is a valid Python expression. Let's go ahead and say zero and let's just give it the same logic here. Notice how this will run. Now what's going on in the hood is this is still functioning. Or is it? Let's find out here. I'm going to kill this terminal just in case. We'll say test two is done. So X zero while X is less than notice how we never increment it like we did in this guy, we're going to pass and then eventually print out test two is done. Save run. Notice how it says test one is done from up here, but it never gets down to test two. So what's going on under the hood? This script is still running. It's just doing an infinite loop right here. So whenever you see pass inside of a loop, be very careful because you're creating some sort of infinite loop. Pass is a special keyword in Python that tells Python take no action and just continue with what you would normally do. So be very careful with pass. We just covered that pass is well pretty dangerous in a loop simply because you can loop forever on accident. Well, it's not that simple under the hood, but we're going to try and make it simple and we're going to talk about continuing and breaking. So we're going to say X equals zero. And let's go ahead and do something really, really terrible. We're going to say while true do something. If you're paying attention, you know that while is going to evaluate this much in the way an if statement would and it's going to say if this is true. Do the loop and keep doing it over and over until this is no longer true. However, we've used the Python true keyword. So this will now loop forever. And you will actually see this out in code because sometimes people just want to loop until some arbitrary condition is met. It could be input from the keyboard, it could be some network connection, it could be anything, but you're going to see this a lot. So you'll need to know when to break out of the loop or how to handle certain conditions. So let's talk about continue first. We're going to say X plus equals one. And if X is less than five, we want to well just kind of skip over this. And we don't really want to do anything. So I'm just going to say X is less than five, whatever the number is, and then we're going to continue. And what continue does is say go back to the beginning, just continue along your merry way and do what you were going to normally do. And then we're going to add in some sort of logic. So if X was greater than 10, we would do something with that X out. And we're going to put a print statement at the very, very end of this. And notice this is on the same line as while. So this will not print out until this loop is complete. One thing I love about VS code in most editors, you can actually collapse. So it actually looks like this. So once this loop is done, it'll print complete. Let's go ahead and clear this out. See this in action here. Uh oh, you notice how it's working exactly as we expect it would because the computer while very fast is very dumb. So what's going on here is it's going and saying if X is less than five, print it out and then just go back to the beginning. So we've still in continual loop land, we need to fix that. So our programming logic, we want to basically say if it's less than five, just go back to the beginning of loop and keep incrementing that counter. If it's greater than five, do something, but we also want to stop when we hit 10. So I'm going to say if X is 10, print and then X equals just so we can see that X is actually at that value. Now we're going to use the break keyword. Now don't worry, we're not breaking anything. Break is like kind of think of the while loop. This guy has a little jail cell and we're going to break out of jail and just jump right to the next line. So it's going to pick up code execution right here and then it's going to run this statement. Go ahead and see this in action. Okay, sure enough, we've got X is less than five. We've got one, two, three, four, and then do something six, seven, eight, nine, 10. And then when we hit 10, we're going to exit and we are complete. So that is looping in a nutshell. Some major takeaways from this video is well, loops are very, very powerful, but you need to have some sort of condition to evaluate. Otherwise you're going to create an infinite loop. An easy way to create an infinite loop is just simply pass and it will loop forever. And you can say while true, which will also create an infinite loop, but you can use continue to well continue to use the flow inside of the loop or break to break out of that flow and use the programming logic. I hope you enjoyed this video. You can find the source code out on github.com. If you need additional help myself and thousands of other developers are hanging out in the void realms Facebook group. This is a large group with lots of developers and we talk about everything technology related not just the technology that you just watched. And if you want official training, I do develop courses out on you to me.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there. Drop me a note. I'm either working on it or I will actually develop it. I will put a link down below for all three of those. And as always, help me help you smash that like and subscribe button. The more popular these videos become, the more I'll create and publish out on YouTube. Thank you for watching.