Logic Expression ... possible bug for short circuit boolean operator?
I just came across some thing that's supposed to be fundamental but it puzzled me a lot.
It's the evaluation of the logic/boolean expression : A or B or C and D.
if this is written in java short-circuit boolean operators: A || B || C && D
if D is false, by right the whole expression is false.
However if any of A, B, is true, java short-circuit boolean operator seems to stop after the first || is true. I think by right, the more correct operation/practice is to skip all the parallel ORs, and jump to the parallel ANDs, sees D is false, and return whole expression value as false.
Please feel free to comment on the following test code:
==================================================
public class testLogic {
public static void main(String[] args)
{
boolean a = true;
boolean b = true;
boolean c = false;
boolean d = false;
System.out.println("(a || b || c) && d = " +( (a || b || c) && d));
System.out.println("a || b || c && d = " +( a || b || c && d));
System.out.println("a | b | c & d = " +( a | b | c && d));
}
}
================================================== execution
Test> java testLogic
(a || b || c) && d = false
a || b || c && d = true
a | b | c & d = false
Re: Logic Expression ... possible bug for short circuit boolean operator?
The operator precedence of the && operator is higher than the || operator precedence. So a || b || c && d is evaluated as a || b || (c && d); short circuiting stops the evaluation (which is strictly from left to right) if a happens to be true. If none of a and b are true then c && d must be true to make the entire expression true. The && operator has lower precedence than the | (single vertical bar) operator, so a | b | c && d is evaluated as (a | b | c ) && d.
kind regards,
Jos
Re: Logic Expression ... possible bug for short circuit boolean operator?
Thank you Jobs! That's very clear and helpful explanation.
Just 1 more question.. Is this order of precedence by design? would grouping by brackets increase the cost of evaluation?
It seems a trade off between cost and clarity here..
Re: Logic Expression ... possible bug for short circuit boolean operator?
Quote:
Originally Posted by
JimmyD
Thank you Jobs! That's very clear and helpful explanation.
Just 1 more question.. Is this order of precedence by design? would grouping by brackets increase the cost of evaluation?
It seems a trade off between cost and clarity here..
Nope, brackets in expressions don't cost anything during runtime, e.g. (((((1+(((((2)))))))))) is the same as 1+2; the compiler might have to take a few extra steps (depending on the way the expression is parsed) but the result in compiled code is the same. Brackets in expressions can change the order of evaluation though, e.g. 2*(3+4) is evaluated in a different order than the expression 2*3+4.
kind regards,
Jos
Re: Logic Expression ... possible bug for short circuit boolean operator?
Thanks so much Jos for taking time to reply!
Re: Logic Expression ... possible bug for short circuit boolean operator?
Quote:
Originally Posted by
JimmyD
Thanks so much Jos for taking time to reply!
You're welcome of course.
kind regards,
Jos
Re: Logic Expression ... possible bug for short circuit boolean operator?
JosAH beat me to the answer, but I'll chip in that using lots of parenthesis is a good practice. In Eclipse, you can set code cleanup to add them automatically. I HATE having to find a bug in code where the programmer relied on precedence, rather than using parenthesis. I have to parse every expression, knowing one of them is wrong.