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?
3 Answers 3
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 operationx && y
corresponds to the operationx & y
, except thaty
is evaluated only ifx
is not false. The operationx || y
corresponds to the operationx | y
, except thaty
is evaluated only ifx
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– Eric Lippert2017年11月22日 18:01:21 +00:00Commented Nov 22, 2017 at 18:01
-
16I 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– Doc Brown2017年11月22日 19:15:45 +00:00Commented Nov 22, 2017 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– Eric Lippert2017年11月22日 19:26:24 +00:00Commented Nov 22, 2017 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– hackerb92017年11月22日 22:44:09 +00:00Commented Nov 22, 2017 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– Eric Lippert2017年11月23日 16:37:05 +00:00Commented Nov 23, 2017 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.
-
3Yes, Microsoft is the authority, but the authoritative document is the specification. See section 7.12, Conditional Logical Operators.Eric Lippert– Eric Lippert2017年11月22日 17:49:42 +00:00Commented Nov 22, 2017 at 17:49
-
8The 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– Robert Harvey2017年11月22日 17:50:26 +00:00Commented Nov 22, 2017 at 17:50
-
21Focus on what the operators do, and stop obsessing with the vocabulary. See also Naming Considered Harmful.Robert Harvey– Robert Harvey2017年11月22日 17:56:30 +00:00Commented Nov 22, 2017 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– Blrfl2017年11月22日 18:00:52 +00:00Commented Nov 22, 2017 at 18:00
-
3@RobertHarvey "Considered Harmful" Essays Considered Harmfuluser11153– user111532017年11月23日 13:43:48 +00:00Commented Nov 23, 2017 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.
-
5Of 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– DarkDust2017年11月24日 13:56:24 +00:00Commented Nov 24, 2017 at 13:56
-
1I 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– Honza Zidek2017年11月24日 19:04:35 +00:00Commented Nov 24, 2017 at 19:04
-
@DarkDust Every C, C++, Java and C# programmer should understand those operators. This is not being rude but a fact.Phil1970– Phil19702017年11月25日 14:16:48 +00:00Commented Nov 25, 2017 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– DarkDust2017年11月25日 15:51:50 +00:00Commented Nov 25, 2017 at 15:51
-
4Dear 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– Hilton Fernandes2017年11月26日 23:28:34 +00:00Commented Nov 26, 2017 at 23:28
|
is interchangeable with||
, even though in many cases they can be interchanged with no apparent change in program behavior.