Forums Register Login

Postfixed increment operator in expressions

+Pie Number of slices to send: Send
Hi,
I have tried the following expression:

the result surprises me. In fact I expected 21, it seems to me that the postfix hasn't been valued. Any suggestion about it?

Thank you in advance.
+Pie Number of slices to send: Send
 

pagano monello wrote:the result surprises me. In fact I expected 21, it seems to me that the postfix hasn't been valued. Any suggestion about it?


Because it's postfix, the value of a (10) is used first and then it's incremented with 1 (a=11) and then the result of the sum (20) is assigned to a. It's much more clear if you use another variable to store the sum like in this example

And this topic has a very informative discussion about post-increment (or post-decrement) operators. So if you have some doubts, it's definitely worth reading. And if you still have some doubts after reading that topic, just let us know by hitting the reply button

Hope it helps!
Kind regards,
Roel

PS. You are using the postfix increment operator in your example, but the title suggests the decrement operator. I updated the title accordingly, hope that's ok for you.
+Pie Number of slices to send: Send
Hi Roel,
thank you for your answer, I am gonna read the topic you suggested me; this thing is a little tricky.
Thank you also for having updated the title of the topic which was actually wrong.

pagano
LOOK! OVER THERE! (yoink) your tiny ad is now my tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 357 times.
Similar Threads
reusult of this a = a + a++;
Why is this printing 1,0 instead of 1,1
Marcus Green Question(can any body explain
Prefix or Postfix Operator
JQ+ Arrays[0]++
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:25:00.