62

I am a bit confused by the MSDN C# documentation which states that & and | are logical operators and that && and || are conditional operators.

I keep calling &&, || and ! logical operators, so I am wrong?

H. Pauwelyn
  • 229
  • 1
  • 16
John V
  • 4,898
  • 10
  • 47
  • 73
  • 4
    It does seem illogical, but there is an important difference between the two classes, and it's critical that you not assume that, say, `|` is interchangeable with `||`, even though in many cases they can be interchanged with no apparent change in program behavior. – Daniel R Hicks Nov 23 '17 at 01:36

3 Answers3

119

I am a bit confused by the MSDN C# documentation which states that & and | are logical operators and that && and || are conditional operators. I keep calling &&, || and ! logical operators, so I am wrong?

No; you're correct.

There are numerous small, mostly unimportant nomenclature errors in the MSDN documentation; I tried to get as many of them out as I could, but in cases where it is not egregiously wrong and misleading, it's not always a wise use of time. Go to the specification if you want a definitive statement about the name of a C# feature.

So: the relevant authority is the C# specification, which states in section 7.11:

The &, ^, and | operators are called the logical operators.

It then goes on to further break down the built-in logical operators into integer, enumeration, Boolean and nullable-Boolean logical operators. There are also user-defined logical operators; see the spec for details.

In section 7.12 we have

The && and || operators are called the conditional logical operators. They are also called the “short-circuiting” logical operators.

So all of them are logical operators. Some of them are conditional logical operators.

What makes the conditional logical operators conditional? One might make a specious guess that it is because they are typically used in conditional statements (if) or conditional expressions (? :). The real reason is given by the specification:

The && and || operators are conditional versions of the & and | operators: The operation x && y corresponds to the operation x & y, except that y is evaluated only if x is not false. The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is not true.

The conditional logical operators are thus named because the right hand operand is evaluated conditionally depending on the value of the left hand operand.

We can see this more vividly by noting that the conditional logical operators are just "syntactic sugars" for conditional expressions. x && y is simply a more pleasant way to write x ? y : false, and x || y is simply a more pleasant way to write x ? true : y. The conditional logical expressions are actually conditional expressions.

There is also a user-defined form of the conditional logical operator, and it is a little tricky. See the specification for details.

Further reading, if this subject interests you:

H. Pauwelyn
  • 229
  • 1
  • 16
Eric Lippert
  • 45,799
  • 22
  • 87
  • 126
  • So the fact that && cannot operate on bits has nothing to do with it? – Robert Harvey Nov 22 '17 at 17:57
  • 3
    @RobertHarvey: Right, the fact that & can operate on bools, integer types or enum types but && only operates on bools has nothing to do with the choice of naming one of them the "conditional" form of the operator. The conditional operator is conditional because it has a conditional branch in its evaluation semantics. – Eric Lippert Nov 22 '17 at 18:01
  • @RobertHarvey: You can see this by desugaring the operators into their conditional form. `x && y` is neither more nor less than a sugar for the conditional expression `x ? y : false`. – Eric Lippert Nov 22 '17 at 18:02
  • @EricLippert Thanks a lot. The version with "conditional logical operators" is retired, the one I can find on MSDN does not mention that. But your explanation makes perfect sense. – John V Nov 22 '17 at 18:06
  • @user970696: They're still called "conditional logical operators." See the draft C# 6 specification here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#conditional-logical-operators – Robert Harvey Nov 22 '17 at 18:09
  • 16
    I am under the impression the term "short-circuit operator" is far more popular (and probably less ambigous) than "conditional operator" in the described sense. – Doc Brown Nov 22 '17 at 19:15
  • 15
    @DocBrown: It is surely popular, but I've always found that name misleading; it seems to have been coined by someone who thought that a "short circuit" and a "short cut" to obtaining a result are the same thing. A short circuit is a dangerous fault which can quickly destroy an electrical system. We named "unsafe" blocks in C# "unsafe" because *they're dangerous if used incorrectly*; let's not give things cutesy but misleading value-laden names. Don't even get me started on the Elvis operator. :-) – Eric Lippert Nov 22 '17 at 19:26
  • 26
    @EricLippert: While "short circuit" may sound scary to the general public, I don't think K&R were confused about the actual definition. In electrical engineering, shorting a circuit isn't always a dangerous fault, in fact we do it intentionally all the time. It just means to cut out an unwanted part of the circuit by giving the electricity a shorter path to follow. – hackerb9 Nov 22 '17 at 22:44
  • `x&& y` is not exactly the same as `x ? y : false`. In particular, operator overloading makes `&&` a rather interesting beast: "The operation x && y is evaluated as T.false(x) ? x : T.&(x, y) where T.false(x) is an invocation of the operator false declared in T, and T.&(x, y) is an invocation of the selected operator in &." – Cort Ammon Nov 23 '17 at 15:57
  • 1
    @CortAmmon: You'll want to be reading the following paragraph where I call out that the user-defined operator semantics are slightly different and that you should see the spec for details. – Eric Lippert Nov 23 '17 at 16:37
  • 1
    @CortAmmon Operator overloading makes *anything* a rather interesting beast. – BJ Myers Nov 23 '17 at 17:41
  • Concerning the desugaring (but ignoring the short-circuiting), I actually find it very insightful to think of them as `x || y` := `x ? x : y` and `x && y` := `x ? y : x`. This is also how it works in weakly typed languages such as JS. – yoniLavi Nov 30 '17 at 01:05
27

In C# these are all logical operators.

int x = 0xABCD & 0xFF // x == 0xCD

&& and || are called "conditional logical operators" because they are short-circuiting.

bool someOtherCondition = true;
if (x == 0xEF && someOtherCondition) // someOtherCondition is not evaluated, 
                                     // because x == 0xEF is false

Note that this terminology differs from language to language. In C and C++ && and || are just Logical Operators. In Java, & and | are called Bitwise Operators, while C and C++ classifies them as Arithmetic Operators.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • So is it incorrect to call && a logical operator? As I can see a lot of other sources, including books, actually do that – John V Nov 22 '17 at 17:20
  • Thanks, that is funny, I quickly checked google books and basically not a single book distinguish them, they just call them logical operators. Maybe because they perform logical operations? (AND, OR, NOT, XOR) – John V Nov 22 '17 at 17:25
  • Ah, so I found that that they are actually called "conditional logical operators", so seems like everybody is right. And for C++, MSDN uses for them just logical operators...not really consistent – John V Nov 22 '17 at 17:34
  • The term "conditional logical operators"? When I googled that as a phrase, several books came up. But in the end, arent they doing logical operations? So why not call them logical operators, as you mention C and C++ do. – John V Nov 22 '17 at 17:45
  • Microsoft is the only authority that matters. See [here](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-or-operator): *"The conditional-OR operator* `||` *performs a logical-OR of its `bool` operands."* – Robert Harvey Nov 22 '17 at 17:46
  • The | and & operators operate on bools too. – Eric Lippert Nov 22 '17 at 17:47
  • @EricLippert: Yes, which is why I carefully chose my wording ("can" and "only"). – Robert Harvey Nov 22 '17 at 17:48
  • @EricLippert Exactly, & and && differ just in short-circuiting, when applied to bools. – John V Nov 22 '17 at 17:48
  • @RobertHarvey But for C++, Microsoft calls && LOGICAL OR. – John V Nov 22 '17 at 17:49
  • 1
    I already stated that in my answer. – Robert Harvey Nov 22 '17 at 17:49
  • 3
    Yes, Microsoft is the authority, but the authoritative document is the specification. See section 7.12, **Conditional Logical Operators**. – Eric Lippert Nov 22 '17 at 17:49
  • 8
    The moral of the story being: *it's more important to understand precisely what these operators do than to be precise about their names.* – Robert Harvey Nov 22 '17 at 17:50
  • @EricLippert I wonder why they left this nomenclature and leave out "logical" in the newer versions. – John V Nov 22 '17 at 17:53
  • @user970696: Does it really matter? I personally prefer the term "Conditional Operators." Why muddy the waters by adding more words that are not going to illuminate without reading the specification? – Robert Harvey Nov 22 '17 at 17:53
  • 1
    @user970696: There are many small nomenclature errors in the MSDN documentation; I attempted to correct them but I had many other pressing concerns. – Eric Lippert Nov 22 '17 at 17:53
  • @RobertHarvey Wel to me it does, I am trying to understand everything as much as I can :) As the difference between & and && is short circuiting, I still believe it is a logical operator, working with logical operands, – John V Nov 22 '17 at 17:54
  • 21
    Focus on what the operators do, and stop obsessing with the vocabulary. See also [Naming Considered Harmful](http://the-whiteboard.github.io/2017/11/21/naming-considered-harmful.html). – Robert Harvey Nov 22 '17 at 17:56
  • 4
    +1. Everything listed in the question is just _operators_ that take one or two expressions and evaluate to a value. The adjectives are unnecessary hair splitting. – Blrfl Nov 22 '17 at 18:00
  • 3
    @RobertHarvey [“Considered Harmful” Essays Considered Harmful](https://meyerweb.com/eric/comment/chech.html) – user11153 Nov 23 '17 at 13:43
  • 1
    @Blrfl no, its not hair splitting. The key is that someothercondition is not evaluated. if someothercondition is a function with side effects, it wont be called. Also constructions like p!=NULL && p->a are valid (and useful)expressions, while & would blow up. – lalala Nov 24 '17 at 07:36
  • @lalala Understood, but the adjectives don't change the fact that both are different operators with different semantics. If you wouldn't spend time debating whether `+` and `*` are additive vs. multiplicative or both are just arithmetic or they're just operators, arguing bitwise, non-short-circuiting vs. logical, short-circuiting is a similar waste of time. – Blrfl Nov 24 '17 at 14:05
  • @Blrfl actually short circuiting is quite unique (it only applies to 3 operators, if I am not wrong), and a lot of programmers are not aware of it, so in a sense even dropping this info (callng it a waste of time) is hiding this understanding further. I mean, e.g. is it obvious to you that they are *not* commutative? – lalala Nov 24 '17 at 14:21
  • @RobertHarvey while that article is pretty bad, it's not so bad as to suggest that we shouldn't have nomenclature for things that do actually need to be explained to people. – Jon Hanna Nov 25 '17 at 13:20
  • @lalala I'm _not_ proposing that information about how the operators behave be hidden, although programmers unaware of the behavior of the operators they're using are effectively doing that for themselves. I _am_ saying that the logical-vs.-conditional dichotomy in the question isn't meaningful. – Blrfl Nov 25 '17 at 15:49
  • I know this is old but it's worth noting that in Java `&` and `|` are logical operators i.e. they work on boolean values just without short-cutting. The are overloaded such that it's only when you use them with numeric primitives that they are bitwise operators. – JimmyJames Jul 09 '19 at 18:05
-2

The point is that & and | are bitwise operators, meaning they are applied to and yield bit string values. And bitwise is a very used term among programmers.

For instance 0xff & 0x00 == 0x00, while 0xff | 0x00 == 0xff.

And && and || are applied to conditions, and yield the usual values of conditions; i.e. true and false.

For instance true && false == false, while true || false == true.

Therefore && and || could be called conditional operators, even though that is not an usual term among programmers.

Of course, every C, C++, Java and C# programmer knows all that. But I guess that the misunderstood happens because "conditional operator" is not a term frequently used by us programmers.

  • 5
    _Of course, every C, C++, Java and C# programmer knows all that._ That's a very rude thing to write, implying the OP is stupid. Same by writing _us programmers_, you exclude the OP. Please don't do that. – DarkDust Nov 24 '17 at 13:56
  • 1
    I don't think that your *later* answer brings any added value *after* the Eric Lippert's accepted answer, and furthermore is incorrect in the meaning of not understanding the point of the question. – Honza Zidek Nov 24 '17 at 19:04
  • @DarkDust Every C, C++, Java and C# programmer should understand those operators. This is not being rude **but a fact**. – Phil1970 Nov 25 '17 at 14:16
  • 1
    @Phil1970: OP does seem to understand these operators, it's about clarification of the _naming_. In this light, and having emphasized the relevant terms in Hilton's answer, that sentence of his can be interpreted as meaning _every programmer knows about these naming details, but you don't_. This _is_ wrong (as can be seen in the discussions of the other answers) and its phrasing is rude. – DarkDust Nov 25 '17 at 15:51
  • 4
    Dear all, I'm sorry if my answer looked rude. English is not 1st language. In any case, I never intended to imply that the OP was not a programmer. Au contraire, I meant that he or she was confused by an unusual naming in the text he or she read. All that I tried to do was to clarify the unusual naming by program fragments. – Hilton Fernandes Nov 26 '17 at 23:28