Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Relational operator
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Languages==== Assignments in C have a [[value (programming)|value]] and since any non-zero scalar value is interpreted as ''true'' in [[Conditional (programming)|conditional expression]]s,<ref>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.</ref> the code <code>if (x = y)</code> is legal, but has a very different meaning from <code>if (x == y)</code>. 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".<ref name="kandr">{{cite book |title=The C Programming Language |author=Brian Kernighan and Dennis Ritchie |publisher=Prentice Hall |orig-year=1978 |year=1988 |edition=Second}}, 19</ref> <syntaxhighlight lang="c"> 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); } </syntaxhighlight> Though [[Java (programming language)|Java]] and [[C Sharp (programming language)|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 <code>boolean</code>, and there is no implicit way to convert from other types (''e.g.'', numbers) into <code>boolean</code>s. So unless the variable that is assigned to has type <code>boolean</code> (or wrapper type <code>Boolean</code>), there will be a compile error. In ALGOL-like languages such as Pascal, Delphi, and Ada (in the sense that they allow [[nested function definition]]s), and in [[Python (programming language)|Python]], and many functional languages, among others, assignment operators cannot appear in an [[expression (programming)|expression]] (including <code>if</code> clauses), thus precluding this class of error. Some compilers, such as [[GNU Compiler Collection]] (GCC), provide a warning when compiling code containing an assignment operator inside an if statement, though there are some legitimate uses of an assignment inside an if-condition. In such cases, the assignment must be wrapped in an extra pair of parentheses explicitly, to avoid the warning. Similarly, some languages, such as [[BASIC]] use just the <code>=</code> symbol for both assignment ''and'' equality, as they are syntactically separate (as with Pascal, Ada, Python, etc., 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: <syntaxhighlight lang="c"> if (2 == a) { /* Mistaken use of = versus == would be a compile-time error */ } </syntaxhighlight> If <code>=</code> is used accidentally, the resulting code is invalid because 2 is not a variable. The compiler will generate an error message, on which the proper operator can be substituted. This coding style is termed left-hand comparison, or [[Yoda conditions]]. This table lists the different mechanisms to test for these two types of equality in various languages: {| class="wikitable" |- ! Language !! Physical equality !! Structural equality !! Notes |- | [[ALGOL 68]] || <code>a :=: b</code> ''or'' <code>a ''is'' b</code> || <code>a = b</code> || when <code>a</code> and <code>b</code> are pointers |- | [[C (programming language)|C]], [[C++]] || <code>a == b</code> || <code>*a == *b</code> || when <code>a</code> and <code>b</code> are pointers |- | [[C Sharp (programming language)|C#]] || {{nowrap|<code>object.ReferenceEquals(a, b)</code>}} || <code>a.Equals(b)</code> || The <code>==</code> operator defaults to <code>ReferenceEquals</code>, but can be [[Operator overloading|overloaded]] to perform <code>Equals</code> instead. |- | [[Common Lisp]] || <code>(eq a b)</code> || <code>(equal a b)</code> || |- |[[Erlang (programming language)|Erlang]] |<code>a =:= b</code> |<code>a == b</code> |when a and b are numbers |- | [[Go (programming language)|Go]] || <code>a == b</code> || {{nowrap|<code>reflect.DeepEqual(*a, *b)</code>}} || when a and b are pointers |- | [[Java (programming language)|Java]] || <code>a == b</code> || <code>a.equals(b)</code> || |- | [[JavaScript]] || <code>a === b</code> || <code>a == b</code> || when a and b are two string objects containing equivalent characters, the === operator will still return true. |- | [[OCaml]], [[Smalltalk]] || <code>a == b</code> || <code>a = b</code> || |- | [[Pascal (programming language)|Pascal]] || <code>a^ = b^</code> ||<code>a = b</code> || |- | [[Perl]] || <code>$a == $b</code> || <code>$$a == $$b</code> || when <code>$a</code> and <code>$b</code> are references to scalars |- | [[PHP]] || <code>$a === $b</code> || <code>$a == $b</code> || when <code>$a</code> and <code>$b</code> are objects |- | [[Python (programming language)|Python]] || <code>a is b</code> || <code>a == b</code> || |- | [[Ruby (programming language)|Ruby]] || <code>a.equal?(b)</code> || <code>a == b</code> || |- | [[Scheme (programming language)|Scheme]] || <code>(eq? a b)</code> || <code>(equal? a b)</code> || |- | [[Swift (programming language)|Swift]] || <code>a === b</code> || <code>a == b</code> || when a and b have class type |- | [[Visual Basic .NET]]<ref group="inequality">Patent application: On May 14, 2003, {{US patent application|20040230959}} "IS NOT OPERATOR" was filed for the <code>ISNOT</code> operator by employees of [[Microsoft]]. This patent was granted on November 18, 2004.</ref>|| <code>a Is b</code> or <code>object.ReferenceEquals(a, b)</code> || <code>a = b</code> or <code>a.Equals(b)</code> || Same as C# |- | [[Objective-C]] ([[Cocoa (API)|Cocoa]], [[GNUstep]]) || <code>a == b</code> || <code>[a isEqual:b]</code> || when <code>a</code> and <code>b</code> are pointers to objects that are instances of <code>NSObject</code> |} {{Reflist|group="inequality"}} Ruby uses <code>a === b</code> to mean "b is a member of the set a", though the details of what it means to be a member vary considerably depending on the data types involved. <code>===</code> is here known as the "case equality" or "case subsumption" operator.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)