Last time we saw how we can use a for loop to execute a block or a chunk of code a set number of times. This time we'll look at nested for loops. What are nested for loops? Nested for loops are when we put one for loop inside another for loop. What are they good for? They're most commonly used to process two or higher dimensions of data, though we'll see some other examples as well in the lectures in this lesson. Let's go take a look. The problem we're going to solve here, is we're going to print out the multiplication table from 1-10. The first thing I want to do is print a table header, and I'll start using Console.Write not WriteLine, to print five spaces. It will become clear as we work through this problem why I'm doing that, but now I can print the rest of the table header which will be the numbers from 1-10. Of course I can use a for loop to do that. I'm going to start i at one here not zero, and I'll say i less than or equal to 10 and I'll increment i each time. Now I want to print again using write not WriteLine, I'm going to want to print i. But I actually want to put it in a field that's five spaces wide including the i so that my table looks nice and the way I do that is I use something called a format string. The way the format string works is, I put the format string between these double quotes and then I say what I want to print out. My format string has curly braces on both sides. The first thing I put is which of the arguments following the format string do I want to print here? I want to print the 0th argument I only want to print this i, but the other piece that I can provide and there's lots more you can read about this in the book if you want to but just to set the field width I can say I want this in a field of five. It's going to print i in a field width of five, including the value of i. When I'm done because I'm just printing that row, I want to do a WriteLine. Let's run the code and see the table header , and there you go. I have those five spaces at the beginning and then I have one printed in a field of five and it's right justified and by the way if I made this negative 5 instead, then all those numbers are left justified instead but I want them to be right justified. I've used a regular for loop. There's no nested for loop here. This is a regular for loop to print the table header. Now I want to print the multiplication table. Here's where we'll get to use nested for loops. I'll start and notice that this time since I used i equal one in the previous for loop it suggests I do i equal one here also and I will, and I'll accept the rest of that too, because I want to do it the way I did above. I'm going to add spaces even though they weren't suggested there and they're not necessary. Spaces are just what are called whitespace and they're just to improve readability. Now I'm going to print all the rows in the actual table. The first thing that should appear in each row, is the i for that row and I can write justify that as well. I'll actually just grab this code right here, and that will print out i at the beginning in the row. I also know that at the end of each row, I'm going to want to have a Console.WriteLine. Let's actually see what this looks like when I run it. That's the basic stuff for my multiplication table. All I need to add is the actual multiplication table. Here's where we get to use nested for loops. Before we do I want to point out that this i is not the same as this i, the scope of this i, the places where this i here is visible, is right there. If I actually tried to output i down here, I'll get a compilation error. It says the name i does not exist in the current context because this is the scope of i, so i is not visible outside that for loop. That means that this i is a totally different i from this i even though they have the same name. You might say well that's confusing you should use a different loop control variable name down here, and you could. We'd been using i but you could call it Bob or Mary but it's pretty common to use i as our loop control variable. It's not really confusing to most people who are used to dealing with for loops. I will say though that here we want another for loop, but you'll notice I get a suggestion that we use j here instead of i and we're going to take that suggestion. That's a great suggestion, so we'll do it and I like the rest of that suggestion too; I'll just take it. If we use i here that's going to be a problem, because the scope of this i is here. If I try to use i here the compiler will say you already have an i that's in scope here so you can't use the same variable name twice. It's also really common to use j as our loop control variable for a loop that's nested inside a loop that uses i. Now we'll actually just print out the multiplication table part of this particular row. We're only working on one row right here. I'll Console and I'll use write and here I'm going to again use a format string to output in a field of five. Now I need to output the product of i times j. We'll talk about how this works in just a moment. I think I'll get rid of this extra whitespace I'm just printing each row here so I don't need to separate those chunks of code. Let's look at the output and then talk about how it works. There you go. That's the multiplication table. You can convince yourself that this is a correct multiplication table but it certainly is correct. How does this all work? When we get to this for loop, we set i to one, we make sure that i is less than or equal to 10 and it is. We come down into the first row and we print one. Now we get to this for loop and j gets set to one and one is less than or equal to 10 so we print 1 times 1. We come back to the top of this inner for loop, we make j two, two is less than or equal to 10, we print one, we haven't changed i yet but we have changed j to two so we print 1 times 2. We come back again set j to three, three is less than or equal to 10 we print 1 times 3 and we do that until this for loop is done. In other words once we increment j to 11, 11 isn't less than or equal to 10, so we're done with this for loop. We do our WriteLine so that we move to the next row and that's the end of the body for our outer for loop. We come up here and change i to two, two is less than or equal to 10, we print two at the beginning of our row, and now we get to this inner for loop. This inner for loop has no memory that it ever ran before. When we get here, we just started fresh like it's never been executed before. We set j to one, one is less than or equal to 10 so we print out 2 times 1 which is two. Then we set j to two, two is less than or equal to 10 so we print 2 times 2 and so on. That's the part that sometimes confuses people with nested for loops, that when we get here, it's brand new. It's not continuing from the last time we ran this nested for loop, this is getting to it for the very first time from the perspective of this for loop. You can think of this as wheels within wheels if you'd like. We have this outer wheel that we start and then we spin this inner wheel, and when we're done we forward this outer wheel one tick of a revolution. Then we spin this inner wheel again and then we just keep doing that. That's how we can use nested for loops in C-Sharp. To recap, in this lecture we saw how to use nested for loops to solve a problem.