# 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:

  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)

## 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-like 1 BASIC-like 2 MATLAB 3 Fortran 4 Bourne-like shells 5 MUMPS
== = == 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.
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.

## Equality

### 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 if y is anything but 0*/
printf("x is %d and y is %d\n", x, y);
}

In Ada and Python, assignment operators cannot appear in an expression (including if 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 (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.

### 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:

Language Physical equality Structural equality Notes
C, C++ a == b *a == *b a and b are pointers
C# object.ReferenceEquals(a, b)1 a.Equals(b)1
Common Lisp (eq a b) (equal a b)
Java a == b a.equals(b) a and b are references
Ocaml a == b a = b
Pascal a^ = b^ a = b
Perl $a ==$b $$a ==$$b $a and $b are references to scalars
PHP5 $a ===$b $a ==$b $a and $b are objects
Python a is b</tt>  <code>a == b
Ruby a.equal?(b) a == b
Scheme (eq? a b) (equal? a b)
Visual Basic .NET a Is b a = b
C a == b [a isEqual:b] a and b are pointers to objects

1 In C#, the == operator defaults to ReferenceEquals, but can be overloaded to perform Equals 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, "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 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 0 to false), and that is not always what one wants[2], but "x === 0" is only true when x is 0.

## 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)$

• Binary relation
• Common operator notation
• Equals sign
• Equality (mathematics)
• Logical operator
• Operation
• Operator