4

I'm having a hard time understanding why the following loop prints 0 on each iteration.

for (int i = 0, j = 0; i < 10; i++)
{
    Console.WriteLine(j += j++);
}

Shouldn't the value of j increase after each iteration? If not can you please explain?

After positive feed back from @Jon Skeet I stepped through the disassembly of the statement and was able how the code was behaving at a low level. I have added the disassembly with my comments.

Thanks!!!

    54:                 Console.WriteLine(j += j++);
0000004f  mov         eax,dword ptr [ebp-40h]   /* [ebp-40h] == 0 move to eax */
00000052  mov         dword ptr [ebp-48h],eax   /* eax == 0 move to [ebp-48h] */
00000055  mov         eax,dword ptr [ebp-40h]   /* [ebp-40h] move to eax == 0 */
00000058  mov         dword ptr [ebp-4Ch],eax   /* eax move to [ebp-4Ch] == 0 */
0000005b  inc         dword ptr [ebp-40h]       /* increment [ebp-40h]== 1*/
0000005e  mov         eax,dword ptr [ebp-48h]   /* [ebp-48h] move to eax == 0 */
00000061  add         eax,dword ptr [ebp-4Ch]   /* (eax == 0 + [ebp-4Ch]) eax == 0 */
00000064  mov         dword ptr [ebp-40h],eax   /* eax == 0 move to [ebp-40h] */
00000067  mov         ecx,dword ptr [ebp-40h]   /* [ebp-40h] move to ecx == 0 */
0000006a  call        71DF1E00                  /* System.Console.WriteLine */
0000006f  nop 
    55:             }
dcrearer
  • 1,972
  • 4
  • 24
  • 48
  • @Rabid Penguin it not a matter of incrementing j... as I stated this is a exercise from Robert Sedgewick / Kevin Wayne intro programming book. I understand how to pre and post increment. My objective is to understand why the loop printed 0 on each iteration. – dcrearer Dec 26 '16 at 21:44

1 Answers1

14

Shouldn't the value of j increase after each iteration?

Nope. Your loop body is somewhat equivalent to this:

int tmp1 = j; // Evaluate LHS of +=
int tmp2 = j; // Result of j++ is the value *before* the increment
j++;
j = tmp1 + tmp2; // This is j += j++, basically
Console.WriteLine(j);

So basically, you're doubling j on each iteration... but j is 0 to start with, so it stays as 0. If you want to just increment j on each iteration, just use j++... but ideally do it as a statement on its own, rather than using it as an expression within a bigger statement.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • ok... at what stage is the post increment executed? I'm actually doing an exercise from a introductory programming book.. no its not homework :). It was my impression that the post increment would increment the value through each iteration. – dcrearer Dec 26 '16 at 21:30
  • 1
    @dcrearer: It's incremented as part of evaluating the `j++` expression. Basically, `j++` evaluates as "increment `j`, but the result is the original value". The `j++` *does* increment `j++`, but the effect is fleeting as you're then assigning a new value to `j` due to the `j +=` part. If you *just* had `Console.WriteLine(j++);` then it would work, although I'd still suggest moving the `j++` to a separate statement on its own. – Jon Skeet Dec 26 '16 at 21:31
  • Thanks for the feedback. – dcrearer Dec 26 '16 at 21:34
  • @dcrearer: If that is still not clear, read http://stackoverflow.com/a/3346729/88656. Keep in mind that almost every answer about this topic gets it wrong at some point. Jon of course knows what he is talking about. – Eric Lippert Dec 26 '16 at 21:50