[Tutor] Re: Why x+=y instead of x=x+y?

Andres Rosado arosado@softhome.net
Sat, 20 Jul 2002 19:41:29 -0400


At 07:43 AM 7/19/2002 -0400, you wrote:
>While reading your post on "Loopy Questions" I noticed these lines:
>
> > =09x +=3D 0.01
> >     x +=3D step
>
>Now I know this is shorthand for "x =x+0.01" and "x =x+step", but I
>  have
>always wondered what the point is - surely it is not too much to ask for
>to type that extra character, especially because the resulting version is
>
>not clear to newbies (why put the "+" _before_ the "=", for starters -
>"x
>=+ step" would make more sense).

One point here is what you mean with it. If you remember correctly, there 
are two self-increment operators in C, ++x and x++. The way the ++ are 
written have a meaning, which goes first, the addition or the assigment.

a = ++x
x--
b = x++
a != b /*The result is this.*/

>If we want to save space, then "cnt"
>instead of "continue" or "prt" instead of "print" might be a better place
>
>to start.

It's not about saving space only. See below.

>Or, to put it differently: Is this just another example of a construct th
>at
>was included in Python to make the "Waaah, I wanna keep my C style, even
>if it is bizarre and non-intuitive"-crowd happy, or is there a real
>difference between "x +=y" and "x = x+y" that justifies a notation th
>at
>looks like a typo?
>
>Y, Scot

On prehistoric times, when the dinosaurs ruled the earth and DOS wasn't 
even invented, programmers had to use every single trick to optimize their 
programs. It happens that inside most microprocessor, x = x + y is:

temp = x
add x, y
x = temp

Which are 3 instructions. So, language definitions also included the +=, 
-=, /= and *= to speed this process. Nowadays, with processors so powerful, 
there is no need for these operators, but by tradition are still kept.


-----------------------------------
Andres Rosado
Email: andresr@despammed.com
ICQ: 66750646
Homepage: http://andres980.tripod.com/

Reality -- what a concept!
                 -- Robin Williams