Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Conditional And-Or

Akash Roy
Greenhorn
Posts: 16
Hi,
just want to clear a basic doubt..
Consider the following code:

the values printed are true false false

But since Conditional And && has higher priority than Conditional OR || shouldnt the && expression get evaluated first??? Thanking in advance,

Regards

Jeroen Wenting
Ranch Hand
Posts: 5093
The condition in front of the OR is always true.
This means that the result of the AND is no longer relevant, and so it is not even looked at.

(a = true) || (b = true) && (c = true) can become either ((a = true) || (b = true)) && (c = true) or (a = true) || ((b = true) && (c = true)) depending on operator precedence, but in either case the result of the && is irrelevant if the operand on the left of the || is true (as is the case here, as (a = true) returns true.

amit taneja
Ranch Hand
Posts: 813
in case ((a = true) || (b = true)) && (c = true)

i think && will be relavent because (a = true) || (b = true) in above is one block and will be evaluated and if return true ( in above case (a=true) is sufficent for making that block true ).....then (c = true)
will be evaluated....
if first block is not true then (c=true) will not be evaluated at all...

and one more thing...
the expresion (a = true) || (b = true) && (c = true) is equalent to
(a = true) || (( b = true) && (c = true))
but why ? .. i want more explanations....

but NOT ((a = true) || (b = true)) && (c = true)

I am right ?
pls correct me ...

Animesh Shrivastava
Ranch Hand
Posts: 298
I dont understand why we need to simplify (a = true) || (b = true) && (c = true) to either (a = true) || (( b = true) && (c = true)) or
((a = true) || (b = true)) && (c = true)?

As Short Circuit operators are used here, so as soon as the result of the expression is found, JVM does not proceed further
As a = true, the whole expression would result in true as we can make out.
So, b and c are not evaluated to true,
b and c still has their default value i.e false

Jeroen Wenting
Ranch Hand
Posts: 5093
That's exactly what I mean Animesh.

Akash Roy
Greenhorn
Posts: 16
hi,
but doesnt () have a higher precedence than || and &&.
Regards

Animesh Shrivastava
Ranch Hand
Posts: 298
So, how does () is creating confusion for u?
The expression is testing finally for conditional operators || and &&

Jeroen Wenting
Ranch Hand
Posts: 5093
Originally posted by Akash Roy:
hi,
but doesnt () have a higher precedence than || and &&.
Regards

it does, but in this case it's irrelevant!
After the JVM determines that the statement "a = true" returns true is knows that the entire conditional statement is true (because of the shortcut || operator) and is content to skip the rest of the statement (as it's pointless because it won't affect the result).

amit taneja
Ranch Hand
Posts: 813
hey guys..

please see my post... and tell me whether i m right or wrong...
i mean i have consider the point of involving () brackets..
and seeing how the result will make the difference...

just wana comments on u ppl... pls go though my post above...

regards,

Akash Roy
Greenhorn
Posts: 16
hi,
Yeah i am thinking that since () has a higher precedence than && and || all the assignment (a=),(b=),(c=) should get evaluated and then the JVM should proceed with the && ,|| operations. Which is obviously not seen here. So please explain why isnt it so???

Regards,

Jeroen Wenting
Ranch Hand
Posts: 5093
(a=true) has exactly the same effect here as would have a=true so the braces have no effect at all.

Your error is in not understanding that the shortcut OR operation causes everything to the right of it to not be considered at all if that which is on the left of it results in true.

No matter how much you want that not to be so, you're not going to make it so...