• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Conditional And-Or

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ...
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's exactly what I mean Animesh.
 
Akash Roy
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
but doesnt () have a higher precedence than || and &&.
Regards
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, how does () is creating confusion for u?
The expression is testing finally for conditional operators || and &&
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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...
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic