posted 9 months ago

Hi guys,

I was just going through some of Twist in Tale questions from the Mala Gupta book. My question is about logical operator precedence. To be more specific take for example this snippet of code:

In the explanation for line 9, Mala explains that "Because the short-circuit operator

AND has higher operator precedence than the short-circuit operator OR, the expression

is evaluated as follows:

(a >= 99 || (a <= 33 && b == 10))".

Ok, so far so good. But then I wrote a sample code snippet of my own:

The output for this code is: 11 20 30. My assumption that the line would be evaluated as : (a++==10 ||( ++b >20 && c++>40)) was obviously incorrect because neither b or c was incremented. Why is that? How do I know in which scenario && will take a higher precedence and in which situation it won't?

Thanks in advance for helping.

I was just going through some of Twist in Tale questions from the Mala Gupta book. My question is about logical operator precedence. To be more specific take for example this snippet of code:

In the explanation for line 9, Mala explains that "Because the short-circuit operator

AND has higher operator precedence than the short-circuit operator OR, the expression

is evaluated as follows:

(a >= 99 || (a <= 33 && b == 10))".

Ok, so far so good. But then I wrote a sample code snippet of my own:

The output for this code is: 11 20 30. My assumption that the line would be evaluated as : (a++==10 ||( ++b >20 && c++>40)) was obviously incorrect because neither b or c was incremented. Why is that? How do I know in which scenario && will take a higher precedence and in which situation it won't?

Thanks in advance for helping.

posted 9 months ago

The output

If the expression

Hope it helps!

Kind regards,

Roel

- 1

Sama Willson wrote:The output for this code is: 11 20 30. My assumption that the line would be evaluated as : (a++==10 ||( ++b >20 && c++>40)) was obviously incorrect because neither b or c was incremented. Why is that? How do I know in which scenario && will take a higher precedence and in which situation it won't?

The output

`"11 20 30"`confirms that your assumption about the evaluation order of the expression

`a++==10 || ++b >20 && c++>40`is correct. Because

`a++==10`evaluates to

`true`, the short-circuit operator won't execute the right-hand side (

`++b >20 && c++>40`) and therefore

`b`and

`c`are not incremented.

If the expression

`a++==10 || ++b >20 && c++>40`would have been evaluated like

`(a++==10 || ++b >20) && c++>40`, then

`c`would be incremented as well.

Hope it helps!

Kind regards,

Roel

posted 9 months ago

Hi Roel

I am still confused about it, in Mala's example, she says that since AND has higher precedence than OR, the AND operation would be performed first. So as far as I understand this, it should be something like: 2+3*3 where 3*3 would be evaluated first. Keeping this in mind, in my expression wouldn't the AND expression be evaluated first ? Is there no such precedence between AND and OR? Am I understanding it incorrectly?

I am still confused about it, in Mala's example, she says that since AND has higher precedence than OR, the AND operation would be performed first. So as far as I understand this, it should be something like: 2+3*3 where 3*3 would be evaluated first. Keeping this in mind, in my expression wouldn't the AND expression be evaluated first ? Is there no such precedence between AND and OR? Am I understanding it incorrectly?

posted 9 months ago

I just had a look at the example you are referring to and the study guide is

It is correct that the expression

In this topic you'll find an extensive explanation about the order of evaluation of expressions with short-circuit operators using a boatload of code examples. Definitely worth reading!

Hope it helps!

Kind regards,

Roel

Sama Willson wrote:I am still confused about it, in Mala's example, she says that since AND has higher precedence than OR, the AND operation would be performed first. So as far as I understand this, it should be something like: 2+3*3 where 3*3 would be evaluated first. Keeping this in mind, in my expression wouldn't the AND expression be evaluated first ? Is there no such precedence between AND and OR? Am I understanding it incorrectly?

I just had a look at the example you are referring to and the study guide is

__! That's not at all how this expression is evaluated. This is an errata item which should definitely be reported.__

**completely wrong**It is correct that the expression

`a++==10 || ++b >20 && c++>40`is equivalent with

`a++==10 || (++b >20 && c++>40)`and thus will produce the same output (and same values for the different variables). But an expression (in Java) is always evaluated from left to right (except an assignment which is evaluated from right to left). So that means the sub-expression

`a++==10`is evaluated first, and because this sub-expression evaluates to

`true`, the short-circuit operator won't execute the right-hand side operand anymore because the expression will always evaluate to

`true`. And the output of your own code snippet illustrates that this order of execution is the only correct one. If it would have been evaluated like described in the book, you would have got different values for

`b`and

`c`.

In this topic you'll find an extensive explanation about the order of evaluation of expressions with short-circuit operators using a boatload of code examples. Definitely worth reading!

Hope it helps!

Kind regards,

Roel