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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

post and pre increment opeartor

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this output 5 as after ++y ,y become 2 then add to it y++ where y =2 in that case then 2+2 =4 then increment y to be 3 then add the left hand side operands of y to have y=y+4= 3+4= 7
??? that what i understand but i don't know how 5 is the output ??

also try to test the same code changing y to int y=0; and see the result that =2
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Heba Mahmoud wrote:

this output 5 as after ++y ,y become 2 then add to it y++ where y =2 in that case then 2+2 =4 then increment y to be 3 then add the left hand side operands of y to have y=y+4= 3+4= 7



First of all try to simplyfy the expression.
Actual expression is

y= y+ ++y + y++;

Here y=1. And when you say y+ that is 1+
then it comes to ++y here y becomes 2

expression is y= 1 + 2 + y++ ok?

then it becomes like this y=1 + 2 + 2 (And not 3 Because it is post increment operator so it first uses current value then
increment it.)

Thats why... finally y= 1 + 2 + 2 i.e. 5

In case of 0.... I think you can follow the above steps.
You'll get the answer as 2.






 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct!
 
Heba Mahmoud
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why you evalute the expression as


and didn't evalute it as the rule saying evalute the right operand first then the left operand

i understand what you do but i don't understand why not you evalute it as i made ??
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Heba Mahmoud wrote:why you evalute the expression as


and didn't evalute it as the rule saying evalute the right operand first then the left operand

i understand what you do but i don't understand why not you evalute it as i made ??



There is a order.

In Java the Order of Evaluation is from left to right...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 3 things related to Operators in Java

1) Precedence Order: When two operators share an operand, the operator with higher precedence goes first.. I think, you know that!
2) Associativity : When two operators with the same precedence the expression is evaluated according to its associativity.
For Ex :

is treated as

Since = operator has right to left associativity.

3) Order of evaluation : In Java, the left operand is always evaluated before the right operand. also it applies to function arguments.

For Ex :


Hope you understand this!
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Heba Mahmoud wrote:



is nothing but y = y + (++y + y++);

Always use decompiler for this kind of code .

decompiled code is below:


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic