Why is the value of int d 25 and not 26 after executing the following code snippet?
int n = 20;
int d = n++ + 5;
Console.WriteLine(d);
n++
is the "post-increment operator", which only increments the value after its initial value has been used in the surrounding expression.
Your code is equivalent to:
int d = n + 5;
n = n + 1;
To increment the value before its value gets used, use ++n
, the pre-increment operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With