While loops are, well they're still a loop, so you can loop through
things. But the difference between a while loop and a for
loop, is that a while loop is going to run forever until
you tell it to stop.
A for loop will loop through all the items in a list, and
when it gets the end, it just stops automatically.
That's really nice, but a while loop will run until you tell
it to stop.
Now you have to be careful with these because while loops
will run forever, and depending on your settings on your
computer, that could end up eating a lot of memory on your
computer and end up crashing it.
So just be careful with that one.
So let's go over the syntax of this.
Generally, it's just while something is True, do something,
else. That's all this is going to do, and you can see that
these are all highlighted words because these are all keywords.
So it's a while loop,
while anything is True, run some code.
So let's do an example here where the 'num =
100'. We could say 'while > 0:
print(num)', and then also because this is going to
run forever, because right now 'num = 100', it will
always be greater than 0.
So instead we have to set this manually.
So we say 'num = num - 1',
or actually we could do '-5' if we wanted to.
We don't have to increment or decrement by 1.
So this is going to print 100, and then it's going to say
100 = 100 - 5,
so it's 95, then print 95, then 90, then goes over, 85, 80, all
the way down until that number continues to be True, till it
is greater than 0.
So let's go ahead and run this, and we see it starts at 100,
goes all the way down to 5.
The reason it doesn't show 0 is because we didn't say
'while num >= 0',
we said as long as it's greater than 0. If we wanted
0 to be in there, we could just change it to a '>=' sign,
and that's not going to work because I have to reassign
number up here. There we go.
And now we see 0 at the bottom.
Now, if you ever get stuck in a while loop, generally you
can just hit 'Control + C' on your computer, especially if you're
just running something in the terminal, this is,
yeah, this is my Jupyter Notebook running.
But if you're running your code in your terminal, and you
just see your code is just running the same thing over and over and
over again, forever and ever, just hit 'Control + C',
and it should usually quit
it. Might take a couple seconds for it to kick in, but usually
that's all you have to do.
Typically, people like to stick with for loops just because
a for loop will automatically stop, and we're usually looping
through things that are predefined anyways.
But there are cases where you want to use a while loop.
For example, if you wanted to randomly create 100 users on
your website, you would do a number of users is equal to
0, and then while number of users is less than or equal
to 100, create some users in here, and then do a number of
users is equal to number of users plus one, and that will
just create 100 users for you.
So you see, there are times when you want to use a while
loop. Generally speaking, though, at least in my experience,
you see for loops a lot more frequently than you do while loops.