11:07:43 Hello! So in this lesson we are going to learn about tracing. 11:07:48 Often times as programmers, we're actually working with someone else's code that needs to be modified. 11:07:55 And in order to modify it, we first have to understand what it does. 11:07:59 So this module we're going to spend a lot of time practicing tracing. 11:08:04 But I need to help you get started. How do you trace code? 11:08:09 So this very first problem has some loops in it, and we're going to see how to trace code. 11:08:15 So one of the things that we're gonna need to keep track of is our main memory. 11:08:21 So our variables. So if you see I'm gonna make a big box over here with main memory. 11:08:26 So, it's not the candy anymore. It's main memory and we're gonna see, we're gonna put information in main memory. 11:08:34 And we're gonna to watch it change through the process. 11:08:36 So that's our first step. Now, when actually looking at the code, the next thing that's important to do is to align the brackets right? 11:08:45 So we know. What is in what brackets. So there's our public class, and there's the closing bracket and when you're programming and using those nice comments that helps to know these brackets, but when we're tracing and we don't have those nice comments, it's 11:09:00 good to first see what is in each one of the brackets, so you can see I'm matching them up. 11:09:06 And so here's my opening bracket this next opening bracket and that matches. 11:09:11 With this closing bracket. 11:09:15 There we go, and in this case here's another opening bracket, and there's a close that matches. 11:09:23 And so here's an opening bracket. Then here is the close oops one doesn't seem to want to drop 1 s. 11:09:32 There. Okay? And. 11:09:36 There we go! And then the last one notice is for loop. 11:09:40 It doesn't have any brackets, but remember the rules, for if or else, or any of the looping structures, if the conditional statement is true, it executes one line of code. 11:09:52 So this for loop is gonna execute this line of code. 11:09:55 Okay, so this got a started. Now, the next thing I wanna know that there are some for loops in here. 11:10:03 And when you are flowcharting this pre test loop, you notice that the mode, the method of egress, where does this actually happen? 11:10:12 Well, this method of egress actually happens right here at the end of this bracket. 11:10:19 So I'm gonna note that. So here's another for loop. 11:10:21 So here is my M again. So I'm gonna note that the Moll happens in this case. 11:10:26 There's only one statement. So it happens right here, before I recheck the conditional statement. 11:10:30 Okay, so let's get started. So now that we have the code marked up for our brackets, let's begin at the beginning programming rule number one, the processes linear and so the very first thing we see is we're making some data. 11:10:44 Storage. So in main memory, I'm in a note over here that I haven't. 11:10:49 Hey? I haven't. B. I haven't see and in D, and now none of them have initial value. So I don't assign any values to them yet. 11:11:02 Okay, they're eventually going to get some values but they have none right now. 11:11:06 Oh, look at the very next step! So, as I walked through this linear process, a does get an initial value of one. 11:11:13 So a now is set to one. Now I have a while loop, and so we're checking. 11:11:19 Is a less than 5. Well, sure A is one we can see that main memory. 11:11:25 One is less than 5, so I execute these statements right? 11:11:28 They're in a curly bracket. So that means I'm gonna execute both of these. 11:11:33 And so I'm a system out print what's in a well, what's in a one? 11:11:37 Then what happens today, hey, is incremented, and if becomes 2? 11:11:43 And now I go back into my loop right so I go back to the top of the wire loop is 2 less than 5. 11:11:49 Yes, it is so. I execute the body again in the body, says, print a. 11:11:54 So what's an a well, 2 is in a right now now notice, this is a print, not a print line. 11:11:59 So they're staying right next to each other on the same line. 11:12:02 Now the next thing is increment. A again. So what's the new value in a a becomes 3? 11:12:10 Go back to the top of the loop. This 3 less than 5. 11:12:13 Yes, this is still true, and so I print the value. In a this time I'm printing out 3. 11:12:19 Sorry. There we go, printing out 3, and still not taking it. 11:12:28 Give me one more second here, fighting with my tablet. 11:12:32 Really fighting with it. Hang on! 11:12:39 We're back. 11:12:41 There we go, still not. 11:12:47 There, now we've got it. Okay? And so then what happens? 11:12:52 A plus, plus. And so a becomes 4 is 4 less than 5. 11:12:57 Yes, this is still true. So I'm going to print out 4. 11:13:00 Then what a becomes 5. So I set it to 5 is 5 less than 5. 11:13:09 No, this is false, like 5 on the loop, and I execute the next line of code. 11:13:12 Well, the next line of code is just a system. All print line will remember. 11:13:16 My cursor was sitting here next to the floor. So print line, with no argument just brings my cursor down to the next line so little dot there to remind me that I'm now on the next line. 11:13:25 Now I start this for loop, and so what do I see first? 11:13:29 For B equals. 5. So B. Now has an initial value of 5 great. 11:13:36 And now I check is 5 greater than or equal to one. 11:13:40 Well, yes, this is true. So I'm gonna go into the body of the loop in the body of the loop, says, Print out B, and a space. 11:13:47 So B is 5 and a space so I'm just gonna note, a little dot here to remind myself there'll be a space between these. 11:13:55 And then what happens? Remember that mode, that method of egress, actually happens. 11:14:01 It's the last task before we go back to the top of the loop. 11:14:04 So B is now going to equal B divided by 2. Well, B is an integer, and so I'm going to do some little scratchwork here. 11:14:11 This is arithmetic and logic registers down here, and they're going to divide by by 2 and 2 goes into 5. 11:14:17 How many times. Well, 2 times. Okay, we have a remainder. Right? 11:14:21 But we don't care, because it's truncated. 11:14:25 We're doing integer division here. So what happens is 2 is now stored in B. 11:14:30 We lost that fractional portion, because this is an integer data type and we did integer division. 11:14:36 So B now equals 2. Now is 2 greater than or equal to one. 11:14:45 Well, yes, that's true, so I'm going to print out B, which is now 2. 11:14:48 And it's based over there and then I have a new space right? 11:14:52 So now, my cursor, sitting here and again, what am I gonna do? 11:14:56 I'm gonna divide by by 2. So B's new value is 2. 11:15:01 And so how many times is to go into 2? Well, it goes in one time, goes in exactly, but we don't really care, because this is the only portion we keep cause we're going into your division again, and so B's new value is one okay, and so now is one greater than equal to one well, yeah, one. 11:15:19 Is equal to one. So this is so. I end up printing it out. 11:15:23 I move over, and now I have to do the math again. 11:15:25 And so this time my math is one divided by 2. Well, how many times is 2 going to one? Well, it goes in there 0 times. 11:15:33 Doesn't it? And we do have a remainder of one. 11:15:35 But we don't care. We're only keeping the whole portion, and 2 doesn't go into one ever. 11:15:40 Okay. So I knew that B is 0. And so now we go and try. 11:15:46 Our conditional statement is 0 greater than or equal to one. 11:15:48 Well, it's false, and we fall out. So what happens? 11:15:51 It's another print line. So when my cursor was sitting here now, my cursor was sitting here. Now, my cursor sitting down here on the next line. 11:15:56 Oh, what happens next? We have a new value for a A is now assigned 0. 11:16:02 So a is no longer 5. It is now 0, and what's the value? 11:16:07 In Dd. Is set to 9. So d. Is now been initialized to 9. 11:16:13 And now we're going into a post test loop. 11:16:15 The d-while, which means we're going to do this body at least once and and so the very first thing we do is we system out, print, line the value in a well, what's in a at this moment? 11:16:26 It is 0. Now this was a print line so notice, I'm gonna make a little mark on my paper to say I am now on that next line. 11:16:33 And now I enter for loop, and I'm entering it from the top every time I enter a for loop from the top. 11:16:40 That initial statement is executed. So as long as I'm entering from the top, this is executed. 11:16:46 Sorry. Got my cursor. Doesn't want to go, so I set what C is equal to a so what's the value in C. 11:16:54 Well, it's equal to a and a right now is 0. 11:16:55 So C is also equal to 0. And then I check. Okay, so now I go to the conditional statement, is C greater than or equal to 0. Well, it's true. 11:17:03 It's equal to 0. So I'm in a system outlined this time. Look at the double quotes. 11:17:08 I'm printing out the literal B. I'm printing out the literal B. I'm not printing out the variable B, I'm literally printing out the string with the single character B in it. 11:17:17 Okay. Now, this is a for loop. So remember that C, minus minus. 11:17:22 When does that happen after this? It's a very last statement. 11:17:25 In the for loop. So the value in C is 0 and I minus one. 11:17:28 It. So the new value in C is negative one. Okay? And now I go back and I test is negative. 11:17:36 One greater than or equal to 0. Well, that's false, and I fall out of the loop. 11:17:39 So I execute the next line of code, which is what a system out print line oops. 11:17:45 I forgot to remind you. Your cursor was sitting here and now we have a print lines to your cursor is moved down to the next line. 11:17:51 Okay, then, what a is updated. So the new value in a is whatever's in a add one to it. 11:17:58 So a has 0, and so now A is going to have what one in it, and D is getting a new value as well. 11:18:06 D equals whatever's in d right now subtract 2, 4. 11:18:09 So d is 9, and so minus 2, so d becomes 7, and then I check my wild condition. 11:18:17 Remember, because this is a post test loop. So I did the body, and then I check so a is equal to one is one less than 7. 11:18:24 Yes, this is true, which means what I go back into the due. 11:18:28 And so what's the very first thing that I do? 11:18:30 I print out the value in a well, what's the value in a? 11:18:36 It's one. So I print out one. It was a print line, so I'm moving down to the next line. 11:18:40 Please let me get my eraser for a second. 11:18:43 Let's move these stats away, so we don't think that's part of the answer right? 11:18:46 No's go back to the numbers here. We know we're down on this next line now. 11:18:51 Okay, so I printed out a. And now remember, I'm entering the for loop from the top, which means I will reset this in this starting value. 11:19:00 So, C now equals the current value in a. So what's the current value in a it's one. 11:19:06 So it's no longer negative one. It is one. And then what is C greater than or equal to 0? 11:19:12 Yes, one is greater than 0. So I'll execute the body. 11:19:16 And so what am I gonna do? I'm going to print out the letter B. 11:19:20 Great. Okay? And I'm staying on that line. Remember, that's a print. 11:19:26 And so the last thing I do is I decrement. 11:19:29 See. So I see minus minus, so, C becomes 0. And now I go back into the for loop, and I check is C greater than or equal to 0. 11:19:38 Well, 0 is greater than or equal to 0. So I go ahead, and I execute that print again, and I print another baby. 11:19:45 Okay, then, what c, minus, minus. So what happens to C, now, it's negative. One. 11:19:51 Again. Go back into the for loop is negative, one greater than 0. 11:19:54 No, it is not so. I fall out, and I print. I do a print line. 11:20:00 So that means my cursors down on this next line. 11:20:02 Now, I'm gonna reinitialize a. So what's what happens here? 11:20:06 A is equal to whatever's in a plus one, so a becomes one, plus one is 2, and then D becomes what d is currently 7 minus 2. 11:20:18 So the new value in D is 5, and then I check is a let's get over. 11:20:27 Here is a less than D, so is to less than 5. 11:20:33 Yes, this is true, so I go back into this due. Okay. 11:20:36 So I wanted to make, cursor out here. So where I can see it. 11:20:39 And so I print out what? 11:22:35 Did you figure it out at this point? The value in a is 2. 11:22:41 So I print that out. 11:22:43 And then what happens again? I'm going into the for loop from the top. 11:22:48 So C is assigned the value in A, which is 2 so we're gonna change C to be 2 here, and then what happens is 2 greater than or equal to 0. 11:22:59 This is true, so I print out a B, and then one happen. See minus minus. 11:23:06 So C. Becomes one. I'm gonna run out of room in the box. 11:23:10 You can see that already, and now we check is one greater than or equal to 0. It's true. 11:23:15 We print out A, B, and then we minus minus and C becomes 0, 0 is greater than or equal to to 0. 11:23:26 So we print out that B, and then we see minus minus. 11:23:30 And so we're now at what? Now get it? One. 11:23:32 And negative. One is not greater than or equal to 0. So I fall out of that for loop, and then what happens? 11:23:40 So I have a print line. So I'm putting my cursor down on that next line. 11:23:45 And now a gets a new value, and so a becomes. 11:23:54 3, right? Because it's 2 plus one. It's 3. 11:23:58 I'm running out of space over on this 1, 2, and then D becomes well, what's the value, indeed, it's 5, 5, minus 2 becomes 3, and then we check is a less than D, so it's 3 less than 3. 11:24:13 No, it's false, and we fall out, and the program is over. 11:24:17 So when you're tracing the things you wanna do is build a box for main memory. 11:24:22 Put the variables in there, note them as they change throughout the process. 11:24:27 The output window should only display the items that are being output. 11:24:31 Make sure to keep track versus print versus print line, whether you're moving the cursor down to the next line and empty print line does just that. 11:24:40 It just brings you down to the next line, and also, if you need to do the math, that's your arithmetic and logic unit using those registers to do the math computation always be careful if we're doing integer division. 11:24:51 We lose that fractional portion, so only if one of the values in the division statement is a float or a double, will we not be doing integer division? 11:25:03 Otherwise, if both numbers are integers without a floating point portion, we will be doing that integer division. 11:25:08 I hope you found this very useful. I will note that in some of the examples you have some branching, if else, and so, as soon as you check the if condition, if if condition is true, cross off any matching else, and do not execute that code, and remember carefully, the rules, are for loops and for 11:25:29 branching. If the statement is true, it executes only one line of code. 11:25:35 Remember that it doesn't have any more than that unless currently braces are being used, in which case it's treated as a unit or a compound statement. 11:25:44 That's also true for the Ls. If the if condition is false and there is a matching, else it's only gonna execute one line of code, and then we go back to programming Rule number one, the process is linear as we walk through. 11:26:00 The I hope you found this lesson useful. Have a great day.