1 00:00:00,470 --> 00:00:04,560 Welcome back. I'm here with some way of the program or 2 00:00:04,560 --> 00:00:09,720 tips on how to make it so that you can understand what's going on in your programs. 3 00:00:09,720 --> 00:00:12,075 Here, I've got some code for doing 4 00:00:12,075 --> 00:00:15,705 an accumulator pattern to add up all the numbers from one to 10. 5 00:00:15,705 --> 00:00:18,465 But suppose you didn't recognize it as that. 6 00:00:18,465 --> 00:00:24,000 You could use CodeLens if you have a small enough program like, 7 00:00:24,000 --> 00:00:26,220 this one, but it's good to start learning how 8 00:00:26,220 --> 00:00:31,260 to add some print statements that will print out intermediate results. 9 00:00:31,260 --> 00:00:42,000 So, suppose I run this in Enterprise that I got 45 rather than 55. What should I do? 10 00:00:42,000 --> 00:00:45,380 The answer is, I should start adding some print statements. 11 00:00:45,380 --> 00:00:51,635 So, here I've added inside the for loop something that prints the number, 12 00:00:51,635 --> 00:00:55,470 the current value of the iterator variable. 13 00:00:55,470 --> 00:00:58,860 So, what's the value of num on this iteration. 14 00:00:58,860 --> 00:01:01,590 So, now I find out that at zero, 15 00:01:01,590 --> 00:01:03,510 then one, then two, then three, then four, 16 00:01:03,510 --> 00:01:09,265 if I was thinking that range of 10 was going to give me the numbers from one to 10, 17 00:01:09,265 --> 00:01:12,860 adding this print statement on line five will give me 18 00:01:12,860 --> 00:01:17,070 a clue that actually it's giving me the numbers from zero to nine. 19 00:01:17,410 --> 00:01:20,960 Now, suppose I'm still a little puzzled. 20 00:01:20,960 --> 00:01:26,100 Maybe I don't really understand what lines six is doing with this tot plus equals num. 21 00:01:26,440 --> 00:01:32,965 Here, I've added an extra print statement on line eight to print tot, 22 00:01:32,965 --> 00:01:37,685 the value of the accumulator variable at the end of each iteration. 23 00:01:37,685 --> 00:01:42,660 So, now we find that we get zero and zero. 24 00:01:45,280 --> 00:01:51,990 So, this is num is zero and tot is zero, 25 00:01:51,990 --> 00:01:55,560 and num and tot are both one, 26 00:01:55,560 --> 00:02:00,120 and num is two and tot is three, 27 00:02:00,120 --> 00:02:03,395 because we've added the previous total one 28 00:02:03,395 --> 00:02:08,435 plus the new number two and we get three and so on. 29 00:02:08,435 --> 00:02:15,055 So, now I can start to understand that line seven is taking the old value of total, 30 00:02:15,055 --> 00:02:21,170 adding in the current value of num and writing that back into tot. 31 00:02:21,170 --> 00:02:24,710 So, we get the old value of total was one, 32 00:02:24,710 --> 00:02:30,020 the new value of num is two and we write it back into tot, 33 00:02:30,020 --> 00:02:35,195 so that it's three, and on the next iteration will have three in tot. 34 00:02:35,195 --> 00:02:38,600 Now, I had to do a lot of writing stuff 35 00:02:38,600 --> 00:02:41,660 here to remind us of which variables were getting printed out. 36 00:02:41,660 --> 00:02:43,640 So, when you're printing intermediate results, 37 00:02:43,640 --> 00:02:50,315 sometimes it's helpful to print out a little text saying what it is that you're printing, 38 00:02:50,315 --> 00:02:55,560 and that's what I've done in the next window. 39 00:02:56,660 --> 00:03:00,260 So, I've got a print statement on line four saying, 40 00:03:00,260 --> 00:03:02,465 "Hey, we are before the for loop." 41 00:03:02,465 --> 00:03:04,865 Then, on line six, 42 00:03:04,865 --> 00:03:10,020 you say we're doing a new iteration and the value of num is something. 43 00:03:10,020 --> 00:03:13,470 You can see that with all of this diagnostics, 44 00:03:13,470 --> 00:03:16,450 it's much easier to read the output. 45 00:03:16,520 --> 00:03:19,710 So, instead of me having to write in num and tot, 46 00:03:19,710 --> 00:03:22,390 we've got it all getting printed out already. 47 00:03:22,390 --> 00:03:25,360 The value of num was zero and the value of tote was zero, 48 00:03:25,360 --> 00:03:27,310 and they were both one, 49 00:03:27,310 --> 00:03:35,770 then num was two and tot was three because we added the two and the one. 50 00:03:35,770 --> 00:03:40,780 We have a value of tot is three and the value of num is three, 51 00:03:40,780 --> 00:03:42,955 so we add those two together to get six, 52 00:03:42,955 --> 00:03:45,530 six and four makes 10 and so on. 53 00:03:45,530 --> 00:03:48,150 So, it's helpful to add print statements. 54 00:03:48,150 --> 00:03:49,350 If you just add one, 55 00:03:49,350 --> 00:03:51,380 you don't need to have all this diagnostic stuff 56 00:03:51,380 --> 00:03:54,005 like saying what it is you're printing out? 57 00:03:54,005 --> 00:03:57,679 But once you start to have a several print statements, 58 00:03:57,679 --> 00:04:01,030 it's helpful if you include things like, 59 00:04:01,540 --> 00:04:04,180 now you have done this, 60 00:04:04,180 --> 00:04:06,860 so that when you go to look at it over here, 61 00:04:06,860 --> 00:04:09,530 you know what you're seeing in the printout. 62 00:04:09,530 --> 00:04:17,100 So, that's a little tip on printing intermediate results. I'll see you next time.