Relational operator

In computer science, a relational operator is a programming language construct or operator that tests or defines 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)

Contents

Standard relational operators

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

Common relational operators
Convention equal to not equal to greater than less than greater than
or equal to
less than
or equal to
In print = > <
ALGOL 68 [note 1] = > <
/= >= <=
eq ne gt lt ge le
C-like[note 2] == != > < >= <=
BASIC-like[note 3] = <> > < >= <=
Mathematica[1] == != > < >= <=
Equal[x,y] Unequal[x,y] Greater[x,y] Less[x,y] GreaterEqual[x,y] LessEqual[x,y]
MATLAB[note 4] == ~= > < >= <=
eq(x,y) ne(x,y) gt(x,y) lt(x,y) ge(x,y) le(x,y)
Fortran [note 5] == /= > < >= <=
.EQ. .NE. .GT. .LT. .GE. .LE.
Bourne-like shells [note 6] -eq -ne -gt -lt -ge -le
MUMPS = '= > < '< '>
  1. ^ ALGOL 68: "stropping" regimes are used in code on platforms with limited character sets (e.g. use >= or GE instead of ), platforms with no bold[1] typeface (use 'ge'), or platforms with only UPPERCASE (use .GE or 'GE').
  2. ^ Including C, C++, C#, Go, Java, JavaScript, Perl (numerical comparison only), PHP, Python, Ruby, and R.
  3. ^ Including BASIC, Visual Basic .NET, VB.NET, Objective Caml, Pascal, SQL, and Standard ML.
  4. ^ 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. The first form is also used in Smalltalk, with the exception of equality, which is =.
  5. ^ The first form including Haskell.
  6. ^ Including Bourne shell, Bash, 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.

Other conventions are less common: Common Lisp and Macsyma/Maxima use Basic-like operators except for inequality, which is /= in Common Lisp and # in Macsyma/Maxima. Older Lisps used equal, greaterp, and lessp; and negated them using not for the remaining operators.

Equality

Confusion with assignment operators

Early FORTRAN (1956–57) was bounded by heavily restricted character sets where "=" was the only relational operator available. There were no "<" or ">" (and certainly no ≤ or ≥). This forced the designers to define symbols such as .GT., .LT., .GE., .EQ. etc. and subsequently made it tempting to use the remaining "=" character for copying, despite the obvious incoherence with mathematical usage (X=X+1 should be impossible).

International Algebraic Language and ALGOL (1958 and 1960) therefore introduced ":=" for assignment, leaving the standard "=" available for equality, a convention followed by CPL, Algol W, BCPL, Simula, Algol 68, SETL, Pascal, Smalltalk, Modula2, Ada, Standard ML, Objective Caml, Eiffel, Delphi, Oberon, Dylan, VHDL, and several other languages.

On the other hand, the now very influential language C started off as a minimal compiled language called B, which, in turn, started off as a simplified version of BCPL (a typeless version of CPL). The intented application for B was solely as a vehicle for a first port of (a then very primitive) UNIX. In what that has been described as a "strip-down" process, B replaced the original ":=" and "=" of BCPL by "=" and "==" respectively, the reason for this being unknown (and and or meanwhile became "&" and "|", and later "&&" and "||", respectively). As a small type system was later introduced, B became C. The popularity of C, and its association with UNIX, led to Java, C#, and other languages (including new versions of Fortran) following suit, syntactically, despite this unnecessary conflict with the mathematical meaning of the equal sign.

Languages

Assignments in C have a value and since any non-zero scalar value is interpreted as true in conditional expressions,[2] the code "if (x = y)" is legal, but has a very different meaning from "if (x == y)". The former code fragment means "assign y to x, and if the new value of x is not zero, execute the following statement". The latter fragment means "if and only if x is equal to y, execute the following statement".[3]

  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);
  }

Though Java and C# have the same operators as C, 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.

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, though there are some legitimate uses of an assignment inside an if-condition.

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).

Some programmers get in the habit of writing comparisons against a constant 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
ALGOL 68 a :=: b or a is b a = b when a and b are pointers
C, C++ a == b *a == *b when a and b are pointers
C# object.ReferenceEquals(a, b) a.Equals(b) The == operator defaults to ReferenceEquals, but can be overloaded to perform Equals instead.
Common Lisp (eq a b) (equal a b)
Go a == b reflect.DeepEqual(*a, *b) when a and b are pointers
Java a == b a.equals(b)
Objective Caml, Smalltalk a == b a = b
Pascal a^ = b^ a = b
Perl $a == $b $$a == $$b when $a and $b are references to scalars
PHP5 $a === $b $a == $b when $a and $b are objects
Python a is b a == b
Ruby a.equal?(b) a == b
Scheme (eq? a b) (equal? a b)
Visual Basic .NET [inequality 1] a Is b a = b
Objective-C a == b [a isEqual:b] when a and b are pointers to objects that are instances of NSObject
  1. ^ Patent application: On 14 May 2003, US application 20,040,230,959  "IS NOT OPERATOR" was filed for the ISNOT operator by employees of Microsoft. This patent was granted on 18 November 2004.

The === operator

The languages JavaScript and 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).[4] 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,[4] but "x === 0" is only true when x is 0. When comparing objects in PHP 5, the "==" operator tests for structural equality, while the "===" operator tests for physical equality.[5]

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)

See also

Notes and references

  1. ^ Relational and Logical Operators of Mathematica
  2. ^ A zero scalar value is interpreted as false while any non-zero scalar value is interpreted as true; this is typically used with integer types, similar to assembly language idioms.
  3. ^ Kernighan, Brian; Dennis Ritchie (1988) [1978]. The C Programming Language (Second edition ed.). Prentice Hall. , 19
  4. ^ a b "PHP: Comparison Operators - Manual". http://php.net/manual/en/language.operators.comparison.php. Retrieved 2008-07-31. 
  5. ^ http://www.php.net/manual/en/language.oop5.object-comparison.php

Wikimedia Foundation. 2010.

Look at other dictionaries:

  • relational operator — santykio operatorius statusas T sritis automatika atitikmenys: angl. relation operator; relational operator vok. Vergleichsoperator, m; Verhältnisoperator, m rus. оператор отношения, m pranc. opérateur de relation, m …   Automatikos terminų žodynas

  • Relational — may refer to: *Relational (mathematics) *Relational aggression *Relational algebra *Relational art *Relational database *Relational calculus *Relational operator *Relational model *Relational theory *Relational philosophy *Relational… …   Wikipedia

  • Operator (programming) — Programming languages generally support a set of operators that are similar to operations in mathematics. A language may contain a fixed number of built in operators (e.g. + * = in C and C++), or it may allow the creation of programmer defined… …   Wikipedia

  • Relational Model/Tasmania — (RM/T) was published by E.F. Codd in 1979 and is the name given to a number of extensions to his original relational model (RM) published in 1970. The overall goal of the RM/T was to define some fundamental semantic units, at atomic and molecular …   Wikipedia

  • Relational algebra — Not to be confused with Relation algebra. Relational algebra, an offshoot of first order logic (and of algebra of sets), deals with a set of finitary relations (see also relation (database)) that is closed under certain operators. These operators …   Wikipedia

  • Relational database — A visual diagram showing the relationship between the two tables, as indicated by the arrow A relational database matches data by using common characteristics found within the data set. The resulting groups of data uses the relational model (a… …   Wikipedia

  • Relational quantum mechanics — This article is intended for those already familiar with quantum mechanics and its attendant interpretational difficulties. Readers who are new to the subject may first want to read the introduction to quantum mechanics. Relational quantum… …   Wikipedia

  • Relational model — The relational model for database management is a database model based on first order predicate logic, first formulated and proposed in 1969 by Edgar Codd. [ Derivability, Redundancy, and Consistency of Relations Stored in Large Data Banks , E.F …   Wikipedia

  • Relational Database Management System — Eine relationale Datenbank dient zur elektronischen Datenverwaltung in Computersystemen und beruht auf dem relationalen Datenbankmodell. Dieses wurde 1970 von Edgar F. Codd erstmals vorgeschlagen und ist bis heute, trotz einiger Kritikpunkte, ein …   Deutsch Wikipedia

  • Relational Database Management Systems — Eine relationale Datenbank dient zur elektronischen Datenverwaltung in Computersystemen und beruht auf dem relationalen Datenbankmodell. Dieses wurde 1970 von Edgar F. Codd erstmals vorgeschlagen und ist bis heute, trotz einiger Kritikpunkte, ein …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”