Welcome back.
Up until now, we've learned that with looping we can use four.
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 to zero.
And I'm going to say while I is less than 50%, I.
Now, if I ran this program, what do you think will happen?
Let's find out.
If I click Run.
All 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 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 print it.
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?
Yeah.
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 keyword 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 wild loop.
So it's going to go to line five and it's going to say, got nothing there, programs done.
I'm going to exit out of the program.
Hmm.
But 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 change 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 lived through it 50 times because we kept incrementing eye 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 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 wild loop, you can either turn the condition false or you can break out of the
loop.
Now, another interesting thing that we can do with a 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 want to write?
Else here.
What 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 Elf's statement does not print.
And that is because the else block will only execute if there isn't a break.
That is, if the wall 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 gotcha here that you won't see else too often next to.
Well, but there is that special case where else is used when you want to 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.