Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Precedence rules and short circuit boolean operators.

Narasimha Rao B.
Ranch Hand
Posts: 205
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.

Preethi Shetty
Greenhorn
Posts: 11
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.
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
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
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

Corey McGlone
Ranch Hand
Posts: 3271
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
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
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
Hi,
The question still remains unanswered............help needed..
Thanks,
Preethi

JL Henderson
Greenhorn
Posts: 5
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 ]

 Don't get me started about those stupid light bulbs.