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

# Precedence rules and short circuit boolean operators.

Ranch Hand
Posts: 205
• Number of slices to send:
Optional 'thank-you' note:
Hi,
Please go through the below piece of code,
1. boolean a,b,c;
2. a=b=c=false;
3. boolean x = (a=true) || (b=true) && (c=true);
After this statement values of the different boolean variables are,
a - true
b - false
c - false
x - true
Now upto this fine. My doubt is, && having higher precedence than ||, hence first && should be evaluated before ||. That means, (b=true) && (c=true) should have been evaluated and values of b and c should have been true.
Even if i use () for (b=true) && (c=true), still i am getting the same output. That is,
boolean x = (a=true) || ( (b=true) && (c=true) );
This statement is also giving the same output.
IS it something like this,
if in the equation it finds || operator and the left hand side is true, then it will ignore all the right hand sides of || operator.

Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:
Hi,
When the boolean || operator is encountered the control doesn't deal with the rest of the statement if its TRUE. In this case as the first part of the statement is TRUE so the control will not go beyond the || operator.Hence you see those results.
Now please try this:
boolean x = (b=true) && (c=true) || (a=true);
and you will get a result as,
a - false
b - true
c - true
x - true
Hope that helps,
Preethi

Narasimha Rao B.
Ranch Hand
Posts: 205
• Number of slices to send:
Optional 'thank-you' note:
Preethi, Do you mean here precedence rules will not be applicable. If you take the below example,
int x = 2 + 3 * 4 - In this equation always 3*4 will be evaluated first and then on that result + 2 will be applied, becuase * is having higher precedence than +.
But if take && , || --- precedence rules will not be applicable (and also even if you have more braces) - Always evaluation starts from left side, based on the operators and operands equation will be evaluated partly or fully..
Is my understanding is correct...
Thanks

Preethi Shetty
Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:
Hi,
You really made me put on my thinking caps............
After going through the following sets of examlpes I really don't know as to why the precedence rule with the && and || operator is not followed
Eg 1) boolean a,b,c;
a=b=c=false;
boolean x = (a=true) || (b=true) && (c=true);
After this statement values of the different boolean variables are,
a - true
b - false
c - false
x - true
Eg 2 -- boolean a,b,c;
a=b=c=false;
boolean x = (b=true) && (c=true) || (a=true);
After this statement values of the different boolean variables are,
a - false
b - true
c - true
x - true
&& HAS MORE PRECEDENCE THAN THE || OPERATOR THEN WHY IS IT THAT THE PRECEDENCE RULE IS NOT FOLLOWED IN THIS CASE?
Perhaphs the compiler has been built that way.I guess someone can help us in answering this question ...Experts do fill us in.
Thanks in advance for the help.
Preethi

Ranch Hand
Posts: 3271
• Number of slices to send:
Optional 'thank-you' note:
Narasimha Rao B. & Preethi S,
I just noticed that your publicly dislpayed names don't follow our general rule for names. The key part is that we do allow first initials, but we ask for full last names. I know it seems like a nitpicky thing, but I would like to ask you to modify your publicly displayed names. You can read the Javaranch Naming Policy here and change your publicly displayed name here.
To Narasimha, I realize that you've been around the ranch for some time now and I'm surprised no one has asked you about this before. If there are some other extenuating circumstances that I don't know about, please let me know.
To Preethi, I welcome you to the Ranch and I hope you enjoy your stay.
Corey

Narasimha Rao B.
Ranch Hand
Posts: 205
• Number of slices to send:
Optional 'thank-you' note:
Hi Corey,
My full name is Bandlamudi Narasimha Rao.
Bandlamudi - Is my family name ( Common to all my family members)
Narasimha Rao - Is my name. (It is my actual name)
So every one call me as - Narasimha, Narasimha Rao.
In all the applications i will write as, B. NARASIMHA RAO or
Narasimha Rao B.

I hope it clarifies your doubt.

Thanks..

Corey McGlone
Ranch Hand
Posts: 3271
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Narasimha Rao B.:
I hope it clarifies your doubt.

That's fine - our goal is really to give JavaRanch a "professional" feel. Having people posting with handles such as "javakiller" or "juicyjuice696" takes away from that - I would have to say that your name is close enough (and certainly been in use long enough) to be considered to pass that rule.
Thanks.

Preethi Shetty
Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:
Hi,
The question still remains unanswered............help needed..
Thanks,
Preethi

Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
Think of it this way: precedence determines how tightly operators bind to their operands, which is not the same as the order in which terms are evaluated. Given the precedence rules,
(a=true) || (b=true) && (c=true);
is the same as
(a=true) || ((b=true) && (c=true));
but once the grouping is determined, the evaluation still proceeds left to right. So the short circuit || prevents further evaluation.
---
Jerry
[ March 18, 2004: Message edited by: JL Henderson ]

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.