The variable assignment operator in Pascal. Examples of usage include:

  • number1 := 765;
  • letter1 := 'Z';
  • money := 32.579;
  • sum := 5 + number1;

I find this amazingly annoying to type. What's wrong with a simple =?


dmd: I understand what you are saying, and I agree with it too. It's just that := is difficult to type after using = for so long in other languages. That's what I have a problem with.

What's wrong with a simple =  [doing double duty as both a comparison and assignment operator]? The same thing that's wrong with it in C, Java, Perl, and, for that matter, the large majority of languages with the notable exception of BASIC.

In C, the equality and comparison operators are  ==  and  = , respectively. This tends to cause trouble for novice and experienced programmers alike:

/*
   Example of equality-comparison confusion in C.
   Note that the same operator is used in both cases,
   once correctly, once incorrectly.
*/

if (defcon = 666)
{
    launch_missile();
    launches = launches + 1;
}
Clearly the intended purpose of this code is to check for a certain defcon, and, finding it, execute the launch_missile() function. However, the programmer used =, the assignment operator, not  == , the comparison operator. Thus, the if-test assigned a value of 666 to defcon. Because the value of the act of assignment is the value it assigned, the if-test evaluated as 666 (which, being nonzero, is interpreted as 'true')... and thus the function gets called regardless of the initial value of the test variable.

Pascal, for all its faults, appears to make the more rational choice in operator typography. Consider the same code in Pascal:

(*
   Example of equality-comparison confusion in Pascal.
   Note that the same operator is used in both cases,
   once correctly, once incorrectly.
*)

if defcon = 666 then
  begin
    launch_missile;
    launches = launches + 1
  end;
This code launches the missile only if defcon is indeed 666. It does not properly increment the launches variable, but it would seem that this is a minor problem compared to the alternative. (Furthermore, the compiler will catch that particular error.)

We are taught as early as first grade that  =  is the symbol for equality, and it makes more sense to make assignment the exception rather than the rule.


N-Wing points out that in Java, the test would fail because you're attempting to assign an integer to a boolean. However, the problem would return if one were testing a boolean variable. On the other hand, how often do you check booleans for equality? Normally, you would just do if(z) or if(!z).

In FORTRAN (IBM, 1956), = denotes assignment, and equality is denoted by .EQ..

In ALGOL (a largely European design-by-committee effort, 1960), := denotes assignment, and = denotes equality.

Which seems more natural to you depends on which languages you write most fluently. Pascal, being an Algol derivative, uses the Algol convention. C and Perl use Fortran-like conventions.


It's worth noting that John Backus, one of the principal designers of both Fortran and Algol, later came to the conviction that assignment should be banished from programming languages altogether; he founded the branch of programming languages known as applicative or functional languages.

Also used in mathematics to indicate a definition rather than a deduction - e.g. we might define a polynomial f by writing

f(x) := x2

and if we later wanted to note the derivative of f we would write

f'(x) = 2x

Note that often a plain "=" is used instead of ":=" when we are clearly talking about a definition, and many people don't bother to use ":=" at all.

Log in or register to write something here or to contact authors.