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!
==Syntax== 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 Python will print the message if the ''x'' is less than ''y'': <syntaxhighlight lang="python"> if x < y: print("x is less than y in this example") </syntaxhighlight> Other programming languages, such as [[Lisp (programming language)|Lisp]], use [[prefix notation]], as follows: <syntaxhighlight lang="lisp"> (>= X Y) </syntaxhighlight> ===Operator chaining=== In mathematics, it is common practice to chain relational operators, such as in 3 < x < y < 20 (meaning 3 < x ''and'' x < y ''and'' y < 20). The syntax is clear since these relational operators in mathematics are transitive. However, many recent programming languages would see an expression like 3 < x < y as consisting of two left (or right-) associative operators, interpreting it as something like <code>(3 < x) < y</code>. If we say that x=4, we then get <code>(3 < 4) < y</code>, and evaluation will give <code>true < y</code> which generally does not make sense. However, it does compile in C/C++ and some other languages, yielding surprising result (as ''true'' would be represented by the number 1 here). It is possible to give the expression <code>x < y < z</code> its familiar mathematical meaning, and some programming languages such as Python and [[Raku (programming language)|Raku]] do that. Others, such as C# and Java, do not, partly because it would differ from the way most other infix operators work in C-like languages. The [[D programming language]] does not do that since it maintains some compatibility with C, and "Allowing C expressions but with subtly different semantics (albeit arguably in the right direction) would add more confusion than convenience".<ref>{{cite book |last=Alexandrescu |first=Andrei |title=The D Programming Language |year=2010 |publisher=Addison Wesley |page=58 |isbn=978-0-321-63536-5}}</ref> Some languages, like [[Common Lisp]], use multiple argument predicates for this. In Lisp <code>(<= 1 x 10)</code> is true when x is between 1 and 10. ===Confusion with assignment operators=== {{See also|Assignment (computer science)#Assignment versus equality}} Early FORTRAN (1956β57) was bounded by heavily restricted character sets where <code>=</code> was the only relational operator available. There were no <code><</code> or <code>></code> (and certainly no <code>β€</code> or <code>β₯</code>). This forced the designers to define symbols such as <code>.GT.</code>, <code>.LT.</code>, <code>.GE.</code>, <code>.EQ.</code> etc. and subsequently made it tempting to use the remaining <code>=</code> character for copying, despite the obvious incoherence with mathematical usage (<code>X=X+1</code> should be impossible). International Algebraic Language (IAL, [[ALGOL 58]]) and [[ALGOL]] (1958 and 1960) thus introduced <code>:=</code> for assignment, leaving the standard <code>=</code> available for equality, a convention followed by [[CPL (programming language)|CPL]], [[ALGOL W]], [[ALGOL 68]], Basic Combined Programming Language ([[BCPL]]), [[Simula]], SET Language ([[SETL]]), [[Pascal (programming language)|Pascal]], [[Smalltalk]], [[Modula-2]], [[Ada (programming language)|Ada]], [[Standard ML]], [[OCaml]], [[Eiffel (programming language)|Eiffel]], [[Object Pascal]] ([[Delphi (programming language)|Delphi]]), [[Oberon (programming language)|Oberon]], [[Dylan (programming language)|Dylan]], VHSIC Hardware Description Language ([[VHDL]]), and several other languages. ====B and C==== This uniform de facto standard among most programming languages was eventually changed, indirectly, by a minimalist compiled language named [[B (programming language)|B]]. Its sole intended application was as a vehicle for a first port of (a then very primitive) [[Unix]], but it also evolved into the very influential [[C (programming language)|C]] language. B started off as a syntactically changed variant of the systems programming language [[BCPL]], a simplified (and typeless) version of [[CPL (programming language)|CPL]]. In what has been described as a "strip-down" process, the <code>and</code> and <code>or</code> operators of BCPL<ref>Used not only in ALGOL-like languages, but also in FORTRAN and BASIC</ref> were replaced with <code>&</code> and <code>|</code> (which would later become <code>&&</code> and <code>||</code>, respectively.<ref>As some programmers were confused by the dual meanings (bitwise operator, and logical connective) of these new symbols (according to [[Dennis Ritchie]]). Only the bitwise meaning of & and | were kept.</ref>). In the same process, the ALGOL style <code>:=</code> of BCPL was replaced by <code>=</code> in B. The reason for all this being unknown.<ref>Although [[Dennis Ritchie]] has suggested that this may have had to do with "economy of typing" as updates of variables may be more frequent than comparisons in certain types of programs</ref> As variable updates had no special syntax in B (such as <code>let</code> or similar) and were allowed in expressions, this non standard meaning of the equal sign meant that the traditional semantics of the equal sign now had to be associated with another symbol. [[Ken Thompson]] used the ad hoc <code>==</code> combination for this. As a small type system was later introduced, B then became C. The popularity of this language along with its association with Unix, led to Java, C#, and many other languages following suit, syntactically, despite this needless conflict with the mathematical meaning of the equal sign. ====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)