Last time, we learned how to use for loops to execute the body of the loop a set number of times. This time, we're going to explore nested for loops. So what are nested for loops? They're when we put one for loop inside another and we are not limited to just one inside another, we can put a for loop inside a for loop, inside of a for loop and so on to solve whatever problem we're trying to solve. What are nested for loops good for? They are commonly used when we're traversing a two-dimensional or higher dimensions of data. So we will learn about something called arrays later in the course, and we'll learn about arrays as two-dimensional structures that mathematicians would typically call a matrix, and we'll learn how to use nested for loops to process those arrays. For this lecture, we won't do that but we will use nested for loops to traverse a two-dimensional structure. For our example here, we'll print out a multiplication table and you should be familiar with what these things look like. So the first thing we'll do is we'll print a header and I'm going to start by printing five spaces and you'll see why I do that too. Now I'm going to print out the numbers across the top and I'll use a for loop to do that. This isn't the nested for loop stuff, this is just the for loop stuff we learned last time and I'll go 1-10 again and I'm going to print each of the numbers across the top. My format specifier will be percent 5d instead of percent D. So back when we were talking about floating point numbers, you may recall that I said that we can include in our format specifier a width indication for how wide the field will be for the number that's printed and we're going to take advantage of that here. I said that might be useful when we print out tabular data, printing out tables and since we're printing out a table here, this will really be useful to us. So each time I'll print out i, you should be able to understand how this is going to work but I'll run it, and as you can see, we get that header across the top. Clearly, I'm going to want a blank line before I start printing the rest of the stuff so I'll add that now, and now we can print the table. You can think of it this way, we have 10 columns and for each row, we want to print the 10 numbers that are included in that particular row, the 10 columns for that row and then we'll move down a row and keep doing that until we've printed the entire table. I'm going to start with an outer loop and this outer loop is going to run over the rows in the table. I could in fact say, int row equal one, remember I can call my loop control variable anything I want, but it's also fairly common to use i. So I will use i for my outer loop and I'll point out that this i here is different from this i here. Remember we talked about variable scope and I said that this i is only visible here and that's true. So, this i is a totally different i even though it has the same name. So the first thing I'm going to do in each row is, I'm going to print which number this row corresponds to. So again, this will be percent 5d and I'll print out i and that's why I added these five spaces here because I knew I was going to put the role multiplicand in the beginning of each row. Now, the nested for loop stuff. Again, each of these will be the column and it's common to use j for that. I'm going to do a 10 by 10 multiplication table and j plus plus, and really at this point, I'm going to be doing this except with j not i. Let's run it and see the multiplication table and then we can talk about how it's working. Before I do that, I know at the end of each row, I'm going to want a new line. Okay, now let's run it. That is ridiculous, that is not a multiplication table so I was wrong. Of course I don't want to just print a j here, I want to print i times j because this is a multiplication table, let's try again. There we go. That is a multiplication table and of course that's correct. You can look at a variety of those different multiplications and see that I'm in fact doing it properly now. So how does this actually work? Printing the header is just a for loop like we learned last time. So that shouldn't be confusing in any way, but nesting the loops can be a little confusing. So let's talk about how this happens. We come to the outer loop so i is one. Now, we come to the inner loop so j is one. So the first thing we print is one times one. Now, we loop around in the inner loop, j gets set to two, two is less than or equal to 10. So now we have one times two and this keeps happening until j gets set to 11 here and 11 is not less than or equal to 10. So we break out of that nested loop, we print the new line for the end of this particular row and now we come back to the outer loop where we change i to two. Now, we come back to the inner loop again and the inner loop has no memory of the fact that it ran before and it's running again, it doesn't know any of that stuff, it just knows that when it gets here, j gets set to one we do the test and of course one is less than or equal to 10. So we come here, so now we're doing two times one and so on. When we enter the outer loop, we set i and then we spin through the inner loop until it's done and then we come back to the outer loop and move that along to the next row and then we spin through the inner loop again and so on and that's how nested loops work. To recap, in this lecture, we learned how we can use nested for loops to do computations in two-dimensions. In this particular, case, we built a multiplication table but we'll see when we get to processing arrays that we can do lots of other things as well when we're dealing with two-dimensional or higher structures.