Results 1 to 7 of 7
  1. #1
    JimmyD is offline Member
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    44
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default 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
    Last edited by JosAH; 10-26-2011 at 07:46 PM.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    JimmyD is offline Member
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    44
    Rep Power
    0

    Default 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..

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default Re: Logic Expression ... possible bug for short circuit boolean operator?

    Quote Originally Posted by JimmyD View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    JimmyD is offline Member
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    44
    Rep Power
    0

    Default Re: Logic Expression ... possible bug for short circuit boolean operator?

    Thanks so much Jos for taking time to reply!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default Re: Logic Expression ... possible bug for short circuit boolean operator?

    Quote Originally Posted by JimmyD View Post
    Thanks so much Jos for taking time to reply!
    You're welcome of course.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default 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.
    The Java Tutorial. Read it.

Similar Threads

  1. Boolean Logic
    By DaveC in forum New To Java
    Replies: 6
    Last Post: 06-15-2011, 05:11 AM
  2. [SOLVED] Boolean Expression Evaluation Framework
    By priyanka.dandekar in forum Advanced Java
    Replies: 8
    Last Post: 03-27-2010, 02:35 PM
  3. operator || cant be applied to OlimpicFrog, boolean
    By darkblue24 in forum New To Java
    Replies: 2
    Last Post: 02-16-2010, 12:37 AM
  4. [SOLVED] need help with logic operator
    By auralius in forum New To Java
    Replies: 10
    Last Post: 12-25-2008, 10:01 PM
  5. Boolean Expression
    By ritwik07 in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 04:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •