6

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?

7
  • 8
    "In many programming languages, the two operators '<>' and '!=' are interchangeable." Are they? Most I know, there is either one or the other.
    – Euphoric
    Sep 15, 2017 at 15:28
  • 1
    I've only seen <> and != together in SQL. Are there other languages that do it?
    – Blrfl
    Sep 15, 2017 at 15:30
  • @Blrfl PHP, see: php.net/manual/en/language.operators.comparison.php
    – yannis
    Sep 15, 2017 at 15:32
  • 8
    PHP is the special child. Source: been programming in PHP for a long time.
    – Andy
    Sep 15, 2017 at 15:34
  • 1
    @Blrfl Python 2 has both != and <>, which behave identically. (Python 3 only has !=). Both 2 and 3 also have is not, which behaves differently. Sep 15, 2017 at 15:47

4 Answers 4

18

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:

3

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

0

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

0

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.

3
  • That's not why we have two different operators. Jul 20, 2019 at 15:03
  • IEEE NaNs are completely tangential to this question and, no, they do not make sense. == is false even if both sides are the same NaN bit pattern, which violates the Law of Identity: that a thing A is itself: A is A.
    – Kaz
    Jun 16, 2021 at 8:07
  • <> is just an operator spelling; it doesn't literally mean "less than or greater than".
    – Kaz
    Jun 16, 2021 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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