Transcript of the tutorial.
TOP
Okay then.
So in the last video we saw how to create a for loop.
Now, in this video I want to talk about a different type of loop called a while loop.
Now they do exactly the same thing.
They loop through some code a certain amount of times.
There's no real difference.
The only difference is the syntax, how we write them.
So this was our for loop from the last video.
We had three things inside here, an initialization.
Then we had a condition, then we had a final expression where we incremented I each time around.
So if we run this, we should see I go up to four from zero and we do.
So what we're going to do is change this into a while loop.
So first of all, we change for into while.
Then we don't have all three of these different statements inside the parentheses.
We only have one of these statements inside the parentheses and that is the condition itself.
So right here we're saying while I is less than five, I want you to run this code inside the code.
Block Now, there's a couple of problems with this.
The first problem is that we don't even know what I is.
Nowhere in this file do we initialize.
I before when we use the for loop, we did it inside the parentheses, but we don't do that in a while.
Loop.
I must already exist somewhere.
So we say let I equal to zero before the actual loop begins.
We declare that outside of the loop.
Now, I'm not going to run this because if I do, it's going to be a big problem.
We're going to get stuck in what's known as an infinite loop.
So why is it an infinite loop?
Well, think about it.
We're saying that I is equal to zero to begin with.
Then we're coming to this loop and we say while I is less than five, then execute this code.
Well, it is less than five, it's zero.
So it does this code then we're not incrementing.
I anyway, we're not adding one.
So it's just going to come up again and say is I less than five.
Well yeah, it's still zero.
So we're going to run it again.
And over and over and over and over until eventually your browser will probably crash because we're
outputting it so many times.
Because I is never changing.
That is an infinite loop and it's a really bad thing.
So what we need to do before we run this is find a way to increment I every time around.
Now, before when we used a for loop, we did that inside the parentheses.
But again, we don't do it here in a while loop.
We do it inside the code block so we could do it at the very end right here.
So we run the code and then at the end of the code block, we say take I and add one to it.
Then when it comes to the top again, it's going to say, well, I is one now.
It's still less than five, but eventually I will get to five and then it won't run.
So we're not caught in an infinite loop.
Okay, so if I save this again, I'm going to go over to the browser and now we can see exactly the
same result.
So this works.
So this is just a different way of creating a loop, using a while loop instead of a for loop.
Okay.
So before we did an example with some data, some names.
So what I'm going to do is also create a constant here called names and set those equal to an array.
We'll put it inside Sean, Mario and Luigi.
Okay, so we have those names and we want to do the same thing.
Now I'm going to comment this stuff out.
I want to cycle through those names using a while loop.
So what I'm going to do is again say, let I equal to zero.
We still need that initialization variable.
Then I'm going to say while I is less than names dot length, I want to do something.
And inside here, what do I want to do?
I just want to log out the name.
So I'll say console dot log and then in parentheses the names and then we'll pass I in square brackets
to get that position from the names array.
Now we need a way to increment I otherwise we're going to get stuck in an infinite loop.
So we'll say I plus plus at the end and this is going to do the same thing as the for loop before it's
going to cycle through these and it's going to output the name each time around.
So save it and preview and boom, we get those names.
Okay, so there we go.
That is a while loop.
Just an alternative to a for loop.