[comp.lang.misc] Comparison operators

magnus@THEP.LU.SE (Magnus Olsson) (07/30/90)

In article <25681@cs.yale.edu>, zenith@cs.yale.edu writes:
>a := b	means "assign the value of b to a".
>a = b	means "a is equal to b".
>
>The use of := distinguishes assignment from equality, thus prevents
>overloading a single operator and IMHO is a much nicer solution to the
>C hack == used to ovecome the same problem.

The people behind C *deliberately* changed the Algol operators
'=' and ':=' to '==' and '=' to save keystrokes - in most programs,
assignment is done much more often than comparison for equality, so,
quite in line with the philosophy behind C, they chose the shorter form
for the most common operation.

IMHO, using '=' for assignment isn't too bad - after all, that's the way
you do it in Fortran. The *real* drawback is that it's just too easy
to write
if (y=0) printf ("Division by zero"); else z=x/y;
instead of
if (y==0) ...
and get a quite unexpected result...

[For those who don't know C: The assignment 'y=0' is a perfectly legal
logical operator, with the value 0 (i.e. FALSE). So the code will
first assign 0 to y, then proceed to divide x by y...]

If you want to see a *really* ugly solution, have a look at Fortran,
where the comparison operators are written .EQ., .GT. and so on.

BTW, does anyone know the reason for this? In Algol and C, you *must*
have different operators, since e.g. the Algol statements

a := b := 1;  and  a := b = 1; 

mean two entirely different things ("assign 1 to both a and b" and 
"assign the logical result of the comparison b=1 to a", respectively).


Magnus Olsson		     	| \e+ 	   /_	      
Dept. of Theoretical Physics 	|  \  Z	  / q	      
University of Lund, Sweden	|   >----<	      
Internet: magnus@thep.lu.se	|  /	  \===== g    
Bitnet: THEPMO@SELDC52 		| /e-	   \q