Welcome back. I'm here with some way of the program or
tips on how to make it so that you can understand what's going on in your programs.
Here, I've got some code for doing
an accumulator pattern to add up all the numbers from one to 10.
But suppose you didn't recognize it as that.
You could use CodeLens if you have a small enough program like,
this one, but it's good to start learning how
to add some print statements that will print out intermediate results.
So, suppose I run this in Enterprise that I got 45 rather than 55. What should I do?
The answer is, I should start adding some print statements.
So, here I've added inside the for loop something that prints the number,
the current value of the iterator variable.
So, what's the value of num on this iteration.
So, now I find out that at zero,
then one, then two, then three, then four,
if I was thinking that range of 10 was going to give me the numbers from one to 10,
adding this print statement on line five will give me
a clue that actually it's giving me the numbers from zero to nine.
Now, suppose I'm still a little puzzled.
Maybe I don't really understand what lines six is doing with this tot plus equals num.
Here, I've added an extra print statement on line eight to print tot,
the value of the accumulator variable at the end of each iteration.
So, now we find that we get zero and zero.
So, this is num is zero and tot is zero,
and num and tot are both one,
and num is two and tot is three,
because we've added the previous total one
plus the new number two and we get three and so on.
So, now I can start to understand that line seven is taking the old value of total,
adding in the current value of num and writing that back into tot.
So, we get the old value of total was one,
the new value of num is two and we write it back into tot,
so that it's three, and on the next iteration will have three in tot.
Now, I had to do a lot of writing stuff
here to remind us of which variables were getting printed out.
So, when you're printing intermediate results,
sometimes it's helpful to print out a little text saying what it is that you're printing,
and that's what I've done in the next window.
So, I've got a print statement on line four saying,
"Hey, we are before the for loop."
Then, on line six,
you say we're doing a new iteration and the value of num is something.
You can see that with all of this diagnostics,
it's much easier to read the output.
So, instead of me having to write in num and tot,
we've got it all getting printed out already.
The value of num was zero and the value of tote was zero,
and they were both one,
then num was two and tot was three because we added the two and the one.
We have a value of tot is three and the value of num is three,
so we add those two together to get six,
six and four makes 10 and so on.
So, it's helpful to add print statements.
If you just add one,
you don't need to have all this diagnostic stuff
like saying what it is you're printing out?
But once you start to have a several print statements,
it's helpful if you include things like,
now you have done this,
so that when you go to look at it over here,
you know what you're seeing in the printout.
So, that's a little tip on printing intermediate results. I'll see you next time.