我在工作中经常会陷入不必要的编码争论。今天,我问了一下条件AND(&&)或OR(||)哪个优先级更高。我的一个同事坚称它们的优先级相同,我有疑虑,于是我查了一下。
根据MSDN文档,AND(&&)比OR(||)的优先级更高。但是,你能向怀疑的同事证明吗?
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
我的问题是如何用代码证明 AND(&&) 的优先级高于 OR(||)?如果你的答案是它并不重要,那么为什么语言中要这样构建呢?