Welcome back, everyone, in this lecture, we're going to discuss while loops.
Now, while loops are loops that are going to continue executing a block of code, while some condition
remains true, unlike for loops that end up iterating through a sequence in order to execute blocks
of code while loops keep going.
So for example, while my pool is not full, keep filling my pool of water and then stop the loop when
the pool is full.
And we can do it with just a simple line of code saying while, for example, while my dogs are still
hungry, keep feeding my dogs.
So this means in the syntax of a wire loop, we're actually checking while some bullying condition is
doing something, execute a block of code, you can almost think of this more like an if statement that
just keeps going over and over and over again for that condition until that condition is false.
In fact, let's go ahead and jump to our code editor and check this out.
OK, so here I am back at the coding editor.
But I'm going to first start off with is assigning some variable end to a number like one.
And then what I'm going to do is set up my while.
So I say while and then I have to figure out some sort of condition.
Now you always want to be careful for a while, loop not to do something like while true colon.
Prints some letters here, because this is going to basically execute forever.
It's going to be while this is true, always run this block of code.
So you really want to be careful of something like this because that essentially says, run this forever,
never break out of this wild loop.
So instead, we want conditions that are probably going to be affected either by some outside source
that we're checking or internally within the block of code inside the wire loop.
So going say something like while M is less than five, I'm going to print out the current value events
I'm going to print out and is currently.
And then let's use a string literal printing here.
So I'm going to say, while an is less than five print F and is currently RN.
And then if I just run this by itself, there's no change to an ever.
So this is going to print out forever.
Hey, and it's currently one and is currently one and is currently one.
I want to avoid that.
Otherwise I'd have to somehow figure out how to kill the Python kernel.
Instead, I want to make sure I can update the current value of.
So then I'm going to say NW is equal to and.
Plus one.
And so now let's think about what's happening as this keeps looping.
So it's going to start off with an equal to one.
It's going to report back and is equal to currently one.
And then it's going to reassign men to be equal to the current value of N plus one, which is then going
to be one plus one.
So it's going to be two.
There's going to run this again and it's going to be three, then it's to be four.
And then suddenly, once it hits five five, it's no longer less than five and the while Loop will stop
executing.
So you should see the results look something like this and is currently one two three four and then
it stops executing.
So while loops can be really useful when you have the ability to check something that's happening outside
the wire loop, we could do something like Prince Hey, Server is up.
And then in the while loop, I could have some other function.
Let's say somewhere else in some other piece of code that could be something like check server.
And then I would have here something like, let's say, server condition or a server up.
And then I would have this be something like, OK, have the check server report back whether or not
my server is up true or false?
And then I can print out, OK, the servers up continually until it's not up.
So that's the kind of thing I could do if a while loop again, we haven't really learned about functions
yet.
We're about to learn that next.
I just want you to give you some ideas of how this could be useful.
It's when you want something to continually go over and over and over again in a loop until some condition
is no longer true.
Again, it's while some condition is true.
Keep doing something.
All right.
So that's it for the wire loop.
Coming up next, we're going to discuss functions.