In all the programming languages I have ever used, the comparator for testing a mathematical equivalence is ==. For example, in python:

if 7 == 8 - 1:

Why are all, as far as I am aware, programming languages designed like this? Wouldn't it make so much more sense to have only one = sign? I understand that = is used for defining and assigning variable values, as in

foo = bar

But surely the compiler can differentiate from an if statement (from the if tagged to the front of it) and a variable assignment (due to the lack of an if tagged to the front of it)? Is it a trend dating back to the FORTRAN days? Why hasn't it been changed?

Honestly, I can't see any logical reason to use == instead of =, when we surely have the computing power to use the = operator in two different ways rather than have two different operators. I'm sick of running into petty compiler errors - who thought of this stupid convention?

  • 16
    This wouldn't work in most C-like languages, because you can assign inside of a condition. – Blacklight Shining Apr 19 '15 at 12:54
  • 29
    The premise of this question is quite fundamentally flawed. Pascal-based languages such as Pascal itself, Ada, Delphi, ..., all use a single = sign for equality comparison (whereas assignments are expressed by :=). And while the Pascal family might not currently be very fashionable and it is well possible to only work in C-based languages and Python, Pascal-based languages are probably nonetheless far from being "esoteric niche languages" in a way that one could say == is globally the "standard equality operator", or assuming that it applies to "all" programming languages, – O. R. Mapper Apr 19 '15 at 13:42
  • 4
    GML, the scripting language used by Game Maker actually permitted the use of = in both contexts in the way you're suggesting (sort of). In my opinion, this was something of a disaster. For example, the code a = b = c was perfectly valid, but you had to understand that this isn't multiple assignment (as most people thought it was), but really more equivalent to a = b == c. Having one representation of two different operators is confusing. I won't argue that using = for assignment is good, but having separate operators is. – Kyle Apr 19 '15 at 16:43
  • 5
    In Visual Basic, there is no ==. The single = is used for assignment and comparison. – nhgrif Apr 19 '15 at 18:02
  • 6
    I'm voting to close this question because the basic premise is wrong. – Vitaly Mijiritsky Apr 20 '15 at 5:03

I have a feeling this question might be moved to the Stackoverflow site but Its an interesting question.

The reason behind this was because Fortran introduced the concept of using "=" as assigning values from one variable to another which led to a lot of confusion about what to use as an equality operator. To quote this wikipedia article.

The use of the equals sign = 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:

A notorious example for a bad idea was the choice of the equal sign to denote assignment. It goes back to Fortran in 1957[a] 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 “=” 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). x = y does not mean the same thing as y = x.

Niklaus Wirth, Good Ideas, Through the Looking Glass*

The history of how this led to == being used as the comparative operator is given below from this wikipedia article

Early FORTRAN (1956–57) was bounded by heavily restricted character sets where "=" was the only relational operator available. There were no "<" or ">" (and certainly no ≤ or ≥). This forced the designers to define symbols such as .GT., .LT., .GE., .EQ. etc. and subsequently made it tempting to use the remaining "=" character for copying, despite the obvious incoherence with mathematical usage (X=X+1 should be impossible).

International Algebraic Language and ALGOL (1958 and 1960) therefore introduced ":=" for assignment, leaving the standard "=" available for equality, a convention followed by CPL, Algol W, BCPL, Simula, Algol 68, SETL, Pascal, Smalltalk, Modula2, Ada, Standard ML, OCaml, Eiffel, Delphi, Oberon, Dylan, VHDL, and several other languages.

On the other hand, the now very influential language C started off as a minimal compiled language called B, which, in turn, started off as a simplified version of BCPL (a typeless version of CPL). The intended application for B was solely as a vehicle for a first port of (a then very primitive) UNIX. In what has been described as a "strip-down" process, B replaced the original ":=" and "=" of BCPL by "=" and "==" respectively, the reason for this being unknown (and and or meanwhile became "&" and "|", and later "&&" and "||", respectively). As a small type system was later introduced, B became C. The popularity of C, and its association with UNIX, led to Java, C#, and other languages (including new versions of Fortran) following suit, syntactically, despite this unnecessary conflict with the mathematical meaning of the equal sign.

  • 2
    "B replaced the original ":=" and "=" of BCPL by "=" and "==" respectively, the reason for this being unknown" ... so ... we dont know? in any case, good answer, plenty of research. also, this can't be moved to stackoverflow.com - it is about language architecture, which isnt suited for SO, CR, Programmers, SF or SU, which leads me to here. – blaizor Apr 19 '15 at 7:48
  • Here are a couple of quotes from Dennis Ritchie, the designer of C. "B ... is BCPL squeezed into 8K bytes of memory and filtered through [Ken] Thompson's brain." "Other flddles in the transition from BCPL to B were introduced as a matter of taste, and some remain controversial, for example the decision to use the single character = for assignment instead of :=." [Source: Dennis M. Ritchie: "The development of the C language", Sigplan Notices, Vol 28, #3, March 1993.] – Theodore Norvell Apr 19 '15 at 16:16
  • 1
    One of Holt's design guidelines for programming languages is "usage shortens form". I.e. more frequently used constructs should have short expressions. So we can speculate that Thompson preferred a one character operator for the very common operation of assignment at the price of a two character operator for the less common operation of assignment. In Holt's own language TURING, the = is used for equals and the := for assignment; this is in keeping with another of Holt's guidelines, which is not to contradict cultural expectations. TURING's main users would be familiar with = from algebra. – Theodore Norvell Apr 19 '15 at 16:37

Checking equality is not used exclusively in if statements. For example:

are_equal = foo == bar

If the syntax would allow for using single equals sign, such assignment would be ambiguous:

are_equal = foo = bar

This could either mean "compare foo and bar, and assign the result to are_equal", or "assign bar value to both are_equal and foo".

  • 1
    There is an easy workaround to this problem: disallow fall-through assignment of the form a=b=0. – Federico Poloni Apr 19 '15 at 16:46
  • 1
    @FedericoPoloni I wouldn't consider that an acceptable work-around, since it essentially disallows the assignment of generic Boolean expressions. Sure, you could design the language in such a way that other operators could be allowed, like a = b > 0, but at that point I think the workaround stops being easy. The real annoying part of it from a parsing perspective is now you have two different kinds of Boolean expressions: those that are in if statements (or other flow control structures) and those that are in assignments. – Kyle Apr 19 '15 at 18:58
  • @Kyle Probably I wasn't clear enough; I was suggesting for a = b = 0 to be parsed as a = (b==0), not a = 0; b = 0. Double assignments are far less common. – Federico Poloni Apr 19 '15 at 19:04
  • @FedericoPoloni Ahh, my mistake then. I would consider that to be acceptable (barring just having two different operators). But, yeah, multiple assignment is far less common (and I actually find them to be less readable, so I don't typically use them even if I can). – Kyle Apr 19 '15 at 19:11
  • A more convincing example, maybe, is when passing the result of an equality test to a function. Indeed, I think use of both f(a = b) and f(a == b) is relatively common in C-like languages. – James Wood Apr 20 '15 at 8:58

Why different operators, instead of letting the compiler figure it out? I think perhaps some of you have been spoiled by multi-GHz processors with multiple GBytes of RAM. Remember that C was designed in the 1970s, FORTRAN in the '50s, and consider the capabilities of the machines of the day. If you want your compiles to finish within a practical amount if time, you don't give it complicated work to do.

As to why = for assignment, == for comparison, I'm just guessing but I think it might have something to do with patterns. After all, you have a number of comparison operators, not just one. E.g. C's ==, !=, >=, or FORTRAN's .eq., .ne., .gt.. == or .eq. fits into this pattern, leaving = free to be used for assignment. (Besides, as has already been noted, one tends to write more assignments than equality comparisons, so a single-character assignment means a bit less typing.)

  • 5
    GW-BASIC was published in '83 and used = for both — I don't think "spoiled by multi-GHz processors" applies at all. – Nathan Tuggy Apr 19 '15 at 21:14
  • @Nathan Tuggy: But you need to remember that BASIC was never really intended for serious programming. The name itself tells you that: it's an acronym for "Beginner's All-purpose Symbolic Instruction Code". Also, IIRC BASIC didn't use a plain = for assignment. Rather, assignment was indicated by the LET keyword, e.g. "LET X = 1", so a simple parser could easily distinguish the two uses. – jamesqf Apr 20 '15 at 3:57
  • 3
    I said GW-BASIC for a reason, as neither of your counterarguments applies to it: it most certainly was intended and distributed for actual computing (however simplified for the masses) and that's what it was used for in practice (yes, even on fairly limited hardware with <<16 kB available), and GW-BASIC had already made LET optional. – Nathan Tuggy Apr 20 '15 at 4:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.