Standard relational operators
The standard numerical relational operators used in programming languages are shown below.
In programming languages | In print | Meaning | Used to: | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
C-like 1 | BASIC-like 2 | Mathematica[1] | MATLAB 3 | Fortran 4 | Bourne-like shells 5 | MUMPS | ||||||
== |
= |
== |
Equal[x,y] |
== |
eq(x,y) |
== |
.EQ. |
-eq |
= |
= | equal to | Test the equivalence of two values. |
!= |
<> |
!= |
Unequal[x,y] |
~= |
ne(x,y) |
/= |
.NE. |
-ne |
'= |
≠ | not equal to | Test the negated equivalence of two values. |
> |
> |
> |
Greater[x,y] |
> |
gt(x,y) |
> |
.GT. |
-gt |
> |
> | greater than | Test if the value of the left expression is greater than that of the right. |
< |
< |
< |
Less[x,y] |
< |
lt(x,y) |
< |
.LT. |
-lt |
< |
< | less than | Test if the value of the left expression is less than that of the right. |
>= |
>= |
>= |
GreaterEqual[x,y] |
>= |
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. |
<= |
<= |
<= |
LessEqual[x,y] |
<= |
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#, Go, Java, JavaScript, Perl (numerical comparison only), PHP, Python, and Ruby.
- Note (2): Including BASIC, Visual Basic .NET, VB.NET, Objective Caml, 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, 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.
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 |
Objective Caml | a == b |
a = b |
|
Pascal | a^ = b^ |
a = b |
|
Perl | $a == $b |
$$a == $$b |
$a and $b are references to scalars |
PHP5 | N/A | $a == $b |
$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 | a Is b |
a = b |
|
Objective-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.
Posted by 홍반장