I am having trouble understanding the following code.
int count = 0; for (int i = 0; i < 3; i++){ count += (count++); System.out.println("count = " + count); System.out.println("i = " + i); }
I understand that the loop runs three times, pre-creating the following
count = count + count count = 1 + count
This means the following: count is initially 0:
count = 0 + 0 count = 1 + 0 = 1 count = 1 + 1 = 2 count = 1 + 2 = 3 count = 3 + 3 = 6 count = 6 + 1 = 7
The result is lower, and the number is counted as 0.
count = 0 i = 0 count = 0 i = 1 count = 0 i = 2
Can anyone explain this to me? Thanks
source share