Last time we learned how to use four loops, to execute a chunk of code, a set number of times, this time we'll learn how to use nested for loops. So to build the nested for loops, we just put one for loop inside another, what are they good for? In general, they're good for traversing two-dimensional or higher dimensions, of data. To demonstrate nested for loops. I'm going to print out the multiplication table, for the numbers from 1 to10. So the first thing we'll do, is we'll print the header for that table. It will become clear to you, momentarily, why I'm starting, by printing five spaces. Now I'm going to use a For Loop, to print the numbers from 1 to 10, and this isn't the nested for loops stuff. This is just a regular for loop. And I'm going to do things just a little differently here. Because, in a table I want each output to be in a field, if you will, of a particular width, and I know how to do that in C++, we can just use set width, and say how many characters, that field is going to be. And then I can print out i, in a field of width five, I'll point out that set width is in the iomanip, iomanipulation library. So you need to pound include that library, if you want to use set width. But now you can run the code, to show that our header appears properly with that space at the beginning. The reason I want that space in the beginning is, because when I print the table, I'll show you where I needed that space. So here's the nested for loops stuff. My outer loop, I will set my loop control variable. And I could call this anything, it doesn't have to be i, it could be row or bob or joe or whatever. It's fairly common to use i as a loop control variable here, so I'm going to do that. And I also want to point out, that this i, is not the same as this i. Remember we talked about the scope of this i up here and it's from here to here. So the scope of this i is from here to here, and it's a totally separate variable, even though it has the same name. The reason I wanted to print out those first five spaces at the beginning of the table header, is because I know, okay, that here, I want to print out i, and I will also say that I need to put standard there. And I will also say that, here, right, before I look back in this outer loop, we don't have the inner loop yet. But before I looped back, I know I'm also going to want to print, a new line. And if I run my code now, I realized that the one that I wanted on the left is at the end of the headed. So I will print a new line here as well. Try that one more time, so there you go. I have the header along the top and each row will be the, we do road times, column, to do our multiplication table. So now all I'm missing, is that, inner loop. I'll do another for loop, and I can pick any variable name I want. Except i, if I pick i here, it's going to screw everything up, because this i here is visible from here to here. So it's really common, to say j equal 1, j less than or equal to 10. J++, and in the body of this inner loop, I will print out. Okay, in a width, of 5. The product of i times j, and of course I need to columns there, push that back where it belongs. And now when we run our code, we get the entire multiplication table, and you can convince yourself that this is correct. But each cell is the product of its row and its column. So how does this work, when we get to the outer loop. The very first time, we've just printed the header, we set i to 1, and we know 1 is less than or equal to 10. So we go inside the body of that loop. We print out i, and now we get to this loop, so we set j to1, 1 is less than 10. So we print out i, which is 1 right now, times j, which is also 1 right now. And then we get to the end of this inner loop, and we come back to the top, and we set j equal to 2, 2 is less than or equal to 10. So we print out 1 times 2 which is 2 and so on. So this inner for loop, works just like a for loop. Like we learned last time, now when this for loop ends, we've just finished printing a row of the table. So we move to the next line, and now we get to the end of the outer for loop, we increment i from 1 to 2, and then we come into the body again and we print out 2. And here's where people sometimes get confused. So, when we get to this inner loop again, it has no memory that it has ever executed before. So, we initialize j 1 and do that inner for loop. So, it's sort of like wheels spinning within wheels, right? We get to the outer for loop, and then we spin the inner for loop till it's done. And then we rotate the outer for loop one tick, and then we spin the inner for loop again and then we rotate and spin and so on. So, the important idea is, the inner for loop, always starts fresh whenever we get to it, and it works just like a For Loop. And the outer for loop, also works just like the for loops we've learned before. And that's how you use nested for loops, in C++. To recap in this lecture, you learned how to use nested for loops, to do something useful.