aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Conditional And-Or Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Conditional And-Or" Watch "Conditional And-Or" New topic
Author

Conditional And-Or

Akash Roy
Greenhorn

Joined: Jan 08, 2005
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

Joined: Oct 12, 2000
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.


42
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
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 ...


Thanks and Regards, Amit Taneja
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
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

Joined: Oct 12, 2000
Posts: 5093
That's exactly what I mean Animesh.
Akash Roy
Greenhorn

Joined: Jan 08, 2005
Posts: 16
hi,
but doesnt () have a higher precedence than || and &&.
Regards
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
So, how does () is creating confusion for u?
The expression is testing finally for conditional operators || and &&
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
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

Joined: Mar 14, 2003
Posts: 810
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,
Akash Roy
Greenhorn

Joined: Jan 08, 2005
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

Joined: Oct 12, 2000
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Conditional And-Or