How to show debugging statements?

Hello, I am studying a section of the book that briefly describes debugging. It does not explain how to add debugging statements properly however, and so I am a bit confused on how to show them. I didn't think it was to find errors but I could be wrong. This is the question and what I have resolved: (my alterations to the problem are the comments)


Add debugging statements to the following for loop to show the value of the loop control variable at the start of each repetition. Also, add debugging statements to show the value of sum at the end of each loop repetition.

1
2
3
4
5
6
7
8
     k = 1;
    sum = 0;                             //for loop begins with i = -n
    For (int i = -n; i < n-k; i++)       //each loop adds 1 to i
        sum += (i * i);

//if n is 1, i is -1. sum will be 1. i++ makes i=2, i < n-k is false to exit loop
//if n is 0, i is 0. sum will be 0. test statement false to exit loop
//if n is -1, i is 1. sum will be 1. 1 < n-k false to exit loop 


Thanks!
You know how to print values (to cout), don't you?
...show the value of the loop control variable at the start of each repetition


I'd read this to mean that you should put an output statement (e.g. std::cout) at the beginning of the iteration of the for loop to output the value of i (before line 4 is executed)

...show the value of sum at the end of each loop repetition.


Similar to above - but outputting the value of sum after line 4 is executed.
Yes, I didn't think the question was to add to the program itself, just debug statements?

EDIT:

k = 1;
sum = 0;
For (int i = -n; i < n-k; i++)
sum += (i * i);

All of the above are part of the question, only the comments are added. I did not write this code.
Last edited on

...show the value of the loop control variable at the start of each repetition


I'd read this to mean that you should put an output statement (e.g. std::cout) at the beginning of the iteration of the for loop to output the value of i (before line 4 is executed)

...show the value of sum at the end of each loop repetition.


Similar to above - but outputting the value of sum after line 4 is executed.


Oh! Excellent, thank you! I see now.
Topic archived. No new replies allowed.