 Hi everyone today. I'm going to be tracing a single while and for loop using a logic table here So I'll be explaining how we can use a logic table To explain our thinking and show its use when it comes to tracing loops say in an exam Or in an assignment So I'm going to start with a while loop here. I've got this while loop here I've got an integer a which is six. I've got result which is one so I've already added those values here And then my condition is that while a must be greater than zero So when you're creating logic tables You want to have a column for each variable that you have And then a column for each conditional you have so we only have two variables and a single conditional here So it's quite a straightforward Logic table, but I'll show you later on a more complex logic table and how you would deal with that So we're going to start Tracing our code. So as I said before I've already filled in my values for a and result which is with this initializing here And what we want to do is for every value of a because we can see that a is being updated here For every value of a we want to trace and see Whether our conditional returns true or false So the first time we enter our loop we get to line three And we say while a is greater than zero At this point a is six Six is greater than zero. So we'll enter true I'm just going to write T in this column here And if that condition is true, then we know that we enter the loop So if we enter the loop and we get to line four and line four says result is equal to result times a So we are updating the value in the result column with the current value of a which is six So result is equal to result times a so we're multiplying by six So now our result is equal to six So we've done line four we get to line five and a is now equal to a minus one so the next time We loop around a will be equal to six minus one Which is five so we update that here And at this point we've gotten to line six, which is the end of our loop Which means that we'll be moving back up to line three where we do the second loop iteration And so our second row here represents the second iteration And the current value of result is six So we want to update that there So again, we check our condition which says a is greater than zero and a is currently five So we're checking with this five is greater than zero. That's true So we enter our loop and we head to line four Line four updates the value of result by multiplying it by a And again at this point in time a is five So we want to multiply by five here and now result Is 30 we get to line five a is equal to a minus one. So a is equal to five minus one, which is four and Then we get to line six, which is the end of our loop So we head back up to line three and do our third iteration of the loop Where a is four And the last update we did to result it was equal to 30. So this time we go around the loop Result is 30 and we check our conditional again is four greater than zero. That's true Line four we've entered our loop line four says result is equal to result times a Well a is currently four So we're times in by four So now result is 120 Line five says a is equal to a minus one four minus one. We get three We get to the end of our loop on line six and we head back up to line three to check our condition It's three gate greater than zero. Yes I forgot to update result. It was 120 So line four says result is equal to result times a a is currently three And line five says a is equal to a minus one. So a is now equal to two We head back to the top we check our condition is two greater than zero It's true result was previously three sixty So now we had to line four Result is equal to result times a a is two and then a is equal to a minus one And I'll remember to update my result this time We head back to the top one is greater than zero. That's true So result is equal to result times a and a is equal to a minus one We head back to the top of our loop We check our condition zero is greater than zero and this is false. So we've finally Terminated our loop because our condition is false, which means we're going to stop now and then Which means we jump down to line seven and we do whatever is in the rest of our code and in this case It's print line result. So what will be printed here is the value of the result and that value is 720 because that was our last result value So that's how you trace a while loop But if you have a Variable that you're updating a condition and an update in Your while loop that can be easily translated into a for loop. So I'm going to show you how to Trace a for loop as well. So I've got this loop here. I've got my logic table here That's going to help me trace it and I've got three columns. I've got a my i variable I've got my condition i is less than or equal to 16 and then I have my result and I've given those values already with their initialized values So here it's very similar to the while loop except everything is in this loop header here. So Let's start tracing this one. We've got our first line Sets result to zero initially and then line to we've got i is equal to one So I've updated that we have our condition and the important thing to note here is that this Increment here this update that happens happens at the end of the loop So this isn't the first thing that you do You don't do this straight away as soon as you get to this ending bracket here only then do you update i? So that's one thing to keep in mind So let's start tracing We've got i is one we want to check our condition is one less than or equal to 16 That's true So we enter our loop we go to line three and we say result is equal to result plus i and i is currently one So we're plus in one and only now that we've gotten to line four Do we apply our increment which is i is equal to i times two so one times two we get two and At this point we loop back around to Line two and we check our condition, which is if i is less than or equal to 16 So i is currently two is two less than or equal to 16 is true Result was previously one And we had to line three result is equal to result plus i i is currently two, so we're plus in two and now result is three We get to the end of our loop and only now do we apply our increment now that we're at line four i is equal to i times two so two times two we get four And then we head back to the top and do our condition So it's four less than or equal to 16. That's true Result was previously three We go to line three, which says result is equal to result plus i i is currently four So we're adding four Now result is seven Get to the end and now we're at the end we can apply our update So i is equal to i times two four times two we get eight So eight is less than or equal to 16 is true again result was seven and Line three says that we're adding the value of i which is currently eight So now it's 15 We've gotten to the end. We're applying our update i is equal to i times two eight times two is 16 So 16 less than or equal to 16. That's true because 16 is equal to 16 So we enter our loop our result value was previously 15 And what are we doing inside our loop? Line three result is equal to result plus i so 15 plus 16 And we get 31 We get to the end i is equal to i times two So 16 times two we get 32 We check our condition 32 is less than or equal to 16. That's false And it's important when you're doing these questions to have this terminating Value here and this false value here just to show that you can recognize when a loop finishes when The condition is no longer true so that we'd skip over the loop We're finished and we'd go to line five and continue with the rest of our code which in this case is printing out result Which is 31 so that's how you do your for loop here I've got one last example, which is also in the notes When you have a multi variable Conditional loop We said initially that when we're creating a logic table. We want a Column for each variable in each condition. So here what we've got is we've got a Place for every variable to keep track of the variable We've got a column for each individual Boolean here because this is a Boolean and this is a Boolean and then together they also make a Boolean when you end them together So you want to do the left side the right side and then The Boolean of both of those together and then you've got your result, which is another variable here so if you've got a loop that you want to trace that has More than one variable more than one conditional then you'll want Need to add some more columns there So that is how you trace a single loop