Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does evaluation take place ?

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


1. x * 5 = 15; // as x = 3
2. ( ( x * 5 ) / x ) = 5; // as x = 3 and remaining operations are 5 + --x and ++, -- of x is yet to evaluate.
3. 5 + 2 ; // as x was 3 but --x made x = 2; so = 7
4. ++x and --x means, add 1 to x and minus 1 to x so x is same i.e. x = 2 and y = 7

Operator Precedence in above example:

* (Operators with same priority so left to right

/

-- then + because --x is pre decrement so predcremented first then 5 + 2

++ then -- ( from left to right)

Did evaluate correctly? Output is correct I know, I run that, x = 2 and y = 7.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:Did evaluate correctly? Output is correct I know, I run that, x = 2 and y = 7.

This thread shows the order of evaluation of the exact same code snippet.

And in this thread you'll find a very informative explanation about evaluating boolean expressions with short-circuit operators. And this thread has a very enlightning discussion about the order of evaluation when using post-increment operator.

So I would advice to read these threads and if you still have questions or doubts, just hit the reply button and let us know!

Hope it helps!
Kind regards,
Roel
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Roel yes it's from same page and got your explanation thank you.
 
Henry Wong
author
Marshal
Pie
Posts: 22104
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:
1. x * 5 = 15; // as x = 3


Yes. The x variable is 3, so, "x * 5" equals 15. However, that is not the expression. The expression is "++x * 5", and the "++x" expression has a value of 4, so the expression is equal to 20.

Ganish Patil wrote:
2. ( ( x * 5 ) / x ) = 5; // as x = 3 and remaining operations are 5 + --x and ++, -- of x is yet to evaluate.


Again, that is not the expression. The expression is (with parens to account for precedence and associativity) ... "(( ++x * 5) / (x--)". The evaluation is left to right. So "++x" has a value of four, and "x--" also has a value of four, and the value of x is back to three at the end of the evaluation of that sub-expression. So... the result of the sub-expression is five because four times five divided by four is five.

Henry
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Henry thank you for explanation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic