Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

conditional operator

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class EBH202 {
static boolean a, b, c;
public static void main (String[] args) {
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}}

How come it results in true false while && has higher precedence. c should also get true. please help
Thanks in advance

uzma
 
uzma Akbar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class EBH203 {
static boolean a, b, c;
public static void main (String[] args) {
boolean x = a || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}}

while here && is evaluated and the result is :fase, true, true
 
uzma Akbar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got the answer
posting for others

In evaluation of boolean expressions involving conditional AND and OR, the left-hand operand is evaluated before the right one, and the evaluation is short-circuited (i.e., if the result of the boolean expression can be determined from the left-hand operand, the right-hand operand is not evaluated). In other words, the right-hand operand is evaluated conditionally.

thanks

uzma
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello uzma,
Since the left most expression is evaluated first, a becomes true. Hence other assignments dont take place which would result in
a = true, b = false, c = false

ranchers correct me if i am wrong.
 
Prash Gali
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My answer is for the code you posted the first time.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prash Gali:
Hello uzma,
Since the left most expression is evaluated first, a becomes true. Hence other assignments dont take place which would result in
a = true, b = false, c = false

ranchers correct me if i am wrong.



But why the left most expression is evaluated first, if it has lower precedence than && ?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See here
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

When i run the program, it gives the following output:

false, true, true
 
uzma Akbar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for putting effort to answer.

I run it again and it gives true, false, false. Please Raj run it again the first query I posted.

My confusion was that as && as higher priority than || so it should be evaluated first and c should get true but as I posted in my third post:

In evaluation of boolean expressions involving conditional AND and OR, the left-hand operand is evaluated before the right one, and the evaluation is short-circuited (i.e., if the result of the boolean expression can be determined from the left-hand operand, the right-hand operand is not evaluated). In other words, the right-hand operand is evaluated conditionally.

I concluded it is not the matter of precedence but the matter of left operand.

Please correct me if I am wrong

uzma
 
Ponnus Raj
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Akbar,

Your first Query Result : true, false, false.

It shows || operator evaluated first, thats why the result..

Thanks.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Akbar,

thanks for nice riddle.

IMHO the example is combination of both - operator precedence and evaluation of boolean expressions.

Because && has really higher priority than ||, expression is evaluated the same way as:

boolean x = (a = true) || ( (b = true) && (c = true) );

And now evaluation of boolean expressions takes its part. Because left expression is true, rest is not evaluated.

So the result is: true, false, false
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like our friend sedlacek has quoted the expression can be
rewritten as

boolean x = (a = true) || ( (b = true) && (c = true) );

However now this expression will be evaluated from the left
and as soon as abecomes true, the JVM knows that the expression is short
circuted, so it wont go ahead and execute the other bracket

Tricky one


_____________________________________
Hi Akbar,

thanks for nice riddle.

IMHO the example is combination of both - operator precedence and evaluation of boolean expressions.

Because && has really higher priority than ||, expression is evaluated the same way as:

boolean x = (a = true) || ( (b = true) && (c = true) );

And now evaluation of boolean expressions takes its part. Because left expression is true, rest is not evaluated.

So the result is: true, false, false
_______________________________________
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool, now I will program

where:

instead of boring

[ January 21, 2006: Message edited by: dema rogatkin ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While I *think* that produces the same results, personally I prefer the later example. It is much clearer what happens. Ask yourself if you will be able to understand the first example if you come back to read that code in two weeks, a month, 3 months, or longer.

Layne
 
Maybe he went home and went to bed. And took this tiny ad with him:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic