Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between X = X++; vs X++;?

Tags:

c#

People also ask

Is ++ x and x ++ the same?

The only difference between the two is their return value. The former increments ( ++ ) first, then returns the value of x , thus ++x .

Is there a difference between x x and x ∈ X?

x=x means x is the same thing as itself. That is always true. {insects}={insects}. x∈x means x is a set and x is the member of the set.

What is the difference between ++ x and x ++ explain your answer?

What is the difference between x++ and ++x? X++ is post increment and ++x is pre increment. X++ value is incremented after value assign or printed.

What is difference between x ++ and X 1?

x++ is a const expression that modifies the value of x (It increases it by 1 ). If you reference x++ , the expression will return the value of x before it is incremented. The expression ++x will return the value of x after it is incremented. x + 1 however, is an expression that represents the value of x + 1 .


X++ will increment the value, but then return its old value.

So in this case:

static void Main(string[] args)
{
    int x = 10;
    x = x++;
    Console.WriteLine(x);
}

You have X at 11 just for a moment, then it gets back to 10 because 10 is the return value of (x++).

You could instead do this for the same result:

static int plusplus(ref int x)
{
  int xOld = x;
  x++;
  return xOld;
}

static void Main(string[] args)
{
    int x = 10;
    x = plusplus(x);
    Console.WriteLine(x);
}

It is also worth mentioning that you would have your expected result of 11 if you would have done:

static void Main(string[] args)
{
    int x = 10;
    x = ++x;
    Console.WriteLine(x);
}

In the assignment x = x++ you first extract the old value of x to use in evaluating the right-hand side expression, in this case 'x'; then, you increment x by 1. Last, you assign the results of the expression evaluation (10) to x via the assignment statement.

Perhaps an equivalent code would make the predicament clear:

var tmp = x;
x++;
x = tmp;

This is the equivalent of your x = x++ code in C#.


The behaviour of x++ is to increment x but return the value before the increment. Its called a post increment for this reason.

So x = x++; simply put will

1. return the value, then

2. increment x, then

3. assign the original value(returned in step 1) of x to x.


x = 10
x = ++x 

x would end up equalling 11.


x++;

does the following:

int returnValue = x;
x = x+1;
return returnValue;

As you can see, the original value is saved, x is incremented, and then the original value is returned.

What this ends up doing is saving the value 10 somewhere, setting x equal to 11, and then returning 10, which causes x to be set back to 10. Note that x does actually become 11 for a few cycles (assuming no compiler optimization).


You can think of it like this:

int x = 10;

X is a container, and contains a value, 10.

x = x++;

This can be broken down to:

1) increment the value contained in x 
    now x contains 11

2) return the value that was contained in x before it was incremented
    that is 10

3) assign that value to x
    now, x contains 10

Now, print the value contained in x

Console.WriteLine(x);

And, unsurprisingly, it prints out 10.