Relational operator

In computer science a relational operator is a programming language construct or operator that tests some kind of relation between two entities. These include numerical equality (e.g., 5 = 5) and inequalities (e.g., 4 ≥ 3). In programming languages that include a distinct boolean data type in their type system, like Java, these operators return true or false, depending on whether the conditional relationship between the two operands holds or not. In other languages such as C, relational operators return the integers 0 or 1.

An expression created using a relational operator forms what is known as a relational expression or a condition. 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 C will print the message if the x is less than y:

Other programming languages, such as Lisp, use prefix notation, as follows:

Standard relational operators
The standard numerical relational operators used in programming languages are shown below.


 * Note (1): Including C, C++, C#, Java, JavaScript, Perl (numerical comparison only), PHP, Python, and Ruby.
 * Note (2): Including C, OCaml, Pascal, SQL, and Standard ML.
 * Note (3): MATLAB, although in other respects using similar syntax as C, does not use, as   in MATLAB sends the following text as a command line to the operating system.
 * Note (4): The first form including Haskell.
 * Note (5): Including Bourne shell, Korn shell, and Windows PowerShell. The symbols " " and " " are usually used in a shell for redirection, so other symbols need to be used. Without the hyphen, is used in Perl for string comparison.

Confusion with assignment operators
In languages that use " " to test for equality (usually C-like languages), the " " operator is distinct from the " " operator, the latter being used for assignment. Languages that use this style include all C-style languages, such as C, Java, and PHP.

The similarity in appearance between " " and " " in these languages can lead to coding errors. A programmer may mistype " ", having intended " ". In C, the former code fragment roughly means "assign y to x, and if the new value of x is not zero, execute the statement following". The latter code fragment roughly means "if and only if x is equal to y, execute the statement following". (Even though Java and C# have the same operators, 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.) For example, the following code should print "x is 2 and y is 2" because " " assigns y to x, making both equal to 2, and then executes the following code because 2 is not zero.

In Ada and Python, assignment operators cannot appear in an expression (including  clauses), thus precluding this class of error. Some compilers, such as C, will provide a warning when compiling code that contains an assignment operator inside an if statement.

Similarly, some languages, such as C, use just the " " symbol for both assignment and equality, as they are syntactically separate (as with Ada and Python, assignment operators cannot appear in expressions).

Some programmers get in the habit of writing comparisons in the reverse of the usual order:

If the programmer accidentally uses, the resulting code is invalid because 2 is not a variable. The compiler will generate an error message, upon which the proper operator can be substituted. This coding style is known as left-hand comparison.

Object identity vs. Content equality
In many modern programming languages, objects and data structures are accessed through references. In such languages, there becomes a need to test for two different types of equality:
 * Physical (or shallow) equality - whether two references reference the same object.
 * Structural (or deep) equality - whether the objects referenced by two references are equivalent in some sense (e.g. their contents are the same).

The first type of equality usually implies the second (except for things like NaN which are unequal to themselves), but the converse is not necessarily true. For example, two string objects may be distinct objects (unequal in the first sense) but contain the same sequence of characters (equal in the second sense). See identity for more of this issue.

The following table lists the different mechanisms to test for these two types of equality in various languages: 1 In C#, the  operator defaults to , but can be overloaded to perform   instead. This allows to test for structural equality wherever it's more intuitive, most notably in string comparison.

Other
The language PHP extends this syntax, with the " " operator able to return true if two values are equal, even if they have different types (for example, " " is true), and the " " operator returning true only if two values are equal and have equivalent types as well (such that " " is false but " " is true). This comes in handy when checking if a value is assigned the value of 0, since " " is true for x being, but also for x being   (i.e. a string containing the character 0) and   (as PHP, like other languages, equates   to  ), and that is not always what one wants , but " " is only true when x is.

Logical equivalence
Though perhaps not obvious at first, like the boolean logical operators XOR, AND, OR, and NOT, relational operators can be designed to have logical equivalence, such that they can all be defined in terms of one another. The following four conditional statements all have the same logical equivalence:
 * $$x < y$$
 * $$y > x$$
 * $$\neg (x \geq y)$$
 * $$\neg (y \leq x)$$