 Hi everyone, today we're going to be looking at nested loops in particular how to design a nested loop and then also how to trace it using a logic table. So I'm going to get started with creating a nested loop and what I want to do is I want to recreate this pattern of numbers here. So when we are designing a loop, in particular a nested loop, what we want to think about is what each of our individual loops are responsible for. So in this case, I'll have a doubly nested loop, so an outer loop and an inner loop, just two. And I want one loop to be responsible for the numbers I want to print and the other loop I want to be responsible for how many times I want to print them because if we have a look at this pattern here we're just printing the numbers from one to five but for each of these numbers it's printed a different number of times. So if we were to just focus on printing out the numbers from one to five that's something that we can do quite easily with a regular loop. So that would be a good place to start. So I know that I want to print the numbers from one to five, so I'll have a variable called i that starts at one. We'll continue to increase as long as it's less than or equal to five and then each time I want to plus one. And then I want to print that value to console. So this single loop here should effectively print the numbers one to five. So now we want to design our inner loop which will determine how many times each of these numbers are printed. And I'm going to do that using this table here I've created on the right. So first of all I'm going to write down the values of i that I want to print or the numbers that I want to print. So I know I want to print the numbers one, two, three, four, and five. And then my other column has a look at the number of times I want to print each number. So if we go back to our pattern here I want to print the number one five times. I want to print the number two four times, number three twice, sorry number three three times, number four twice, and number five once. So there are lots of ways to go about designing our inner loop. Lots of different ways to achieve it but I find the easiest way to design it is to try and get the number of times that it's being printed in terms of our i variable. So for each of these well I want to print the number one five times. So I want to rewrite the number five in terms of one. So another way of writing the number five would be six minus one because six minus one is five. And now that I've done that I've got a reference to my i variable. So let's see how that pattern keeps with the rest of our combinations. So when I've got the i value two and I want to print it four times. So if I can rewrite the number four in terms of two I know that four is equal to six minus two. So again that's another i variable here and then I can continue. So I know that I can well three is equal to three but I can also write three is six minus three. So now I've been able to identify a pattern here. So two can be rewritten as six minus four and five can be rewritten as six minus one. Sorry my mistake. One can be rewritten as six minus five. There we go. So when it comes to loops it's all about identifying patterns. So I've identified a pattern here that when I do six minus the i value I will get the number of times that I want to print it. So we know that these are all the same. So now that we've identified our pattern we can say that for each value of i we want to print that value six minus i amount of times. So now we have a value for how many times we want to print our i value. So that's what is going to be our inner loop. We need to design an inner loop that will run six minus i times. So we can say four in k is equal to one. One k must be less than or equal to six minus i plus one to k each time. Let's indent our code and there we go that works well. And this isn't the only inner loop that we can use as long as that inner loop runs six minus i times then we're happy there. So we could do something like we start at six, keep decreasing until we get to i and we minus one each time. Oops, my maths was a bit wrong there. Greater than i. There we go. Yeah so for this loop in particular as long as this runs six minus i times we're happy. So that's what nested loops is all about it's about identifying the pattern and then splitting up the responsibilities between the outer and the inner loop.