remove "C does not include [in]equality as a set of relational operators" -- my S. Prata book of C says these are "Relational operators" |
merge content from == article into Equality section |
||
Line 1: | Line 1: | ||
{{Mergefrom|eqeq|date=July 2007}} |
|||
In [[computer science]] a '''relational operator''' is a [[programming language]] construct that tests some kind of [[relation (mathematics)|relation]] between two entities, for example, ''[[equality (mathematics)|equality]]'' or ''[[greater than]]''. These operators usually return true or false, depending on whether the relationship between the two [[operands]] holds or not. An [[expression (programming)|expression]] created using a relational operator forms what is known as a ''relational expression'' or a ''condition''. |
In [[computer science]] a '''relational operator''' is a [[programming language]] construct that tests some kind of [[relation (mathematics)|relation]] between two entities, for example, ''[[equality (mathematics)|equality]]'' or ''[[greater than]]''. These operators usually return true or false, depending on whether the relationship between the two [[operands]] holds or not. An [[expression (programming)|expression]] created using a relational operator forms what is known as a ''relational expression'' or a ''condition''. |
||
{{TOCRight}} |
|||
⚫ | 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). |
||
⚫ | |||
⚫ | |||
<pre> |
|||
if (x == y) { |
|||
printf("x is equal to y\n"); |
|||
} |
|||
</pre> |
|||
Other programming languages, such as [[Lisp programming language|Lisp]], use [[Prefix notation|prefix]] notation, as follows: |
|||
<pre> |
|||
⚫ | |||
</pre> |
|||
Here are some of the most common relational operators in use in programming languages: |
Here are some of the most common relational operators in use in programming languages: |
||
Line 42: | Line 50: | ||
| align="center" | <code>!<</code> || ''Not less than''. Used in [[SQL]]. |
| align="center" | <code>!<</code> || ''Not less than''. Used in [[SQL]]. |
||
|} |
|} |
||
==Equality== |
|||
In languages that use "<code>==</code>" to test for equality, the "<code>==</code>" operator is distinct from the "<code>=</code>" operator, the latter being used for [[assignment (computer science)|assignment]]. Languages that use this style include all C-style languages, such as [[C (programming language)|C]], [[Java (programming language)|Java]], and [[PHP]]. |
|||
The language [[PHP]] extends this syntax, with the "<code>==</code>" operator able to return true if two values are equal, even if they have different types (for example, "<code>4 == "4"</code>" is true), and the "<code>===</code>" operator returning true only if two values are equal and have equivalent types as well (such that "<code>4 === "4"</code>" is false but "<code>4 === 4</code>" is true).<ref name="php">[http://uk.php.net/manual/en/language.operators.comparison.php PHP Manual: Comparison Operators]</ref> |
|||
===Pitfalls=== |
|||
The similarity in appearance between "==" and "=" in these languages can lead to [[software bug|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". 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.<ref name="kandr" /> |
|||
<pre> |
|||
int x = 1; |
|||
int y = 2; |
|||
if (x = y) { |
|||
/* This code will always execute */ |
|||
printf("x is %d and y is %d\n", x, y); |
|||
} |
|||
</pre> |
|||
For this reason, many non-C style languages, such as [[Eiffel (programming language)|Eiffel]], [[Pascal programming language|Pascal]] and [[Ada programming language|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 <code>=</code> as <code>:=</code>. |
|||
In Ada and [[Python (programming language)|Python]] assignment operator cannot appear in an [[expression (programming)|expression]] (including <code>if</code> clauses), thus precluding this class of error. Some compilers, such as [[GNU Compiler Collection|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 <code>=</code> 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 also write comparisons in the reverse of the usual order, as in <code>2 == a</code>. If the programmer accidentally uses <code>=</code>, 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. |
|||
==Uses outside of programming== |
==Uses outside of programming== |
Revision as of 02:11, 19 July 2007
In computer science a relational operator is a programming language construct that tests some kind of relation between two entities, for example, equality or greater than. These operators usually return true or false, depending on whether the relationship between the two operands holds or not. An expression created using a relational operator forms what is known as a relational expression or a condition.
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 in C:
if (x == y) { printf("x is equal to y\n"); }
Other programming languages, such as Lisp, use prefix notation, as follows:
(>= X Y)
Here are some of the most common relational operators in use in programming languages:
Relational Operator | Meaning |
---|---|
== |
Equality or identity. Used to test the equivalence of two expressions or the identity of two objects. |
=== |
Variant of identity. In some languages (e.g. PHP), represents a stronger sense of equality than == . In PHP, the result of $a === $b is TRUE if $a is equal to $b , and they are of the same type[1].
|
= |
Variant of equality. (Also an assignment operator in C-like languages.) |
<> |
Inequality. (Also a file-search specifier 'operator' in Perl.) |
!= |
Variant of inequality. Used to test the equivalence of two expressions. |
/= |
Variant of inequality. This is not as common as the above operator. |
^= |
Variant of inequality. This is not as common as the above operator. (Also a bitwise XOR assignment in C-like languages.) |
> |
Greater than. Used to test if the value of the left expression is greater than that of the right expression. |
< |
Less than. Used to test if the value of the left expression is less than that of the right expression. |
>= |
Greater than or equal to. Used to test if the value of the left expression is greater than or equal to that of the right expression. |
<= |
Less than or equal to. Used to test if the value of the left expression is less than or equal to that of the right expression. |
=< |
Variant of less than or equal to. |
!> |
Not greater than. Used in SQL. |
!< |
Not less than. Used in SQL. |
Equality
In languages that use "==
" to test for equality, 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 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).[2]
Pitfalls
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". 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.[3]
int x = 1; int y = 2; if (x = y) { /* This code will always execute */ printf("x is %d and y is %d\n", x, y); }
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 =
as :=
.
In Ada and Python assignment operator 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 also write comparisons in the reverse of the usual order, as in 2 == a
. 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.
Uses outside of programming
Relational operators are sometimes used in communities of technical nature instead of words.
See also
References
- ^ PHP Manual: Comparison Operators
- ^ PHP Manual: Comparison Operators
- ^ Cite error: The named reference
kandr
was invoked but never defined (see the help page).