• 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 operator precedence

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
given:

boolean b1 = false, b2 = true, b3 = true;

does b4 = b1 || !b3 && b2;

get executed as b4 = (b1 || (!b3 && b2)); or b4 = ((b1 || !b3) && b2); ???
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeet Jain wrote:
does b4 = b1 || !b3 && b2;

get executed as b4 = (b1 || (!b3 && b2)); or b4 = ((b1 || !b3) && b2); ???



Logical AND has higher precendence than logical OR, so the first choice.

Henry
 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply. but with the same initial values of b1,b2,b3 executing the following code:

boolean b4 = operandEval(1, b1) || !operandEval(2, b3) && operandEval(3, b2);
System.out.println();
System.out.println("Value of b4: " + b4);
static boolean operandEval(int opNum, boolean operand)
{
System.out.print(opNum);
return operand;
}

results in output:

12
Value of b4: false

???
 
Saloon Keeper
Posts: 13268
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, you are referring to logical operators, not conditional. Java has the ?: operator which is conditional.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it got evaluated in the following order:
b4=(b1||(!(b2&&b3)))
 
Henry Wong
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeet Jain wrote:
boolean b4 = operandEval(1, b1) || !operandEval(2, b3) && operandEval(3, b2);
System.out.println();
System.out.println("Value of b4: " + b4);
static boolean operandEval(int opNum, boolean operand)
{
System.out.print(opNum);
return operand;
}

results in output:

12
Value of b4: false

???



Let's not confuse precedence with the order of evaluation. As mentioned, precedence determines "where the parenthesis are", but unlike humans, the computer (and compiler) doesn't have to evaluate in the same order -- meaning it doesn't need to evaluate from the inner most parens outward. The order of evaluation, as defined by the Java specification, is for the most part, done from left to right. In this example, only the short circuiting rules directly affect the order of evaluation.

Henry

 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohh thank you. But then that means that precedence(parentheses) has no relevance since it is going to be evaluated left to right anyways? And doesn't the NOT(!) operator have higher precedence than || and &&? so first the b2 operand should be evaluated, right?
 
Henry Wong
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeet Jain wrote:Ohh thank you. But then that means that precedence(parentheses) has no relevance since it is going to be evaluated left to right anyways? And doesn't the NOT(!) operator have higher precedence than || and &&? so first the b2 operand should be evaluated, right?



Well, I wouldn't say no relevance, as precedence affects what the logical (short-circuit) operators, which in turn, does affect the order of evaluation (to stop evaluating that is).

Henry
 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you so much:) finally got it
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Jeet Jain wrote:Ohh thank you. But then that means that precedence(parentheses) has no relevance since it is going to be evaluated left to right anyways? And doesn't the NOT(!) operator have higher precedence than || and &&? so first the b2 operand should be evaluated, right?



Well, I wouldn't say no relevance, as precedence affects what the logical (short-circuit) operators, which in turn, does affect the order of evaluation (to stop evaluating that is).

Henry



The following,
boolean a,b,c;
a=b=c=false;
boolean x = (a = true) || (b = true) && (c = true);
System.out.println(a +" "+b +" "+c +" "+x);

output
true false false true

I read that the logical operator's precedence is && ^ and ||
I really do not understand when do I have to apply the precedence of && and ^
Can you help me?
 
Henry Wong
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ilakya Mukunth wrote:
I read that the logical operator's precedence is && ^ and ||
I really do not understand when do I have to apply the precedence of && and ^



The logical AND has higher precedence than the logical OR, so this line...



is like this line...



Henry
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic