Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Postfix Operator evaluation

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading a separate post with postfix operators and have some confusion on the evaluation of postfix operators. For example:

This will print out j=0. I understand why this occurs as the value of j is incremented AFTER the assignment occurs. Now consider the next piece of code:


This will print out j=1. In this case the increment has occurred BEFORE the assignment occurs.

Can it be said that when there are more operations than just a single postfix operator that the postfix will be applied to the variable and a left to right evaluation occurs?
(Example: the above statement j = j++ + j turns into:
j = (j + 1) + 0
j = 1 + 0
Thus: j=1
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is all about evaluation of operands. Let me explain exactly what is happening and you can draw your own conclusions.

Here's our line of code:



In order to evaluate this expression, we must first evaluate operands, from left to right. The first operand we evaluate is j++. As you already know, this operand evaluates to 0 but, as a side-effect, j is incremented to 1. We now have this:



Now, we evaluate the next operand, which is just j. At this point, j is 1 so our expression evaluates to this:



Obviously, that expression assigns the value of 1 to j.
 
Chris Allen
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I like the method you use to break out the values (tracking the value of the variable as a comment on the right side of the expression) as the order is extremely important. I changed the example slightly and got different results which only made sense once I used your method of tracking the value. For example:



But if you switch the order of the operands to:

 
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic