# Why the result of this loop is 21? [closed]

Anyone can give me a full example of this code? Why the result total is 21 (ie the sum of current) if I print total???

``````end=6
total = 0
current = 1
while current <= end:
total += current
current += 1

print total
``````
-

## closed as unclear what you're asking by progo, Daniel Roseman, FallenAngel, Jon Clements♦, Srinivas Reddy ThatiparthyOct 24 '13 at 10:59

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

Why do you think the result wouldn't be 21? –  DSM Oct 24 '13 at 10:48
Because I think to find the sum I need to sum the results of total and not of current. Can you give me a an explanation? –  Overnet Oct 24 '13 at 10:52
@Overnet what do you think `total += current` is doing? –  Jon Clements Oct 24 '13 at 10:52
But summing the partial totals would give 1 + (1+2) + (1+2+3) + (1+2+3+4) + (1+2+3+4+5) + (1+2+3+4+5+6). For all I know that's what you really want, but it's something different. –  DSM Oct 24 '13 at 10:54
Thanks. I know. To find the result I need to sum the current because total += current ? It's true? –  Overnet Oct 24 '13 at 10:59

Because `1+2+3+4+5+6` is `21`. Why is that mysterious?

-
Yes, I know that. But I don't understand why I sum the results of current and not of total. –  Overnet Oct 24 '13 at 10:54
Thanks. I know. To find the result I need to sum the current because total += current ? It's true? –  Overnet Oct 24 '13 at 11:11
@Overnet: `total += current` is equivalent (in this context) to `total = total + current`, So in each iteration, you're adding the value of `current` to `total`. –  Tim Pietzcker Oct 24 '13 at 11:18
Thank you very much:) –  Overnet Oct 24 '13 at 14:25

You can usually get to the bottom of this sort of thing by introducing some basic debugging. I have added a print inside the loop of your code so that you can see what is happening after each iteration:

``````end=6
total = 0
current = 1
while current <= end:
total += current
current += 1
print "total: ", total, "\tcurrent: ", current

print total
``````

The output is:

``````total:  1   current:  2
total:  3   current:  3
total:  6   current:  4
total:  10  current:  5
total:  15  current:  6
total:  21  current:  7
21
``````

To summaries what is happening here, total and current are initialised to 0 and 1 respectively, on the first loop total is set using `total += current` (which is equivalent to `total = total + current`) i.e. total = 0+1, then current is incremented by 1 to 2 so after the first loop they are 1 & 2 respectively.

On the second loop `total += current` would be evaluated as total = 1+2 (the values as of the end of the previous loop) and so on.

-
Thanks. I know. To find the result I need to sum the current because total += current ? It's true? –  Overnet Oct 24 '13 at 11:12
@Overnet Sorry, I am not completely clear what you are asking here, I have updated my answer with a further explanation which will hopefully clear things up. Let me know if there is still any confusion. –  ChrisProsser Oct 24 '13 at 11:28

What have you expect? 1 + 2 + 3 + 4 + 5 + 6 = 21

here are are the start values and the values of each loop

``````total   0 -> 1 -> 3 -> 6 -> 10 -> 15 -> 21
current 1 -> 2 -> 3 -> 4 ->  5 ->  6 ->  7
``````
-
Yes I know that. But the sum of this result is because there is write total += current? –  Overnet Oct 24 '13 at 11:09
"total += current" is the same as "total = total + current". I hope that helps you –  Jan Meisel Oct 24 '13 at 11:52
Yes it's this :) –  Overnet Oct 24 '13 at 14:26
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}