 Hi everyone, we're going to look at tracing nested loops now using a logic table. So when we are creating a logic table just as we would with a single loop, we want to create a column for each variable and each condition that we come across. So if we go through our code here, I know that I've got a variable called result that I want to trace the value of, so I'm going to put that at the end. And if I keep tracing through my code here, I've got a variable called i. I've got a condition that i is less than 5. If I keep going, I've got a variable k and I've got a condition k is less than or equal to i. So for each condition, for each variable I come across, I create a column. And say if you have a condition that is something like, here you've got one Boolean expression but you've also got two individual Boolean expressions that create one. So in this case here, what you would want is one column for this one, one column for this Boolean and then another column for the whole condition just to make keeping track of it a lot easier. That's what you would want to do. But we don't have that this time. We've got a simpler nested loop. So let's get started with tracing it. So to begin with, we've got result is equal to zero so I can fill that in. And now I get to my outer loop and I've got my i variable is initialized to one. And my condition is that i must be less than five. One is less than five. That's true. So that means that we enter our loop and then we hit another loop. And this loop says for k is equal to one. So k is one here. k must be less than or equal to i. So our current i value is one. So our k is also one. So one is less than or equal to one is true. So now that we've entered this inner loop, we're stuck in this loop and we're going to keep on going through this loop until it terminates before we exit that and get to the bottom of our outer loop. So we've entered this here. We're on line four and four says result is equal to result plus i. We get to the end of our inner loop, which means we apply our update. Remember that happens at the end of our loop, k plus plus. So what is two less than or equal to i? It's two less than or equal to one. That's false. And again, I just want to remind you it's important to have this here because it shows that you understand where a loop terminates, where it's finished. So it's important to have that case there. And so at that point, that means that our condition is false. So we're finished with this loop and we exit this loop here, which means we get to the bottom of our outer loop. We know that when we get to the bottom of a for loop that we apply our update. So now i is equal to i plus one. And this is the part where some students tend to get a little bit stuck. So I'll go through this a little slower. So I've incremented i, which means I'm back up here and I'm checking my condition. i is less than or equal to five. Sorry, not less than or equal to just less than is two less than five. Yes, that's true. And at this point here, it's easy to assume that k is equal to two because that's the last value that it was at. But what happens is just like when we had a look at global and local scope of variables a few weeks ago. So if you declare a variable inside of setup, you won't be able to access it inside of draw because that's a local variable only accessible inside setup. It's a similar thing with loops. So if you've declared a variable inside of a loop, it only exists in that loop. So if I was to say out here, print the value of k, it'll say that k doesn't exist because k only exists inside that loop. It'll be the same for i because i only exists inside this loop. So as soon as we exit this loop here, we don't have a reference to k anymore. So this second time that we come around and we get to this point here, k doesn't exist. So what we're saying is in k is equal to one. Okay, I've got a new k variable. Oops. And it's equal to one. So every time that we get to this loop again, it's a fresh loop. That variable does not carry over for any variables that are declared inside of this for loop. It's different for result because result was declared outside of both those loops. So in this case, it's a global variable. It can be accessed anywhere. So yeah, that's something to keep an eye out for if you're ever having troubles with your code or you're finding your loops have some bugs in them. Have a look at whether your variables are local or global. So anyway, from that side note, we'll continue tracing. So now we've got k is equal to one. Is k less than or equal to i is our condition. i is currently two. So it's one less than or equal to two. Yes. So what are we doing? Line four says result is equal to result plus i. So result is one. We're plusing i and it's three. We get to the end of our inner loop and we're doing k plus plus. So now k is equal to two. Is two less than or equal to i? Well i is two and two is equal to two so we get true. So result is equal to result plus i. Result plus two equals five. Increment k again is three less than or equal to two. False. So I finished here which means that we've finished with line five and we've gotten to line six which for a for loop means that we're increasing our i value. So now i is three. Is three less than five. That's true. So we enter this. We've got a new value of k and it's one. Is one less than or equal to i? i is three. One less than or equal to three is true. Result is five and we're plusing i. And then we just continue tracing. So this might take a while so I'll speed through this one. So now k plus plus. Two is less than or equal to three is true. Eight plus three. k is now three, k plus plus. Three is less than or equal to three is true. Three we get 14. k is four. Four is less than or equal to three is false. Which means we've exited this inner loop and now we've gotten to the end here which means we're increasing i. So i is four. Is four less than five? Now we've got a new k. k is one. We go around again. Is one less than or equal to i? i is now four. So it's important to look at the current value of i. One is less than four is true. And line four says that we're adding i. So 14 plus four. k is two. Two is less than or equal to four is true. Oh, I've done some bad maths there. Oh no, I just forgot a one. There we go. We're adding i. Increasing k. Three is less than or equal to four is true. Increasing k. Four is less than or equal to four is true. k is five. Five is less than or equal to four. That's false. So that means that we've finished with this loop. We've gotten to the end of our outer loop which means we're increasing k again. Is five less than five? False. So we've finished here. We've gotten to line seven. And we're printing result. So when we print result, if we run that there, we've said that result is 30. I've lost my print statement. There we go, 30. Great. So main thing to be careful about when tracing nested loops is considering the scope of your variables. And so that should hopefully help you when you're also designing nested loops as well.