Narrator: So when should you use a while loop
and when should you use a for loop?
And it really depends on the problem you're trying to solve.
For example, in a for loop
I could do for item in,
let's say a list, 1, 2, 3.
And just looking at this code, you know right away
that it's going to be looped over three times, right?
And I could say here, print item.
But you can do this in a while loop as well.
I can say i equals to 0.
And then while i is less
than, let's say the length of the list,
that is 1, 2, 3.
Well, in that case I want you to print i
or the item and then increment i by 1.
If I run this, I see that, well technically here
we're incrementing the i, which is the value,
but let's say we had a list up here.
So my_list, and this list will be 1, 2, 3.
Well, my_list can be iterated with a for loop
or it can be iterated with a while loop.
And we simply say, my_list,
at index of i.
And if I run this, I get the same results.
But, which one do you think is better?
First off, while loops are very flexible.
We can do a lot because we have this conditional statement,
we can loop more than three times if we really wanted to.
So in that case, while loops are more powerful
but for loops are simpler, right?
Like this code reads really nicely and really well,
it makes sense.
We just wanna loop over something that we already
know how many times we wanna loop
over, three times.
With a while loop. We have to create this variable
and we have to make sure we remember
to increment the variable
so we don't get an infinite loop.
So with a while loop, you need to make sure we remember to
halt the loop at some point.
So my rule is usually this.
For simple loops or iterating over iterable objects
for loops are great.
But let's say you are not sure how many times you wanna loop
over something, you're not really sure how long
it's going to take for looping.
So you wanna say while something is true, just keep looping.
For example, let's say we're trying to go through an email
list that we've collected on a website.
And for each email list we wanna send an email,
Well, while the list is still there,
let's just keep sending emails.
There are many, many cases,
but one of the most useful ways
to use the while loop is like this is.
Is to say, while true, do something.
And make sure that at the end we break.
Hold on what's happening here?
I'm saying while true
wouldn't that create an infinite loop?
I mean, true is always going to be true.
And you'd be right if you notice that.
But again, we have a break statement here,
so at the end, after we run line three, it's going to break.
But now we can do something powerful like input here.
And if you remember an input is going to ask us for a prompt
to enter something.
So I can say, hey, input, say something.
And if I click run, it's going to say, hey, say something.
You know what? Let's add semicolon here
or a colon here and let's try that again.
And look at that. It's saying say something.
I'm on line three right now, so I'm going to say hi.
And did you see that? We just broke out of the loop?
It only asked me once, even though this was true
as soon as we got to line four, I was done.
But what if I removed this and I click run.
If I say something, I say hi.
And then it asks me again, Hi, Hi. I'm telling hi.
And it keeps asking me, asking me, asking me,
and we have an infinite loop.
But the interesting thing here is
that we can use conditional logic, right?
What if we said response here
and collect whatever the response is and say
if response is equal to, let's say bye
then I'm going to break.
So that if I run this again,
I say hi keeps asking me, and you know what?
I'm getting annoyed with you machine. I'm going to say bye.
It's going to exit it out for me.
How cool is that?
So while loops are extremely useful for tasks
like this where looping can happen for a long time
you don't know how many times it's going to happen.
But this is something that you're just going to
have to get used to with practice.
Eventually, and I promise you
this happens where you'll figure out
when to use while, when to use for loop.
But at the end of the day, use whatever solves your problem.
I'll see you in the next one. Bye-bye.