The are two operators '<>' and '!=' used in programming languages with the same general meaning.

'<>' can be used in PHP, SQL, and Pascal.

'!=' seems to be more commonly used.

What is the original reason for both of these operators to co-exist, when they seem to be so similar in purpose? What was the first language to support both?

有帮助吗?

解决方案

The != operator is more commonly used nowadays because of the overwhelming influence in C. But how did C get there?

The mathematical operator for inequality is . Some languages do use this operator directly as part of their syntax, e.g. APL or ALGOL. Since mathematical negation is ¬, the ALGOL W dialect uses ¬=. Whether such symbols were supported depended on the character set and keyboard of the computer system. Since many languages were defined in a more mathematical notation that didn't precisely match the actual representation of the source code, this bit of computer archeology is a bit difficult.

To write programs on systems without these symbols other graphs were used. Note that 7-bit ASCII was in no way universal for a long time, so the ASCII character set was not in itself a constraint or a reservoir of possible characters for language designers.

ALGOL supported negation as /= which is visually very close to the mathematical operator. (As an aside, the backslash character \ was implemented so that ALGOL could write logical operators like /\ and \/.) Many languages supported keywords that could be used in place of operators, such as .NE. (Fortran), .NE (6-bit ALGOL) or NE or NEQ.

C's ancestors are B and BCPL. These are typeless languages: everything is a machine word. They don't discern between logical and bitwise operations. In BCPL bitwise negation was ~ and pointer-dereference was !foo. There was no ASCII-like symbol for not-equals. In B, pointer-dereference had become *foo and negation and not-equals were ! and != respectively. This convention was later kept in C. I am not sure why B changed this, but B was quite shortlived and subject to much syntactic experimentation before C evolved out of it (roughly during 1971–1973).

Since then C turned out to be incredibly popular, and it is a major influence on other languages. Most newer languages see no reason to use any unexpected operators, and borrow != from C.

It seems that <> was mainly used by Pascal, SQL, and BASIC since it indicates “less than or greater than”, i.e. “not equal”. However, I can't find any good references. The reports for Pascal 73 and SEQUEL 74 only seem to mention . The Pascal standard ISO 7185:1990 does use <> but of course doesn't explain any history. I don't know what was used in the 1983 version of the standard.

Interesting documents read while researching this answer, ordered chronologically:

其他提示

The different representations of the inequality operator is largely the result of historical reasons. The very earliest languages were developed in relative isolation compared to what we see today and their syntax was chosen for very weak reasons and often arbitrary reasons such as the personal preference of the language architect/designer. There was lot more variation in representation in early programming languages.

Since programming languages develop within families, for example Java, C# and C++, share a similar syntax to C which came from B. The popularity of each style of inequality operator is largely a result of the popularity of the language family as a whole, rather than any specific feature or advantage.

https://en.wikipedia.org/wiki/Relational_operator#Standard_relational_operators

As far as I know many languages based their choice of inequality operator on the background of the creators, those with a primarily mathematical background tended to use <> or approximated the ≠ symbol with /= while those with a logic, electronic or semantics background tended towards !=, if I recall correctly there was even one that had == but no inequality operator for a while you had to use NOT(a == b).

The earliest language that I personally am familiar with that permits both <> and != is Python which allows the use of both <> and != and also has the identity operators is and is not

For a few years now, most people use floating point arithmetic according to the IEEE 754 standard. In this standard, there is an exceptional value NaN (Not a Number) which compares as not equal to anything else, including itself.

The == and != operator make totally sense: == yields false and != yields true if either operand is NaN.

<> on the other hand could be expected to yield true if the left side is less than or greater than the right side; with NaNs involved that would mean the operands are not equal and not NaNs.

许可以下: CC-BY-SA归因
scroll top