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:
The standard numerical relational operators used in programming languages are shown below.
|In programming languages||In print||Meaning||Used to:|
| || || || || || || ||=||equal to||Test the equivalence of two values.|
| || || || || || || ||≠||not equal to||Test the negated equivalence of two values.|
| || || || || || || ||>||greater than||Test if the value of the left value is greater than that of the right.|
| || || || || || || ||<||less than||Test if the value of the left expression is less than that of the right.|
| || || || || || || ||≥||greater than or equal to||Test if the value of the left expression is greater than or equal to that of the right.|
| || || || || || || ||≤||less than or equal to||Test if the value of the left expression is less than or equal to that of the right.|
MATLAB, although in other respects using similar syntax as C, does not use
! in MATLAB sends the following text as a command line to the operating system.
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 "
if (x = y)", having intended "
if (x == y)". 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 "
if (x = y)" assigns y to x, making both equal to 2, and then executes the following code because 2 is not zero.
For this reason, many non-C style languages, such as Eiffel, Pascal and Ada use "
=" for equality testing and "
:=" for assignment. These two syntactic elements are more easily distinguishable, and thus it is easier to spot situations where an assignment has been used mistakenly in place of an equality test. It is also harder to mistype
In Ada and Python, assignment operators cannot appear in an expression (including
if clauses), thus precluding this class of error. Some compilers, such as GCC, will provide a warning when compiling code that contains an assignment operator inside an if statement.
Similarly, some languages, such as BASIC, 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). While this cannot cause the above error, it can still be confusing to have the same symbol mean two very different things.
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.
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, "
4 == "4"" is true), and the "
===" operator returning true only if two values are equal and have equivalent types as well (such that "
4 === "4"" is false but "
4 === 4" is true). This comes in handy when checking if a value is assigned the value of 0, since "
x == 0" is true for x being
0, but also for x being
"0" (i.e. a string containing the character 0) and
false (as PHP, like other languages, equates
false), and that is not always what one wants, but "
x === 0" is only true when x is
US Patent Issued to Oracle International on Oct. 23 for "Dynamically Sharing a Subtree of Operators in a Data Stream Management System Operating on Existing Queries" (American, Indian Inventors)
Oct 25, 2012; ALEXANDRIA, Va., Oct. 25 -- United States Patent no. 8,296,316, issued on Oct. 23, was assigned to Oracle International Corp....