Relational operator

From Wikipedia, the free encyclopedia

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). These operators usually return true or false, depending on whether the conditional 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 also used in technical literature instead of words.

Contents

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:

if (x < y) {
    printf("x is less than y in this example\n");
}

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

(>= X Y)

[edit] Standard relational operators

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

Common relational operators
In programming languages In print Meaning Used to:
C-style MATLAB Fortran BASIC Windows
PowerShell
== == eq(x,y) == .EQ. = -eq = equal to Test the equivalence of two values.
!= ~= ne(x,y) /= .NE. <> -ne not equal to Test the negated equivalence of two values.
> > gt(x,y) > .GT. > -gt > greater than Test if the value of the left value is greater than that of the right.
< < lt(x,y) < .LT. < -lt < less than Test if the value of the left expression is less than that of the right.
>= >= ge(x,y) >= .GE. >= -ge greater than or equal to Test if the value of the left expression is greater than or equal to that of the right.
<= <= le(x,y) <= .LE. <= -le 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 !=, as ! in MATLAB sends the following text as a command line to the operating system.

[edit] Equality

[edit] 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 "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.[1]

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 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 (2 == a) {   /* Mistaken use of = versus == would be a compile-time error */
}

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.

[edit] Identity

In some object-oriented programming languages, the equality operator is also used to test the identity of two objects.

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] This comes in handy when a value is assigned the value of 0, since "x == 0" is always false (as PHP, like other languages, equate 0 to false), but "x === 0" is true, as 0 is a numeric value.

[edit] 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) == not(x >= y) == not(y <= x)

[edit] See also

[edit] References

  1. ^ Kernighan, Brian; Dennis Ritchie [1978] (1988). The C Programming Language, Second edition, Prentice Hall. , 19
  2. ^ PHP Manual: Comparison Operators
Languages