 Hello. In this video, I wanted to show you how to run code by hand without having to type into the computer and see what happens. This tends to be a really important skill not only for exams in a college course, but also for job interviews or being able to discuss code with other computer scientists. It helps troubleshoot issues. It helps you design test cases. So it's just a really important skill to be able to look at a piece of code like this and say exactly what happens. Not just what does the code output, but how do the variables change over time? So I'm going to show you how to do this on paper and it's a skill that you'll want to practice for the next exam. Before doing so though, let's just take a look at what the code does overall to understand the structure. So you can see here I have a class that has two methods, main and get some. And there's a loop here in the main method that's going to call the get some method in either case of an if statement, but the parameters are different or rather the argument values, right? Now get some is pretty straightforward. All it does is prints out what the two values are and then one and two adds them up and returns the sum. So with that introduction, let's go to paper and see what happens. The basic idea of tracing code by hand is to draw a table of all the variables that you have and see how the values of those variables change over time. So for one, I have this variable I that's part of my loop. And as this I loop runs, I'm going to be changing the values of D1 potentially D2 and total. So I just take all the variables that I have in my method here and I'm going to make a nifty little table, again, just to keep track of those values over time. So the I variable you can see is going to loop from zero to the value three inclusive. So I'm just going to go ahead and fill in the table like that. D1 starts out as 1.5, D2 starts out as 3.1, total starts out as 0.0. Now again, you want to fill out this table at the beginning of each loop in the four statement, right? So when I equals zero, these are the initial values of the other three variables. So what's going to happen here is as the code executes, it's going to test if I is less than or equal to D1 and you can see in the table that that will be true. So this branch will be executed total equals total plus get sum. And to get some method, I'm going to draw a similar table. So here I've got num1, num2, and sum. Those are the three variables declared in that method. Now this method is not looping, but the main method is looping that's calling this method. So I'm just going to draw a similar table. So each time the method is called, I can just see on one row of this table what the result of that method call is. So the first time this method is called, D1 and D2 are passed into num1, num2. So I've got the values 1.5, 3.1, and their sum is going to be 4.6. And that sum is then returned back up here. So at this point, I've got 4.6 in this value. Total equals total plus 4.6. So the next time we loop around when I is 1, the total is 4.6 at the beginning of that iteration. Now notice here just looking at the code that the values D1 and D2 never change. They're copied into these parameters, but they're never assigned in this method. So I'm just going to draw little ditto marks here to note the fact that those values are constant throughout the execution of this loop. What's really going to happen is at each step of the loop, the total value is going to change. So let's see what happens when I equals 1. If I is less than or equal to D1, yep, that's true, 1 is less than 1.5. So we're going to go into this method again, passing the values D1 and D2. Well, that's exactly what happened last time. But now notice that when we return 4.6, I'm adding that to total, which is already 4.6. So the next value of total is going to be 9.2, right? 4.6 plus 4.6. Loops around, I is now 2, and 2 is not less than or equal to 1.5. So we're going to be in this branch now, the else block. And notice how the arguments forget some have changed, right? So now I'm passing total and D1 is the arguments. So num1 is going to be whatever total is 9.2. And num2 is going to be D1, which is 1.5. And of course, if you add up those together, 5 and 2 is 7, carry the 1. And we've got 9. Oh, you don't carry anything, it's just 10.7. Okay. But anyway, you know, usually the numbers are small enough that you can do them in your head. And really what we're looking at here is can you understand the flow of execution through this code, right? So now I'm going to take this 10.7, go ahead and erase these other things. And it's okay to make notes on the code as you're trying to make sense of it, right? So this 10.7 gets added to my current total, which is 9.2. So that's going to make this 19.9. Okay, so my answers are correct here so far. And now, again, this is the beginning of the loop. Maybe I'll just write beginning up here. So we remember that we're actually not done with the program yet. We now have to run this row where i equals 3. Again, 3 is not less than or equal to 1.5. So this branch of the loop is going to run. It's going to pass the current total, which is 19.9. And the value D1, which is 1.5. 19.9 and 1.5 are, okay, so this time we do have to carry 5 and 9 is 14. And then 20 and 1 gives me 21. So that will be the sum that's then returned up here, 21.4. And I'm going to add that to my current total, which is 19.9. So at the end of the program, my actual value is going to be 41.3, right? So the way I did that quickly is notice how 19.9 is just 0.1 below 20. So I'm going to take 0.1 away from here, 21.3, and add that to 20. So that's 41.3 total. But anyway, the whole point of this is can you draw a table that allows you to track over time how these variables are changing and then noticing which values here are used for the arguments that are assigned to the parameters in this other method. So if you compare what we have on the sheet we just drew with what actually happens when I hit the run button, you can see over time the two values that were added each time they get some method was called 1.5 and 3.1, 1.5, 3.1 and so forth. And then the very last line of output of the program after this entire for loop completes is 41.3. I'd like to just review one quick way to do the same type of tracing on the computer using the debugger. So for example, I can go ahead and toggle a break point on any line. I might as well just pick the first line of the program. Turn on debug mode by going into the menu here and then pushing run. Now this time when the program runs, it pauses on the first break point. So it's waiting for me right now to actually say what to do next. And one thing that's kind of handy is hidden away here on the left side of the screen you can open up the other half of the debugger window. So let me see if I can't resize these windows a little bit better. So over here under watches you can create variables that you want to watch. So for example, I want to watch the variable i. It's not found because I haven't yet declared it. And I also want to watch the variable total. So now what's kind of nice, let me move my video over here, is I can go ahead and step through this code one line at a time. So I can hit step over, step over, step over. So now that total has actually, this line of code has executed, you can see that that value is zero. And I'm about to run this first line of the four statements. So when I step over that, i is now zero. And there's a difference between step over and step into. So for example, if I hit step over, it's going to run that whole line of code as one step. So it actually went ahead and called get some with d1 and d2. You can see now the total is 4.6. And when I step over the four statement again, now i is one. I'm ready to go through the second iteration of the loop. In contrast, if I say step into, that will go into any methods that are invoked on that line of code, right? So now I can step through the behavior of get some. Of course, i and total are not found anymore because they're not part of this method stack frame. So let me go ahead and step over these lines of code and see what happens. Of course, now when I return for that method back to this line, you can see the value was 1 and 4.6. But when I step over that line, it now changes the value to 9.2, which was get some plus the previous total. You can also press the automatic trace button. And what that does basically is step into, step into, step into, with about a one second delay between each one. So at this point you can just sit back and watch your program one run one line at a time to understand the order of execution. So finally the four loop ends and it prints the final output statement and your program terminates. The only disadvantage here is that you have a whole bunch of extra output in both interactions and console that the debugger itself is printing out for you to understand all of your watches and anything else that you have running. So if you just wanted to know what the program does, you can go ahead and turn off debug mode, push the run button, and then you'll see only the output of the program.