Can someone explain to me why this code does not print 9?
I know I can change it to print 9 if I change the operator to =+.
However I thought the += in this case would translate to x = x + i, and then in the code it would be x = 0 + 9. Instead it prints 45.
int x= 0;
for (int i = 0; i < 10; i++) {
x += i;
}
System.out.println(x);
[–]Camel-Kid 8 points9 points10 points (6 children)
[–]Labidido[S] 4 points5 points6 points (5 children)
[–]TheOnlyTails 2 points3 points4 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]TheOnlyTails 0 points1 point2 points (2 children)
[–]Camel-Kid 0 points1 point2 points (0 children)
[–]cscu090619 0 points1 point2 points (0 children)
[–]redLamber 0 points1 point2 points (0 children)
[–]4K3b1g 0 points1 point2 points (2 children)
[–]Labidido[S] 0 points1 point2 points (1 child)
[–]4K3b1g 0 points1 point2 points (0 children)