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

# ++X

Ranch Hand
Posts: 1479
• Number of slices to send:
Optional 'thank-you' note:
Khalid Mughal states that the prefix unary operator associates right to left. Can anyone construct an expression where this will happen ? I've tried for days with no luck...
3-13-01
I think I found what I was looking for:
-~++x would associate like this -(~(++x)) which would be right associativity and post fix would associate like this ((x++)~)- which all seems to make sense.
[This message has been edited by herb slocomb (edited March 13, 2001).]

Ranch Hand
Posts: 782
• Number of slices to send:
Optional 'thank-you' note:

frank davis
Ranch Hand
Posts: 1479
• Number of slices to send:
Optional 'thank-you' note:
That's the problem, Khalid makes the statement I gave above, but doesn't elaborate or give any examples. Associativity rules are applied when there are 2 operators of the same precedence that follow each other in an expression. For example, "*" and "/" operators have equal precedence :
int x = 1,y = 2;
int z = y * x / y;
Associativity rules say "*" and "/" have left associativity, so they would be grouped left to right like this :
int z = ((y * x) / y);
The order is important because a different associativity would give a different result (in this case because they are int).
Now, can anyone on the planet give an example of right associativity for the prefix increment operator like I gave for the "*" and "/" operator above???

[This message has been edited by herb slocomb (edited March 12, 2001).]

Ranch Hand
Posts: 154
• Number of slices to send:
Optional 'thank-you' note:
If you consider that the operator is '++' then the prefix version does have right associativity, as it evaluates the variable then increments it (applies the operator).
I'm not even sure if you could call it associativity, as there is only one side to evaluate (didn't really concentrate in maths)- so how do you show this in an expression? Perhaps the author's just trying to explain the difference between x++ and ++x.
I guess any expression shows it
x=1;
y=3*++x; // shows x is incremented before being multiplied

Ranch Hand
Posts: 104
• Number of slices to send:
Optional 'thank-you' note:
Herb
You've asked this before many times in this forum
Can you go through that again?

Ranch Hand
Posts: 232
• Number of slices to send:
Optional 'thank-you' note:
you know what's funny herb?
van der linden (Just Java, p.123) states that pre-
increment/decrement has higher precedence than post-incr/decr.
flanagan (nutshell, p.30) says it's the other way around.
turns out in practice it doesn't matter.
btw, as you know, things like ++i-- and ++++i won't even
compile anyway.

frank davis
Ranch Hand
Posts: 1479
• Number of slices to send:
Optional 'thank-you' note:
Thanks to everyone for their answers. I understand quite well how the pre and postfix operators work and their precedence order. My question is specifically only figuring out what the author meant when he said prefix operator "++" would have right associativity.

This has never been aswered in a way that makes sense. Associativity means you are grouping operand(s) and an operator together in a specific order to evaluate them as I did at previously in this post. Khalid distinguishes between post and pre fix operators as having different associativity - but he gives no examples. I suspect he gave no examples because there are none.

A prior response gave this as an example :
y=3*++x; // shows x is incremented before being multiplied
but Khalid also states that Associativity applies only to operators of equal precedence and that does not occur in the example above. There is also confusion between evaluation of the operand and application of the operator : they are two different things with two distinct steps.
Siva,
if you believe the question has been amswered please help me and print 1 single statement showing both left and right associativity using groups of pre and post fix operators. \$5 dollars says you can't do it...

frank davis
Ranch Hand
Posts: 1479
• Number of slices to send:
Optional 'thank-you' note:
Is there some way to close this post and reopen in the Intermediate level postings? No one here can answer the question it seems..
3-13-01
I think I found what I was looking for:
-~++x would associate like this -(~(++x)) which would be right associativity and post fix would associate like this ((x++)~)- which all seems to make sense.
[This message has been edited by herb slocomb (edited March 13, 2001).]

"The Hood"
Posts: 8521
• Number of slices to send:
Optional 'thank-you' note:
Done.

 The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck. a bit of art, as a gift, the permaculture playing cards https://gardener-gift.com