Hi everybody. Now that we know that arrays are an option for storing data, we need to think of the upsides and downsides of using them. The upside, of course, is that arrays can store a lot of things in a single variable, instead of declaring variable grade[1], var grade[2], var grade[3]. The downside is, is when a single variable stores a lot of data, sometimes it can be a little bit harder at first to access that data. So, what do we want to do if we wanna go through every single number, but we don't wanna write the same code 10 times, or 20 times, or 40 times? Well, what we wanna do is we want to use iteration, or what we typically call looping. When you loop through an array, you look at each element, usually starting at the first one and ending at the last one. So in our example here, we have an array of ten elements. How could we find the average of those ten elements? Well, we would start off by saying we need to go through and add 80 plus 87 plus 94 plus 82. We need to add them all up, and probably store them in a variable. Then, once you have that number, you need to divide it by however many elements there are. So, in order to do this, we are going to use what is called a for loop. A for loop is simply a construct where we can run the same code multiple times. But between each execution, we're going to check some sort of boolean condition to see if we're done or not. So here's a syntax. You need to have the keyword for. It lets JavaScript know, oh, change things here. I'm going to change the flow of control. The next thing you set up is the initializer. You need to have some sort of variable that's going to be used for counting how many times we move. We're going to have a boolean condition that's either going to be true or false. And then, typically, you need to have some sort of update or increment variable that lets us keep going. So we want to start with the keyword for. Then, you set that variable to the initial value. Usually we tend to call that variable index, or counter, or even i or j, something small. It's a variable we don't want for a long time, just for this for loop. Once you've set index or counter to zero, you want to setup, what's that boolean test case we're looking for? We need to write something that can evaluate to either true or false. Once you've written that, you need to think about what code you want to run when the boolean is true, and then fourth, you update your variable and you head back to two. So here's a little diagram of how that all works together. Here we have kind of a narrated explanation of what's going on. We start with our initialization. This only happens once. After you've initialized your variable, you head down and you check, and you say, hey, is my condition true or false? If it's true, we want to head down here and run all of our statements. Once you're done, you increment your variable, and you go back up and you check again. Hey, is it still true? As long as it's true, you're going to just keep looping down, and you might end up looping five or six times. You can't be sure. But once the condition evaluates to false, then you know oh, I'm all done. I'm not going to run this code anymore. And you hop out to the end. So the important thing to know for a for loop is that the initialization is always going to happen once, but you have no idea how many times these steps are going to happen. So here's a quick for loop that's going to go through and write each element of the array. We start at zero, we loop until we get to the length of the array and each time we add one to it. So if the array has ten elements, the for loop will runs ten times, and then it'll stop as soon as, because we know that ten needs to be less than the length of the array. If the array happens to have six elements, it will run six times. If it has two elements, it will run two times. We're very flexible here by using that array.length. So let's get back to that example where we want to find the average of all the grades that are stored in our array. What I've done, is I start off by declaring a variable that's gonna keep track of everything we've looked at so far. So at the beginning, sum is going to equal zero. Then it'll be 80. Then we need to add 80 to 87, so it keeps getting bigger and bigger and bigger. And you can keep track of all those numbers. Each time we loop, this is where we're adding in the next value. And we're going to through all ten times. When we're done with the for loop, when we're all done, that's when you want to go in and actually divide the number by the number of elements. It can be pretty tricky when you're writing this code, because if you're not careful about what you want to be inside the loop versus what you want to be outside the loop. You can get some really inaccurate answers. Okay so if you followed that code and you understand how it works that's great. What we want to think about instead is how what would happen to make that code not work? So the first question I would ask you is why did I use grades.length instead of "10"? We could've said, keep looping as long as the index is less than 10. And that would've been perfectly correct. But the problem is, what if later we decide to add some numbers to our array? Or take some numbers away from our array? Well then, that 10 is no longer correct, and you would need to remember to go in and change it. What would happen if instead of adding one to our counter we subtracted one? This happens a little bit more often than anyone wants to admit. And what's going to happen is you're going to have an infinite loop. You can end up never leaving that line of code, and it will stall your entire browser. So you wanna make sure that you're incrementing or decrementing the way you mean to. Along the same lines, what would happen if I set the condition to check if index was less than or equal to grades.length? I had initially used less than, and that was the correct way. If I use less than or equal to, the JavaScript will actually crash. The thing is, I bet a lot of people would never even realize it did, because JavaScript does such a great job of hiding its errors. You need to go into the console and see what's going on. So just to review, if you're learning to program in any programming language, looping is used for much more than arrays. It's a very powerful construct, and it's something you really want to understand very well. I also want you to realize that, just because I happen to do my loops from a low number to a high number, there's no set way that's the right way. You can also go from high to low, or even subsets. The most important thing is that I need you to practice, practice, practice. You need to feel comfortable with the idea that array elements start at zero. You need to understand that you can use array.length or other methods. The more you play with these things, the easier it will become. So thanks for watching.