WEBVTT 0 00:00.880 --> 00:05.860 Now another way that we could have completed the challenge is by using a for loop. 1 00:05.980 --> 00:13.450 Now a for loop works similarly to a while loop, but instead of testing a condition, we’re specifying the 2 00:13.450 --> 00:19.120 number of times that we want the loop to run. And the syntax looks something like this. 3 00:19.150 --> 00:22.380 So we have the keyword for and then a set of parentheses. 4 00:22.570 --> 00:28.140 And the first statement in the parentheses, we’re defining a starting point for the for loop. 5 00:28.210 --> 00:31.460 So in this case we're starting at 0. 6 00:31.600 --> 00:34.410 The next step, we're defining the end point, 7 00:34.450 --> 00:36.490 so we're ending when i 8 00:36.490 --> 00:37.800 is less than 2. 9 00:37.930 --> 00:42.410 And the last thing we have to tell the for loop is what is the change, 10 00:42.670 --> 00:44.830 which direction are we changing i. 11 00:44.900 --> 00:47.770 Is i increasing by 1 each time, 12 00:47.860 --> 00:52.080 or you can increase it by 2 or 3, or you can even decrease i 13 00:52.330 --> 00:53.500 every single time. 14 00:53.500 --> 00:58.330 So if we take a look back at the while loop that we should now be familiar with, 15 00:58.540 --> 01:03.890 you can see that the for loop is essentially just a reshuffling of all the elements of the while loop. 16 01:03.910 --> 01:11.350 So we're changing the while keyword to the for keyword, and then the var creation goes into the parentheses, 17 01:11.500 --> 01:13.380 defining the starting point, 18 01:13.390 --> 01:18.860 so we're starting from 1. Then the next thing inside the parentheses is the ending point of the for loop, 19 01:18.910 --> 01:21.870 so we'll end once i is less than 2. 20 01:21.880 --> 01:27.520 And finally, if we're using the while loop, we have to include the code that changes i inside the while 21 01:27.520 --> 01:28.180 loop. 22 01:28.180 --> 01:34.930 Now it's integrated into the declaration for the for loop so that when we create the for loop we already 23 01:34.930 --> 01:37.580 define the starting point, the ending point, 24 01:37.720 --> 01:39.640 how much i is going to change, 25 01:39.730 --> 01:43.180 and the code that should be carried out every single time the loop runs. 26 01:43.180 --> 01:48.920 So, as you can see, the for loop is essentially what we would call in programming syntactic sugar. 27 01:49.180 --> 01:56.440 It just sweetens up the syntax and makes it a little bit easier for developers and programmers to write. 28 01:56.440 --> 01:59.680 So let's take a look at this for loop in action. 29 01:59.860 --> 02:04.400 So, in our case, we've got a for loop where we start off with the variable i 30 02:04.420 --> 02:11.680 that’s equal to 1, and we’re ending when i is less than 2, and we're incrementing by 1 each time. 31 02:11.680 --> 02:15.370 And inside the for loop we're simply logging the value of i. 32 02:15.460 --> 02:20.840 So once we get to this line, the computer will run this code and create a variable 33 02:21.100 --> 02:23.480 i, and it will set it to equal 1. 34 02:23.530 --> 02:24.960 That's the beginning value. 35 02:25.240 --> 02:31.510 The next step is it reaches the second part of the for loop where it checks to see whether the current 36 02:31.510 --> 02:36.800 value of i is less than 2. And 1 is in fact less than 2, 37 02:36.940 --> 02:40.970 so it proceeds to run the code inside the for loop, 38 02:40.970 --> 02:47.500 so inside those curly braces. And in this case it console.logs the value of i, which currently we know to 39 02:47.500 --> 02:49.720 be equal to 1. 40 02:49.720 --> 02:52.680 Now once it's gotten to the end of the for loop, 41 02:52.840 --> 02:57.640 this is the point where the third value in that for loop is carried out, 42 02:57.640 --> 03:00.580 so in this case i is incremented by 1. 43 03:00.650 --> 03:07.450 So i is now equal to 2. And it's really important that you know when this happens. This happens exactly 44 03:07.450 --> 03:10.460 at the point where the closing curly braces, 45 03:10.480 --> 03:12.860 so where the arrow is pointing right now. 46 03:13.180 --> 03:21.040 And now the code loops back to run the for loop again, and the first thing it does is it checks whether 47 03:21.160 --> 03:23.600 it should continue running the loop. 48 03:23.710 --> 03:27.600 So currently i is equal to 2. 2 is not less than 2, 49 03:27.730 --> 03:34.680 so the conditions are not met and the code exits the for loop and instead goes to the next line. 50 03:34.690 --> 03:40.690 So if we take a look at the code for our function fizzBuzz, this is how it looks at the moment using 51 03:40.690 --> 03:41.920 the while loop. 52 03:41.920 --> 03:48.220 Now if we wanted to use the for loop instead, then we can change the keyword from while to for, and inside 53 03:48.220 --> 03:52.090 the parentheses, we first declare the variable that we're going to use to count. 54 03:52.090 --> 03:55.200 So we can cut that from there and paste it here instead. 55 03:55.450 --> 04:02.830 So now, inside the for loop, we're creating a counter, called count, starting point from 1 and ends at 56 04:02.890 --> 04:04.330 100. 57 04:04.360 --> 04:08.900 The next thing that we have to add is how are we going to change the counter. 58 04:09.130 --> 04:15.010 And in this case we're actually changing it by increasing it by 1 each time. 59 04:15.010 --> 04:18.130 Now of course you can decrease it by 1 each time as well, 60 04:18.130 --> 04:20.280 so you could say count minus minus. 61 04:20.540 --> 04:26.100 And this would work, say for example, if you're counting down from 100 to 1. 62 04:26.140 --> 04:31.580 So then you would have the opposite code where count starts off being 100 63 04:31.810 --> 04:37.490 and it decreases by 1 each time until it is no longer greater than 1. 64 04:37.510 --> 04:43.840 So the for loop is really flexible like that and allows you to count in any direction and increase 65 04:43.840 --> 04:48.070 your count by 2 or by 3 or by any number you wish really. 66 04:48.080 --> 04:54.970 And now if I run my code and I call the function fizzBuzz, you can see that it does exactly the same 67 04:54.970 --> 05:01.390 thing as we had with the while loop, but it's less wordy, more concise, and this is the format that most 68 05:01.390 --> 05:05.130 programmers will prefer for a use case like this. 69 05:05.140 --> 05:10.120 Now a lot of students wonder in which cases do I use the while loop and in which cases should I use 70 05:10.120 --> 05:10.890 the for loop. 71 05:10.930 --> 05:17.840 Keep in mind that while is essentially checking for a state, so it's while something is true, 72 05:18.130 --> 05:22.410 so that can be while player one is alive. 73 05:22.660 --> 05:29.620 And essentially you want to repeatedly run an instruction while the program is in a certain state. But 74 05:29.620 --> 05:32.900 for the full loop you're essentially trying to iterate. 75 05:33.220 --> 05:38.440 You're trying to run a piece of code many many times and you're going to use the for loop to define 76 05:38.440 --> 05:39.760 how many times. 77 05:39.760 --> 05:46.720 So with practice you'll find that that you'll gravitate toward one or the other type of loops while 78 05:46.720 --> 05:49.180 you're writing code with different purposes. 79 05:49.180 --> 05:52.840 So in the next lesson I've got yet another challenge for you. 80 05:52.900 --> 05:57.210 And you can see the last challenge in the modules as somewhat of a boss level. 81 05:57.220 --> 06:01.390 It's probably going to be the hardest but the most interesting also. 82 06:01.570 --> 06:04.640 So it's an interactive challenge complete with code checking. 83 06:04.660 --> 06:08.370 So once you're ready head over to the next lesson and complete the challenge.