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, answer's going to
do the exact same thing. As for the looping, so
we're going to print Hello with a counter. But
this time using while, okay, just going to put
back 10, okay, because that's going to be simpler
to read. So just follow along, and I'm going to
explain that, okay. I'm going to do create first
variable, i is equal to zero, okay. And then I'm
going to do while i is lower than 10 strictly to
one and 10. Okay, I put a colon, and then I do
print, Hello, and then plus str(i). Okay, so let's
print the same thing. And I do i plus equal one.
Let's actually comment, those two lines. Ok. So as
you can see just comment, like this, so those
will not be executed. I'm running the program,
and you can see we have Hello 0 until Hello
9. 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 we have 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, it is 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 rerun
the program here at line eight. So what did I do
here, I created first 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 is 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 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 then 10, yes, we bring 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 stopped 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. Alright, and now that 1
million dollar question, when do you use for 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 are 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 numbers 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.