Instructor: Welcome back.
Up until now, we've learned
that with looping we can use for.
But there's actually another way for us to loop
and it's called instead of a for loop, a while loop.
And while loop is a little different
in that we say while a condition is happening,
do something.
So you can see that there's a bit of a difference here.
For example, I can say that
while zero is less than 50,
do something.
Or I can say, for example, I equals two zero
and I'm going to say, while I is less than 50,
print I.
Now, if I ran this program, what do you think will happen?
Let's find out.
If I click run,
right.
Do you see that?
I don't know if you can tell
but I'm scrolling right now through all of this.
It's looping, looping, looping, looping, looping.
And you see that the programming's still running.
As a matter of fact, I have to stop it because
what it was doing is it was looping and looping and looping
because if we look at the code,
well, zero is always going to be less than 50.
So what happened was, while this was true
we printed I and then the Python interpreter went back
to line two and says, Hey, is zero still less than 50?
Yep, okay, next line.
Print.
Hey, is zero still less than 50?
Yep, okay, next line.
Print.
And it kept going, kept going, kept going.
Now this is what we call an infinite loop.
That is, the program doesn't know when to stop.
I mean we're telling it to just keep going forever,
for eternity.
And I have some safeguards in place here
but if you did this in code
you'd get this infinite loop until your program crashes.
If you actually ran this code, well
it's going to keep running
until the machine just doesn't have enough power anymore.
Enough resources to keep doing this over and over.
Everything has limits, right?
So with a while loop, we have to be really careful.
This infinite loop can be very dangerous.
So how can we solve this?
We have this idea of a break statement
and break is, well, just another key word in Python.
And as it as you may have guessed
if I click run, look at that.
It only runs once.
As soon as Python sees break, it's going to say, All right
break out of the while loop.
So it's gonna go to line five
and it's going to say, Mm, got nothing there.
Program's done.
I'm going to exit out of the program.
Hmm, but okay, what if I want this to be looped 50 times?
How can we solve that issue?
Think about it.
Well, we can just simply say, I
equals
I plus one.
And then make sure that we changed this to I.
Now what's going to happen if I click run, we stop.
Well, at 49 we've done it 50 times.
We've looped through it 50 times
because we kept incrementing I over and over so that I
each time we came back to the loop was incremented by one.
Now a shorthand for this, we've seen it before, is to do
plus equals one so that if I run, look at that, same thing.
So we're also able to break out of the while loop as long
as we're able to turn a condition into false.
So you'll see this a lot as well.
So to jump out of a while loop,
you can either turn the condition false
or you can break out of the while loop.
Now another interesting thing that we can do
with the while loop is we can have else blocks.
And here we can say,
print done with all the work.
And if I click run here at the very end, look at that,
done with all the work.
And this should read like English, right?
While I is less than 50, keep looping through here.
And as soon as we get to the last part where I becomes 50
where 50 is no longer, less than 50
Python interpreter is going to say, All right
I don't care about anything under there.
Let's go to the next line.
And I get otherwise.
So in this case, we don't have anything else happening.
We can print, done with all the work.
But you're probably asking yourself, Hold on
can't I just
not do this?
And just make sure my indentation is
at the end click run and that that works, right?
Why would I ever wanna write else here?
Well, else is a special case here.
There is one thing that is useful.
That is if we added a, for example
a break statement here and I click run
you'll see that the else statement does not print.
And that is because the else block will only execute
if there isn't a break.
That is, if the while condition turns to false
then it's going to keep going and do the else.
But if there is a break,
it's going to break out of this whole thing
and go to the eighth line.
So a little of gotcha here
that you won't see else too often next to while
but there is that special case
where else is used when you wanna make sure
that this will be executed only
if there isn't a break statement,
only if the loop is finished doing whatever it's doing
without the break statement.
Let's take a break and explore
while loops a little bit more.