Comparison operator
From Wikipedia, the free encyclopedia
A comparison operator in computer programming is a binary operator. The application of the operator usually returns a Boolean value indicating whether the comparison holds.
Contents |
[edit] Equality operator
An equality operator is one type of comparison operator usually represented as either =
(ex. BASIC) or ==
(ex. C, C++, and Java). The meaning of equality often depends on the programming language and on the values being compared. In addition, an equality operator does not always meet the requirements of an equivalence relation, especially in programming languages which allow operator overloading.
[edit] Inequality operators
inequality operators are another type of comparison operator. There are usually five inequality operators <
, <=
, >
, >=
, and !=
. As with equality operators, the comparison usually, but does not always meet the requirements of a total order (for non-strict inequalities) or a strict weak ordering (for strict inequalities), especially in programming languages which allow operator overloading.
[edit] <
The operator <
(called "less than") returns true if its first operand evaluates to less than the second operand, and returns false otherwise.
[edit] <=
The operator <=
(called "less than or equal to") returns true if its first operand evaluates to less than the second operand or if the operands are equal, it returns false otherwise.
[edit] >
The operator >
(called "greater than") returns true if its first operand is greater than its second, and returns false otherwise.
[edit] >=
The operator >=
(called "greater than or equal to") returns true if its first operand is greater than its second or if the two operands are equal, and returns false otherwise.
[edit] !=
The operator !=
(called "not equal") returns true if its operands are not equal and false otherwise. This operand appears in some languages as <>.
[edit] Logical equivalence
Though perhaps not obvious at first, like the boolean logical operators XOR, AND, OR, and NOT, the comparison operators have logical equivalence such that they can all be defined in terms of one another.
(x < y) == (y > x) == not(x >= y) == not(y <= x)
(y < x) == (x > y) == not(y >= x) == not(x <= y)