In this lecture, we'll explore another control structure called iteration, which lots of people call looping. The big idea is, we can repeat a block of code, a set of statements in our code, either a certain number of times or while some condition is true. We have a couple of classes of problem we try to solve with iteration. If we know how many times we need to loop when we actually get to the chunk of code we want to repeat, we use either a for loop or a foreach loop. We'll cover for loops in a few videos this week, and then we'll cover foreach loops next week. In a few lectures, we'll actually see how we can iterate when we don't know how many times we need to iterate or loop when we get to the chunk of code for which we want to repeat those statements. Let's go see how for loops work in this lecture. The problem we're going to solve here is that we're going to print the squares from 1-10. This is a great example of where a for loop will be really useful because we want to repetitively do something a certain number of times. We use the for keyword and then we have an open parenthesis, and we do three separate things in between these parentheses. We initialize what I'll call a loop control variable, because it's a variable that controls the loop and I'll call my loop control variable i that's pretty common for for loops, and I'll initialize it to a particular value. You'll notice that the IntelliSense is suggesting that I initialize it to zero, but I actually went to initialize it to one instead, because I'm going to print the squares from 1-10. If I were printing the squares from 0-10, initializing i to zero would have been a better choice. Then I put a semicolon and I put what's called the condition. Again, the IntelliSense is suggesting something that I don't want to use, I want to use i less than or equal to 10. You should look at that and say, that's a Boolean expression like we used in if statements, because it's a Boolean expression like we used in if statements. Finally, I'll add a modifier that specifies how I'll change i each time through the loop, and this time, I'll take the suggestion that I went to increment i. I then put the body of the loop in between an open curly brace and a close curly brace but I'm not going to do what the suggestion is, so I'll just hit Delete to get rid of that suggestion. I'll finish out the loop body, and then we'll talk about how the code works after I run it the first time. We went to Console.WriteLine, but we don't want to write line i, we want to write line i times i which squares i. I'll run the code and then we'll talk about how it works. As you can see, this prints out the squares of the numbers one through 10. Here's how the for loop actually worked. When we get to the for loop, we initialize our loop control variable, and we're setting i to one, and then we check our condition and one is less than or equal to 10 so we'll go into the loop body and execute the code in the loop body. Right here, i is one, so we print out 1 times 1, and then we reach the end of the loop body, so we come back up here and we increment i. We modify i by incrementing it, and now i is two and we check our condition again, and two is less than or equal to 10, so we come into the loop body again. This time, we print out two times two and we come back and we make i3 and so on. We just keep iterating, we keep executing the body of the loop until we print out 10 times 10, and then we come back up here and we increment i, so i is now 11 and 11 is not less than or equal to 10, so we stop executing the loop body and we go past that close curly brace for the for loop. We come down here which happens to be the end of our main method. That's how the for loop works. Remember, I was very careful with my freezing in the introduction though, I said we can use a for loop if we know how many times we need to iterate when we get to the code. We can actually ask the user, how many squares they want to be printed out? I'll use the console write to prompt the user. Since there's a specific rings that I want them to provide, I'll say 1-10, giving them a hint for what valid input will be, even though they can ignore that hint and we'll see that before we're done. Then I'll declare n equal to int.Parse Console.ReadLine. As you can see, I got some nice suggestions there, so I could just hit Tab to complete my code. Now, I'll come down here, and I'll print the squares from one to n instead of from 1-10, and I'll replace this 10 by n. This doesn't have to be a hardcoded value, it can be the value of a variable or even an expression. Now, when I run my code, I can still print the squares from 1-10 by entering 10. I can print the squares from 1-3. I said I can ignore what the prompt says is a valid range, I can actually say zero, and it doesn't print anything. It's possible that the body of our for loop won't execute at all, because when I entered zero, I initialized i to one, and then I checked, is one less than or equal to zero. Because of that Boolean expression evaluates to false, I never execute the body of the loop, I just go right past it. One more thing before we stop, I did say we can have an expression here, I could say n times 2. If I say I want to print out the squares from 1-10, I can actually print out the squares from 1-20, because I included an expression here in my Boolean expression. I'll change that back to n, so it does with the comment said it would do, testing one more time to make sure I didn't break anything, and that's how we can use a for loop in our C# code. To recap in this lecture, you saw how we can repeat a set of statement a number of times using a for loop.