posted 1 year ago

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.

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.

Being Java programmer.

posted 1 year ago

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 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

posted 1 year ago

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.

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 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

It is sorta covered in the JavaRanch Style Guide. |