Relational operators are also used in technical literature instead of words. Relational operators are usually written in
infix notation, if supported by the programming language, which means that they appear between their operands (the two expressions being related). For example, an expression in Python will print the message if the
x is less than
y: if x Other programming languages, such as
Lisp, use
prefix notation, as follows: (>= X Y)
Operator chaining In mathematics, it is common practice to chain relational operators, such as in 3 (3 . If we say that x=4, we then get (3 , and evaluation will give true which generally does not make sense. However, it does compile in C/C++ and some other languages, yielding surprising result (as
true would be represented by the number 1 here). It is possible to give the expression x its familiar mathematical meaning, and some programming languages such as Python and
Raku do that. Others, such as C# and Java, do not, partly because it would differ from the way most other infix operators work in C-like languages. The
D programming language does not do that since it maintains some compatibility with C, and "Allowing C expressions but with subtly different semantics (albeit arguably in the right direction) would add more confusion than convenience". Some languages, like
Common Lisp, use multiple argument predicates for this. In Lisp ( is true when x is between 1 and 10.
Confusion with assignment operators Early FORTRAN (1956–57) was bounded by heavily restricted character sets where = was the only relational operator available. There were no or > (and certainly no ≤ or ≥). This forced the designers to define symbols such as .GT., .LT., .GE., .EQ. etc. and subsequently made it tempting to use the remaining = character for copying, despite the obvious incoherence with mathematical usage (X=X+1 should be impossible). International Algebraic Language (IAL,
ALGOL 58) and
ALGOL (1958 and 1960) thus introduced := for assignment, leaving the standard = available for equality, a convention followed by
CPL,
ALGOL W,
ALGOL 68, Basic Combined Programming Language (
BCPL),
Simula, SET Language (
SETL),
Pascal,
Smalltalk,
Modula-2,
Ada,
Standard ML,
OCaml,
Eiffel,
Object Pascal (
Delphi),
Oberon,
Dylan, VHSIC Hardware Description Language (
VHDL), and several other languages.
B and C This uniform de facto standard among most programming languages was eventually changed, indirectly, by a minimalist compiled language named
B. Its sole intended application was as a vehicle for a first port of (a then very primitive)
Unix, but it also evolved into the very influential
C language. B started off as a syntactically changed variant of the systems programming language
BCPL, a simplified (and typeless) version of
CPL. In what has been described as a "strip-down" process, the and and or operators of BCPL were replaced with & and | (which would later become && and ||, respectively.). In the same process, the ALGOL style := of BCPL was replaced by = in B. The reason for all this being unknown. As variable updates had no special syntax in B (such as let or similar) and were allowed in expressions, this non standard meaning of the equal sign meant that the traditional semantics of the equal sign now had to be associated with another symbol.
Ken Thompson used the ad hoc == combination for this. As a small type system was later introduced, B then became C. The popularity of this language along with its association with Unix, led to Java, C#, and many other languages following suit, syntactically, despite this needless conflict with the mathematical meaning of the equal sign.
Languages Assignments in C have a
value and since any non-zero scalar value is interpreted as
true in
conditional expressions, the code if (x = y) is legal, but has a very different meaning from if (x == y). The former code fragment means "assign
y to
x, and if the new value of
x is not zero, execute the following statement". The latter fragment means "
if and only if x is equal to
y, execute the following statement". int x = 1; int y = 2; if (x = y) { // This code will always execute if y is anything but 0 printf("x is %d and y is %d\n", x, y); } Though
Java and
C# have the same operators as C, this mistake usually causes a compile error in these languages instead, because the if-condition must be of type boolean, and there is no implicit way to convert from other types (
e.g., numbers) into booleans. So unless the variable that is assigned to has type boolean (or wrapper type Boolean), there will be a compile error. In ALGOL-like languages such as Pascal, Delphi, and Ada (in the sense that they allow
nested function definitions), and in
Python, and many functional languages, among others, assignment operators cannot appear in an
expression (including if clauses), thus precluding this class of error. Some compilers, such as
GNU Compiler Collection (GCC), provide a warning when compiling code containing an assignment operator inside an if statement, though there are some legitimate uses of an assignment inside an if-condition. In such cases, the assignment must be wrapped in an extra pair of parentheses explicitly, to avoid the warning. Similarly, some languages, such as
BASIC use just the = symbol for both assignment
and equality, as they are syntactically separate (as with Pascal, Ada, Python, etc., assignment operators cannot appear in expressions). Some programmers get in the habit of writing comparisons against a constant in the reverse of the usual order: // Mistaken use of = versus == would be a compile-time error if (2 == a) { // ... } If = is used accidentally, the resulting code is invalid because 2 is not a variable. The compiler will generate an error message, on which the proper operator can be substituted. This coding style is termed left-hand comparison, or
Yoda conditions. This table lists the different mechanisms to test for these two types of equality in various languages: Ruby uses a
b to mean "b is a member of the set a", though the details of what it means to be a member vary considerably depending on the data types involved. is here known as the "case equality" or "case subsumption" operator. ==See also==