In this lecture, we'll learn about the for loop. The for loop is the loop that is most commonly used in C, when we know how many times we're going to loop, when we get to the loop. You'll see in a few minutes why I phrased it that way. For our first for loop, we'll print the squares from 1-10. So the syntax for a for loop is we use the keyword for, we put parenthesis, and we put the body of the for loop. Just as for the if statement, if we only have one statement inside our for loop we can omit the curly braces, but I suggest you always include the curly braces. Okay, so what do we put inside the parentheses here? We put two semi-colons, so there are three sections that we need to fill in here. The first section is where we initialize a variable that we're going to use to control our loop, and this variable is regularly called the loop control variable for that reason. So I'm going to declare an int, I'll call it i, and I'll initialize it to one because we're going to print the squares from 1-10. Now, this does not have to be an int, you can use other datatypes for it as well. You don't have to call it i, you could call it Bob, or Joe, or loop control variable. Although I will say it is regularly called i, and you don't have to start it at one, although of course it makes sense for us to start it at one here. The next section is the condition that we will test to see if we should keep looping or not. So because I want to run i from 1-10, the condition I'll check is i, less than or equal to 10. But every time we finish the body of the loop we'll come back to the top again and we'll check this condition, and if this condition is true we'll keep looping, and if it's false we'll stop looping, and you should of course recognize this expression as a Boolean expression. There's actually one more thing we do when we finish the loop and come back to the top before we check the condition, and that is we modify the loop control variable. I'm going to say i plus plus. Plus plus is the increment operator, so this adds one to i. This is identical to saying i equals i plus 1, but people regularly use this shorthand to increment i. Now let's make the body of the loop actually do something. I said we're going to print the squares from 1-10, so let's do that. Print f, and I'll say the square of, here's where I'll print i, is and there's where I'll print the square of i. So we need to provide two variables here, I'm going to print i, and I'm going to print the square of i. Now of course I could have pound included math.h and then above here, but I'll just do i times i, that's fine. When I have 6, we can see that it compiles fine, and now I'll run it. So there you go. As you can see, it prints the squares of the numbers from 1-10, and then it stops looping because this condition is no longer true. When we've just printed the square of 10, and we come back here, we add one to i. So now, i is 11 and 11 is not less than or equal to 10, so we exit the loop and we come down here. Now, you might be wondering why I said in the intro that for loops are used when we know how many times it we're going to loop when we get to the loop. I said it that way because we don't have to know how many times to loop at compiled time, we just have to know how many times to loop when we actually get to the loop at runtime, and I'll show that to you now. So I'm going to get how many squares to print from the user instead of hard coding it. I've limited it to 1-10, but of course the user can just ignore me there. Now we're going to want to read in the integer that they provide, and of course I now realize that I never declared a variable to hold to their input. So here I'll say address of n, even though I haven't declared on yet, so now all come up and declare n. Now I have two more things to change, and we'll see this in action. I need to change the comment because now we're going to print from one to n, and I'll put n there as the upper limit for how many times to loop. Now when I run the code, I can say 10 if I want, and it prints the squares from 1-10, but I could also say 3, and it prints the squares from 1-3, and so on. I'll actually show you, I could say 0, and it doesn't print any squares, and I could even ignore the prompt and say 11, and it'll print the squares from 1-11. So why did entering zero make it not print anything? If n is zero, we get down here and we set i to 1, and we check to see if 1 is less than or equal to zero, and it's immediately false, so we don't go into the body of the loop, we just go past the loop and get to the end of our program. Now I've claimed that this is the format for for loops, and this is by far the most common format you'll see with for loops. But you actually can leave sections of the for loop blank if you want to. For example, if we decided to actually initialize i outside the for loop instead, then we can leave this first part blank and the for loop works exactly the same way it worked before. Here's why you might want to do it that way. If we put this, here where we originally had it, then i is only visible within this block of code. The scope of i, the scope of the variable i which determines its visibility in our code, is only from here to the end of the close curly brace. That works fine most of the time for us, but if you actually want to be able to access i after we've gotten past the end of the loop, you won't be able to if you do it this way. So that would be one reason if you needed to get at i after the for loop was done. Similarly, we can just delete this here and put it here instead, and our loop still works the way it did before. But usually, people will just put this up here like we originally had it. Testing one more time to make sure it works fine, and that's how we do a for loop. To recap, in this lecture we learned about the for loop, which is the most commonly used loop in C when we know how many times we're going to loop when we get to the loop. So we don't have to know how many times we're going to loop at compile time, but we do have to know how many times we're going to loop when we actually get to the loop as we're executing our code.