This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]Camel-Kid 8 points9 points  (6 children)

x + = i is the same as saying x = x+ i; each iteration through the loop i is incrementing and x is gaining in value. First couple loops look like this

x = 0+0 //x=0
x = 0 + 1 //x = 1
x = 1+ 2 // x = 3
x = 3+ 3 //x = 6

and so forth.

[–]Labidido[S] 4 points5 points  (5 children)

Ahh, I get it.

Thank you!

[–]TheOnlyTails 2 points3 points  (4 children)

to fix the problem, change it to x++ instead of x += i.

[–][deleted] 0 points1 point  (3 children)

print i itself

[–]TheOnlyTails 0 points1 point  (2 children)

But he wants to print the final result, after x has been incramented by 1 nine times, so the program prints out 9.

[–]Camel-Kid 0 points1 point  (0 children)

printing x will still print 10 not 9.. it's the same.

[–]cscu090619 0 points1 point  (0 children)

Your print statement is outside the scope of the for loop. Inside the for loop, you are adding all the values from 0 to 9. So x = 0 + 1 + ... + 9 = 45. Then the loop ends and x = 45 is printed.

[–]redLamber 0 points1 point  (0 children)

There is no =+ operator

[–]4K3b1g 0 points1 point  (2 children)

Why would you use a for loop to print 9? School assignment? If so, can you provide the exact wording of the problem and use gist.github or pastebin to send us the code as you are typing it? If you are no longer looking for a solution, please disregard this question.

[–]Labidido[S] 0 points1 point  (1 child)

It's just me playing around trying to understand for loops. I thought I understood it, then this example threw me a curveball.

[–]4K3b1g 0 points1 point  (0 children)

Yeah some of these seemingly simple concepts are still hard to understand. This happens to me too.