So there are two kinds of loops you can use in Python.
The for loop, which you just discovered here in the previous lesson,
and the while loop, which we are going to see now.
And you will see that the knowledge you got when working
on conditional statements will be very useful here.
To explain how while loops work,
I'm first going to do the exact same thing as for the loop here.
So we are going to print hello with a counter.
But this time using while, just going to put back to 10,
because that's going to be a simpler to read.
So just follow along and I'm going to explain that.
I'm going to create first a variable i is equal to 0.
And then I'm going to do while i is lower than 10, strictly lower than 10.
I put a colon and then I do print hello.
And then plus str i.
Okay, so let's bring the same thing and I do i plus equal 1.
Let's actually comment those two lines.
Okay, so you can see I just comment like this.
So those will not be executed.
I'm running the programs and you can see we have hello zero until hello nine.
So this is the exact same behavior with this
and with this we have the same behavior.
So now how does it work?
Well, the while loop is different here because
with the while you are going to test here, this is a conditional statement.
Okay, so the while loop is basically the same as if structure.
The difference is that while this condition is true,
this block of code will continue to be executed.
Okay, so you check if that is true.
If it's true, yes, then you execute this.
You come back to line five.
You check again if this is true.
If yes, you continue to execute this, etc.
etc.
And then when we come back to the line five and if this condition is false,
in that case, we don't enter the block of code
and re-resume the program here at line eight.
So what did I do here?
I created first a variable i that we're going to use as a counter.
Now I'm going to check while i is strictly lower than 10, we continue to execute this.
So at first, i is equal to zero.
So i is... So zero is lower than 10, yes, we print hello plus zero.
And then what I'm going to do, I need to do
that, I'm going to add one to the counter, okay?
So I'm going to add one every time.
To add one, so you can actually do i is equal to i plus one, okay?
But there is a shortcut, you can do plus equal.
And this is simply going to add the element on the right to the variable on the left.
So now i is equal to one, I come back to line five.
One is strictly lower than 10, yes, we print hello one.
And then I put plus one, so i is not equal to two, etc. etc.
At one point, i will be equal to nine.
So nine is lower, strictly lower than 10, yes.
We print hello nine, I do nine plus one.
So we get 10, no, i is equal to 10.
We come back to line five and then is 10 strictly lower than 10?
No, this is false.
So we stop the while loop.
Okay, and the condition here for the while can be anything you want, okay?
Here, this is a very classic example with a counter.
But as an example, let's say you read the temperature of your computer, okay?
And you want to continue doing an action while
the temperature is lower than the threshold, okay?
You continue, you continue, and then you continue to read also the temperature.
And when the temperature starts to get above the threshold
that you have set, then you stop executing the while loop.
All right, and now the one million dollar question.
When to use a for loop versus when to use a while loop?
So basically, use a for loop when you know exactly
how many times you want to execute an action.
Use a while loop when you don't know how many times,
or for example, if the condition to continue is external to the loop.
As an example, let's say you want to ask a user to give you some numbers.
In the first scenario, you want exactly 10 numbers.
So you are going to use a for loop and ask 10 times.
In the second scenario, you allow the user to give as many numbers as they want,
and you stop asking whenever the user gives you the number zero.
Here, you are going to use a while loop.
And don't worry if you still have some confusion about this.
In many cases, both options are actually possible,
just with a different way of writing the code.
With a bit of experience, the choice will become much easier for you.
And lastly, if we come back to the example here, the exact example I've used here,
you can see that, well, here the for loop was actually more appropriate,
because we knew exactly how many times we wanted to print the text.