-5

I was reading through some old code and came across an unqualified Boolean expression like this:

if (first && second || third)
{
    // do something
}

There are two valid ways of qualifying that expression.

if ((first && second) || third)

and

if (first && (second || third))

which evaluate differently for some values of first, second and third.

I tried this out in C# and the results aligned with the first qualification method. Curiosity got the better of me and I tried it out in Java too, which aligned with the second method. Why is this? Shouldn't the evaluation be the same in different languages?

gcarvelli
  • 1,580
  • 1
  • 10
  • 15
  • 3
    Why should it be the same? Totally different people wrote the compilers and runtimes. It would certainly be *nice* if they were the same, but I see no logic that says they *would* be. – BradleyDotNET Nov 06 '14 at 19:41
  • 4
    It's that way because the designers of each language choose to design them that way. What else are you expecting to hear? You'd have to ask the designers of the language why they choose to make any particular design decision. We can't know why they choose what they did. – Servy Nov 06 '14 at 19:42
  • 1
    Actually, this *is* strange - I just looked up the operator precedence of C# and Java, and they both agree that `&&` binds more strongly than `||` - so they should both choose the first way! – harold Nov 06 '14 at 19:45
  • Weird result. Can you show both code snippets (C# and Java) you used to demonstrate this different behavior? – Pierre-Luc Pineault Nov 06 '14 at 19:46
  • The information in this question is not correct. `&&` binds tighter than `||` in all C-like languages, including Java. – khelwood Nov 06 '14 at 19:52
  • This is a classic [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You *should* be showing how you test your theory here and asking if you're testing it correctly but instead you make an (incorrect) assumption that your test was flawless and ask about a difference in the languages. – tnw Nov 06 '14 at 19:53

1 Answers1

6

Both Java an C# evaluate && before || since logical AND has a higher precedence in both languages:

I think you're running your tests incorrectly.

As you can see by running the Java

class Test {

    public static void main (String[] args) {

        boolean a = true;
        boolean b = true;
        boolean c = false;

        System.out.println(a || b && c);
        System.out.println((a || b) && c);
        System.out.println(a || (b && c));
    }
}

and C#

using System;

public class Test
{
    public static void Main()
    {
        bool a = true;
        bool b = true;
        bool c = false;

        Console.WriteLine(a || b && c);
        Console.WriteLine((a || b) && c);
        Console.WriteLine(a || (b && c));
    }
}

demos on Ideone, both produce the same output:

true
false
true

Which show that && is evaluate before || in both languages.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288