Relational operator

From Wikipedia, the free encyclopedia

(Redirected from ==)
Jump to: navigation, search

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)

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
== == eq(x,y) == .EQ. = = equal to Test the equivalence of two values.
!= ~= ne(x,y) /= .NE. <> not equal to Test the negated equivalence of two values.
> > gt(x,y) > .GT. > > greater than Test if the value of the left value is greater than that of the right.
< < lt(x,y) < .LT. < < less than Test if the value of the left expression is less than that of the right.
>= >= ge(x,y) >= .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. <= 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.

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

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]

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)

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


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.