Strange answer after n ++ execution

Why is the value of int d 25, not 26 after the execution of the following code fragment?

int n = 20; int d = n++ + 5; Console.WriteLine(d); 
+6
source share
6 answers

n++ is the "operator after the increment", which only increments the value after , its initial value was used in the surrounding expression.

Your code is equivalent to:

 int d = n + 5; n = n + 1; 

To increase a value to , its value will be used, use ++n , the pre-increment operator.

+10
source

Since you need to use ++n to use the increasing value in this expression.

See that in the expression tree it does not increment n , and then uses this value in the appendix because n++ returns n , but increments it for the next expression used.

However, ++n will actually return the added value of n for this expression.

Therefore, n++ + 5 gives 25 , while ++n + 5 gives 26 .

+3
source

n++ means the execution of the inscription after the operation, so the first d will be equal to n+5 , and then n will be raised.

+2
source

Because n++ first assign a value, and after the iteration is completed, it will increase, and the reason it gives 25

Hence,

 int d= n++ + 5; 

interpreted as

 int d = n + 5; 
+2
source

Due to using Postfix express

 int d = n++ + 5; 

where the compiler first assigns the value d, but in the following

 int d = ++n + 5; 

You will get d value 26

+1
source

++ : post increment statement.

The result of the post fi x ++ operator is the value of the operand. After the result is received, the value of the operand increases

Hence,

 int d= n++ + 5; 

interpreted as

 int d = n + 5; 

after performing the above interpretation. n increases by 1.

+1
source

Source: https://habr.com/ru/post/955981/


All Articles