Forums Register Login

Conditional And-Or

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ...
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
That's exactly what I mean Animesh.
+Pie Number of slices to send: Send
hi,
but doesnt () have a higher precedence than || and &&.
Regards
+Pie Number of slices to send: Send
So, how does () is creating confusion for u?
The expression is testing finally for conditional operators || and &&
+Pie Number of slices to send: Send
 

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).
+Pie Number of slices to send: Send
hey guys..

please see my post... and tell me whether i m right or wrong...
u do have made comments on actually post but not mine..
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...

thanx for reading


regards,
+Pie Number of slices to send: Send
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,
+Pie Number of slices to send: Send
(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...
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 684 times.
Similar Threads
Evaluation Order.
Conditional Operators (try this)
Conditional Operators
unprecedented precedence
System.out.print(s + (b ? "T" : "F"));
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:49:03.