Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Wednesday, December 23, 2015

C# conditional AND (&&) OR (||) precedence

C# conditional AND (&&) OR (||) precedence


We get into unnecessary coding arguments at my work all-the-time. Today I asked if conditional AND (&&) or OR (||) had higher precedence. One of my coworkers insisted that they had the same precedence, I had doubts, so I looked it up.

According to MSDN AND (&&) has higher precedence than OR (||). But, can you prove it to a skeptical coworker?

http://msdn.microsoft.com/en-us/library/aa691323(VS.71).aspx

bool result = false || true && false; // --> false  // is the same result as  bool result = (false || true) && false; // --> false  // even though I know that the first statement is evaluated as   bool result = false || (true && false); // --> false  

So my question is how do you prove with code that AND (&&) has a higher precedence that OR (||)? If your answer is it doesn't matter, then why is it built that way in the language?

Answer by Max Schmeling for C# conditional AND (&&) OR (||) precedence


Wouldn't this get you what you're after? Or maybe I'm missing something...

bool result = true || false && false;  

Answer by Francis B. for C# conditional AND (&&) OR (||) precedence


Change the first false by true. I know it seems stupid to have (true || true) but it proves your point.

bool result = true || true && false;   // --> true        result = (true || true) && false; // --> false       result = true || (true && false); // --> true  

Answer by veggerby for C# conditional AND (&&) OR (||) precedence


false || true && true

Yields: true

false && true || true

Yields: true

Answer by EFraim for C# conditional AND (&&) OR (||) precedence


You don't prove it with code but with logic. AND is boolean multiplication whereas OR is boolean addition. Now which one has higher precedence?

Answer by John Rasch for C# conditional AND (&&) OR (||) precedence


If you really want to freak him out try:

bool result = True() | False() && False();    Console.WriteLine("-----");  Console.WriteLine(result);    static bool True()  {      Console.WriteLine(true);      return true;  }    static bool False()  {      Console.WriteLine(false);      return false;  }  

This will print:

True  False  False  -----  False  

Edit:

In response to the comment:

In C#, | is a logical operator that performs the same boolean logic as ||, but does not short-circuit. Also in C#, the | operator has a higher precedence than both || and &&.

By printing out the values, you can see that if I used the typical || operator, only the first True would be printed - followed by the result of the expression which would have been True also.

But because of the higher precedence of |, the true | false is evaluated first (resulting in true) and then that result is &&ed with false to yield false.

I wasn't trying to show the order of evaluation, just the fact that the right half of the | was evaluated period when it normally wouldn't be :)

Answer by James for C# conditional AND (&&) OR (||) precedence


You cannot just show the end result when your boolean expressions are being short-circuited. Here's a snippet that settles your case.

It relies on implementing & and | operators used by && and ||, as stated in MSDN 7.11 Conditional logical operators

public static void Test()  {      B t = new B(true);      B f = new B(false);        B result = f || t && f;        Console.WriteLine("-----");      Console.WriteLine(result);  }    public class B {      bool val;      public B(bool val) { this.val = val; }      public static bool operator true(B b) { return b.val; }      public static bool operator false(B b) { return !b.val; }      public static B operator &(B lhs, B rhs) {           Console.WriteLine(lhs.ToString() + " & " + rhs.ToString());          return new B(lhs.val & rhs.val);       }      public static B operator |(B lhs, B rhs) {           Console.WriteLine(lhs.ToString() + " | " + rhs.ToString());          return new B(lhs.val | rhs.val);       }      public override string ToString() {           return val.ToString();       }  }  

The output should show that && is evaluated first before ||.

True & False  False | False  -----  False  

For extra fun, try it with result = t || t && f and see what happens with short-circuiting.

Answer by Frank Schwieterman for C# conditional AND (&&) OR (||) precedence


We get into unnecessary coding arguments at my work all-the-time.

Well thats your problem. Being the right one is not as important as working together. The pragmatic solution is to use parenthesis to make it explicit.

Answer by abc for C# conditional AND (&&) OR (||) precedence


For detailed precedence refer: https://msdn.microsoft.com/en-us/library/2bxt6kc4.aspx


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.