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?

  • 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 1:36

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:

  • 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
  • 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
  • 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

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.

  • 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
  • 21
    Focus on what the operators do, and stop obsessing with the vocabulary. See also Naming Considered Harmful. – 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 – user11153 Nov 23 '17 at 13:43

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

protected by gnat Nov 24 '17 at 4:44

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.