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
Assignment (computer science)
(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!
==Assignment versus equality== {{See also|Relational operator#Confusion with assignment operators}} The use of the equals sign <code>=</code> as an assignment operator has been frequently criticized, due to the conflict with equals as comparison for equality. This results both in confusion by novices in writing code, and confusion even by experienced programmers in reading code. The use of equals for assignment dates back to [[Heinz Rutishauser]]'s language [[Superplan]], designed from 1949 to 1951, and was particularly popularized by Fortran: {{blockquote|A notorious example for a bad idea was the choice of the equal sign to denote assignment. It goes back to Fortran in 1957{{efn|1=Use of <code>=</code> predates Fortran, though it was popularized by Fortran.}} and has blindly been copied by armies of language designers. Why is it a bad idea? Because it overthrows a century old tradition to let <nowiki>β=β</nowiki> denote a comparison for equality, a predicate which is either true or false. But Fortran made it to mean assignment, the enforcing of equality. In this case, the operands are on unequal footing: The left operand (a variable) is to be made equal to the right operand (an expression). <nowiki>x = y</nowiki> does not mean the same thing as <nowiki>y = x</nowiki>.<ref>{{cite web |citeseerx=10.1.1.88.8309 |author=Niklaus Wirth |title=Good Ideas, Through the Looking Glass |url=https://citeseerx.ist.psu.edu/doc/10.1.1.88.8309 }}</ref>|[[Niklaus Wirth]]|''Good Ideas, Through the Looking Glass''}} Beginning programmers sometimes confuse assignment with the [[relational operator]] for equality, as "=" means [[equality (mathematics)|equality]] in mathematics, and is used for assignment in many languages. But assignment alters the value of a variable, while equality testing tests whether two expressions have the same value. In some languages, such as [[BASIC]], a single equals sign (<code>"="</code>) is used for both the assignment operator and the equality relational operator, with context determining which is meant. Other languages use different symbols for the two operators.<ref>{{Cite web |date=2013-06-01 |title=C++ Programming Language. Basics |url=https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp1_Basics.html#zz-3.6 |access-date=2024-06-21 |website=ntu.edu.sg}}</ref> For example: * In [[ALGOL]] and [[Pascal (programming language)|Pascal]], the assignment operator is a colon and an equals sign (<code>":="</code>) while the equality operator is a single equals (<code>"="</code>). * In [[C (programming language)|C]], the assignment operator is a single equals sign (<code>"="</code>) while the equality operator is a pair of equals signs (<code>"=="</code>). * In [[R (programming language)|R]], the assignment operator is basically <code><-</code>, as in <code>x <- value</code>, but a single equals sign can be used in certain contexts. The similarity in the two symbols can lead to errors if the programmer forgets which form ("<code>=</code>", "<code>==</code>", "<code>:=</code>") is appropriate, or mistypes "<code>=</code>" when "<code>==</code>" was intended. This is a common programming problem with languages such as C (including one famous attempt to backdoor the Linux kernel),<ref>{{cite web |author=Corbet |date=6 November 2003 |title=An attempt to backdoor the kernel |url=https://lwn.net/Articles/57135/ |access-date=2024-06-21 |website=lwn.net}}</ref> where the assignment operator also returns the value assigned (in the same way that a function returns a value), and can be validly nested inside expressions. If the intention was to compare two values in an <code>if</code> statement, for instance, an assignment is quite likely to return a value interpretable as Boolean true, in which case the <code>then</code> clause will be executed, leading the program to behave unexpectedly. Some language processors (such as [[GNU Compiler Collection|gcc]]) can detect such situations, and warn the programmer of the potential error.<ref>{{Cite web |title=Static Analyzer Options (Using the GNU Compiler Collection (GCC)) |url=https://gcc.gnu.org/onlinedocs/gcc/Static-Analyzer-Options.html |access-date=2024-06-21 |website=gcc.gnu.org}}</ref><ref>{{Cite web |last=Deitel |first=Paul |last2=Deitel |first2=Harvey |date=2022-10-25 |title=C++ Control Statements, Part 2 |url=https://domyassignments.com/ |access-date=2024-06-21 |website=Domyassignments}}</ref>
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)