1

This is a simplified version of something that came up in my code and I couldn't explain.

The code keeps writing 0 in the console output and never ends.

int test = 0;
while (test < 100)
{
    test = test % 2 == 0 ? test++ : test + 2;
    Console.WriteLine(test);
}

I understand that it's not necessary to assign anything to your variable when you use the ++ operator but I don't see how it would change the outcome either.

In my mind what should happen in the first loop is the following: The condition is met and test++ is executed but only after assigning its original value to test, since it's test++ and not ++test. So test is assigned a 0 and afterwards its value is incremented to 1.

But that's not what happens, it just stays 0 so what is actually happening here?

nicscan3
  • 13
  • 2

1 Answers1

2

test++ is not the same as test + 1. It returns the value of test from before the assignment while also assigning a new value to the variable at that point in time that is one more than the current value. Thus test = test++ adds one to test via the test++ and then assigns it back to the original value through the explicit assignment.

Since you're assigning test explicitly, you should just use test + 1 to return the value of test plus one without mutating it.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • You haven't really answered the question here. He/She knows that first it gets the value before the assignment. However, after that we are "assigning a new value" to that same variable so why does it not increment? – Yossi Sternlicht Jun 15 '22 at 16:11
  • 1
    @JoeStarbright It *does* increment, as I said in the answer. It increments the value, and then assigns a new value to it, with the new value being the value of the variable before the increment. – Servy Jun 15 '22 at 16:12
  • @JoeStarbright You should create your own answer. – Lee Taylor Jun 15 '22 at 16:13
  • 1
    @LeeTaylor I did (like the others), but then deleted it as I dont think its fully explained – Yossi Sternlicht Jun 15 '22 at 16:14
  • @JoeStarbright What about it do you think is not explained? – Servy Jun 15 '22 at 16:15
  • 1
    We can simplify it to `test = test++;` which is the equivalent of `var temp = test; test = test + 1; test = temp;` which hopefully makes it more obvious why the value is ultimately unchanged. – Matthew Watson Jun 15 '22 at 16:15
  • @Servy so are you saying these are the steps of the compiler in this order (when doing test++) 1. Take test and increment the value now. 2. Return the test value as it was before the increment – Yossi Sternlicht Jun 15 '22 at 16:16
  • 2
    @JoeStarbright Yes, that's what the `++` postfix operator does. The prefix operator returns the value after the increment, rather than before. – Servy Jun 15 '22 at 16:17
  • 2
    All this discussion had already happened in the past ... so save your time :) https://stackoverflow.com/a/33784071/477420. TL;DR: = has lower priority than ++ and hence all ++ finishes before =. – Alexei Levenkov Jun 15 '22 at 16:17
  • @AlexeiLevenkov Thanks. Not familiar yet with the whole SO off hand : ) – Yossi Sternlicht Jun 15 '22 at 16:19