Now another way that we could have completed the challenge is by using a for loop. 1
Now a for loop works similarly to a while loop, but instead of testing a condition, we’re specifying the 2
number of times that we want the loop to run. And the syntax looks something like this. 3
So we have the keyword for and then a set of parentheses. 4
And the first statement in the parentheses, we’re defining a starting point for the for loop. 5
So in this case we're starting at 0. 6
The next step, we're defining the end point, 7
so we're ending when i 8
is less than 2. 9
And the last thing we have to tell the for loop is what is the change, 10
which direction are we changing i. 11
Is i increasing by 1 each time, 12
or you can increase it by 2 or 3, or you can even decrease i 13
every single time. 14
So if we take a look back at the while loop that we should now be familiar with, 15
you can see that the for loop is essentially just a reshuffling of all the elements of the while loop. 16
So we're changing the while keyword to the for keyword, and then the var creation goes into the parentheses, 17
defining the starting point, 18
so we're starting from 1. Then the next thing inside the parentheses is the ending point of the for loop, 19
so we'll end once i is less than 2. 20
And finally, if we're using the while loop, we have to include the code that changes i inside the while 21
loop. 22
Now it's integrated into the declaration for the for loop so that when we create the for loop we already 23
define the starting point, the ending point, 24
how much i is going to change, 25
and the code that should be carried out every single time the loop runs. 26
So, as you can see, the for loop is essentially what we would call in programming syntactic sugar. 27
It just sweetens up the syntax and makes it a little bit easier for developers and programmers to write. 28
So let's take a look at this for loop in action. 29
So, in our case, we've got a for loop where we start off with the variable i 30
that’s equal to 1, and we’re ending when i is less than 2, and we're incrementing by 1 each time. 31
And inside the for loop we're simply logging the value of i. 32
So once we get to this line, the computer will run this code and create a variable 33
i, and it will set it to equal 1. 34
That's the beginning value. 35
The next step is it reaches the second part of the for loop where it checks to see whether the current 36
value of i is less than 2. And 1 is in fact less than 2, 37
so it proceeds to run the code inside the for loop, 38
so inside those curly braces. And in this case it console.logs the value of i, which currently we know to 39
be equal to 1. 40
Now once it's gotten to the end of the for loop, 41
this is the point where the third value in that for loop is carried out, 42
so in this case i is incremented by 1. 43
So i is now equal to 2. And it's really important that you know when this happens. This happens exactly 44
at the point where the closing curly braces, 45
so where the arrow is pointing right now. 46
And now the code loops back to run the for loop again, and the first thing it does is it checks whether 47
it should continue running the loop. 48
So currently i is equal to 2. 2 is not less than 2, 49
so the conditions are not met and the code exits the for loop and instead goes to the next line. 50
So if we take a look at the code for our function fizzBuzz, this is how it looks at the moment using 51
the while loop. 52
Now if we wanted to use the for loop instead, then we can change the keyword from while to for, and inside 53
the parentheses, we first declare the variable that we're going to use to count. 54
So we can cut that from there and paste it here instead. 55
So now, inside the for loop, we're creating a counter, called count, starting point from 1 and ends at 56
100. 57
The next thing that we have to add is how are we going to change the counter. 58
And in this case we're actually changing it by increasing it by 1 each time. 59
Now of course you can decrease it by 1 each time as well, 60
so you could say count minus minus. 61
And this would work, say for example, if you're counting down from 100 to 1. 62
So then you would have the opposite code where count starts off being 100 63
and it decreases by 1 each time until it is no longer greater than 1. 64
So the for loop is really flexible like that and allows you to count in any direction and increase 65
your count by 2 or by 3 or by any number you wish really. 66
And now if I run my code and I call the function fizzBuzz, you can see that it does exactly the same 67
thing as we had with the while loop, but it's less wordy, more concise, and this is the format that most 68
programmers will prefer for a use case like this. 69
Now a lot of students wonder in which cases do I use the while loop and in which cases should I use 70
the for loop. 71
Keep in mind that while is essentially checking for a state, so it's while something is true, 72
so that can be while player one is alive. 73
And essentially you want to repeatedly run an instruction while the program is in a certain state. But 74
for the full loop you're essentially trying to iterate. 75
You're trying to run a piece of code many many times and you're going to use the for loop to define 76
how many times. 77
So with practice you'll find that that you'll gravitate toward one or the other type of loops while 78
you're writing code with different purposes. 79
So in the next lesson I've got yet another challenge for you. 80
And you can see the last challenge in the modules as somewhat of a boss level. 81
It's probably going to be the hardest but the most interesting also. 82
So it's an interactive challenge complete with code checking. 83
So once you're ready head over to the next lesson and complete the challenge.