‫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 four item in let's say a list one, two, three.
‫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 zero.
‫And then while I is less than, let's say, the length of the list, that is one, two, three.
‫Well, in that case, I want you to print I or the item and then increment.
‫I buy one.
‫If I run this.
‫I see that.
‫Well, technically here we're incrementing the AI, which is the value, but let's say we had a list
‫up here.
‫So my list and this list will be one, two, three.
‫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 want to loop over something that we already know.
‫How many times we want to loop over three times with a y 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're not sure how many times you want to loop over something.
‫You're not really sure how long it's going to take for looping.
‫So you want to 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 want to 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.
‫It's 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 call in 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 remove this?
‫And I click Run.
‫If I say something, I say hi and then it asking me again.
‫Hi.
‫I'm telling hi.
‫And it keeps asking me, asking me, ask 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 by 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 the next one.
‫Bye.