OK so it's time to learn about another type of loop in javascript called the for loop.
So the for loop is similar to the while loop and that they both let us repeat code.
They're a little bit different in their syntax.
So our objective is here.
You want to understand the purpose of for loops why you use one versus a while loop.
You want to write valid for loops and then compare and contrast while loops and for loops.
So to start here's a recipe for a for loop.
We write the word for a keyword and then in parentheses there are three parts.
And so I've some them up as the initialize the condition and the step.
So this will take a little bit to sink in and we'll clarify this over the next few slides.
But the key takeaway here is that we write the word for we have parentheses three different things go
in the parentheses and then we have curly braces.
So here's an example.
This is how you print the numbers from 1 to 5 with a for loop.
So we start with four.
Then we create a variable.
I'm calling it count and set it equal to zero.
In this first chunk then we have our condition while count is less than 6.
And then we have our increment which is going to add one to count.
So this is conceptually very similar to the wildlife's who've been writing.
So you can see the equivalent while loop down here.
The big difference is that this is just shorter and the other difference is that this variable count
only exists inside the loop.
So this variable for a while loop must be initialized outside so that we can use it here in a for loop
.
We can make a variable that exists only for a moment inside of this loop.
So to go over this one more time we're making a variable called count and it starts at zero every time
through the loop.
We add 1 to count and we keep going while count.
It's less than six.
So the first time we run this count is zero.
We printed out the next time we've added 1.
It's still less less than 6 so then we printed out and we keep going until count is six.
It's no longer less than six and the code stops running.
So I'm going to copy this to my counsel and just demonstrate that I paste that in and we get 0 1 2 3
4 5.
So here's another example taking what we did of the while loop where we printed every character in a
string but doing it with a for loop.
So once again you can see we don't have to create this count variable outside of a loop like we do for
the while loop.
We can create a temporary variable and I use one called I.
The reason I did that is that it's pretty conventional to see for loop variables be very short single
letter variable names like i or j or K and that's because they don't exist for any other purpose but
to live inside the loop.
So some people would argue it's better to keep them short.
So over here I used to count.
You can name these whatever you want but you'll see a lot more often in the real world.
So we make a variable equal to zero every time through the loop.
We're going to add one and we're going to keep going.
While it's less than the length of the string.
So the logic is the same as what we're doing here.
Make a variable called Count set it to zero.
Add one every time through and keep going while count is less than the length.
So when I run this I'll copy it.
Open up the console.
Paste that in and we get H E L L O.
And it prints one L with a 2 next to it.
That's just how Chrome does repeated confidant logs.
So to sum this up one more time instead of a for loop there are three parts.
There's the Initialize where we declare a variable and set it to some initial value does not have to
be zero.
Then the next part we have a condition which is when this loop should keep running.
And then the last part is our step.
So what do we do at the end of every iteration.
Do we add 1 to count or to subtract 1 Do we multiply it.
There's all sorts of different things.
Most often though you'll see it with a plus plus just like we did with while loops