In this lecture, you'll learn about the for loop, and we use the for loop when we know how many times we need to iterate when we get to the loop as we run our code. Let's go take a look. The for loophole, right, will print the squares from 1 to 10. So we type for and we put opening closed parenthesis, and we have two semicolons here and then we'll put the body of the for loop. And just as with if statements, if we only have one statement as the body of the for loop, we can omit those curly braces, but I always suggest that you include them. We have two semicolons up here because we have three parts to our for loop. We initialize something that I'll call a loop control variable because this variable i will control the execution of the loop. The next piece that we have is some condition that will test, every time we get to the top of the for loop we'll test this condition to see if it's true, and if it's true we'll go into the body of the for loop again, and if it's false we'll go past this close curly brace and move on in our code. So my condition here will be i less than or equal to 10 and you should recognize this as a boolean expression, right? This will evaluate to true or false. And then finally I need to say how I'm going to modify the variable that controls the loop. The loop control variable as I will call it, and I will modify this by incrementing it. So ++ is the increment operator. And so if i is 0, i++ is 1. If i is 1, i++ is 2 and so on. In here, all I need to do is print out the square of i 1 per line. So I'll use standard cout, and I will print i * i, and I know I'm going to want a new line as well. So now I can run my code and it prints out the squares from 0 to 10 because I said i to 0 here not 1, so let's try that again. I want to start i at 1, so I'll run it again and I get the squares of the numbers from 1 to 10. Now, the way this works is the first time through the loop we set i to 1 and we check if 1 is less than or equal to 10, and of course it is. So I go into the body of the loop and I print out 1 * 1. Now I come back to the top again because I finished with the body of the for loop. And the first thing I do is I increment i, so now i is 2, 2 is less than or equal to 10. So I go into the body of the loop and I print out 2 times 2, I come back and make i3, and we keep doing this until we increment i and i becomes 11. When i becomes 11, 11 is not less than or equal to 10. So we break out of the for loop, we don't execute the body anymore, we just move past it to this line of code, which happens to be the end of our main function. Now you might see people using different formats for the for loop. For example, you could take this part and put it here instead and everything will run fine as you can see. The reason you might do that is because if we do it this way, which is the most common format for a for loop, this i is declared here so it's only visible here. So that is the scope, it's called the scope of i, it's where that variable i is visible, and that works fine for most for loops. But if you need access to i out here on line 17, i is no longer visible so you couldn't use it. So that would be one reason that you might put your loop control variable declaration and initialization before the for loop. You might also see people doing this and just incrementally i here instead, that's probably less common but that also works. And finally, you're likely to see people doing this instead of i++ and that also works fine as you can see. There are some subtle differences between this and this, but they don't matter in this particular case. But there are other cases in c++ where the place you put the ++ actually matters. We're not going to deal with any of those cases in any of the courses in the specialization but you should understand that ++i also increments i. Okay, so this is interesting, right? But let's actually make it so that the user can provide how many squares they want. So remember in the intro I said that a for loop is useful when we know how many times we're going to loop when we get to the code. That's not the same as saying, we know how many times we're going to loop at compile time. So we can prompt for and get how many squares to print. So we'll start with the prompt. And I'll just say, Enter how many squares to print, and I'll give the user a hint about what they should use as the range but they can ignore us as we'll see soon. And now we're going to read in how many squares to print. And by now, I mean after I declare the variable that actually will hold that number. So, We'll read into n. And we'll say here we're going to print the squares from 1 to n and we'll change our upper bound to n instead of 10. So now when we run, we can say how many squares to print and we can say 10 and it prints the squares of the numbers from 1 to 10. We could say 3 and it prints those squares as well. If we do something like this, if we say print 0, it doesn't print any. So here's how that works. If we read into n and we get to line 18 here, and we set i equal to 1, but then we check to see if 1 is less than or equal to 0, and 1 is not less than or equal to 0. So we don't ever execute the body of the loop and we just move on to this blank line after the for loop. So that's how you can use for loops in c++. To recap, in this lecture we learned how to use a for loop to execute a set of statements, the loop a body a certain number of times.