This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Special Forms of Assignment

Particularly when you write procedural programs in Mathematica, you will often need to modify the value of a particular variable repeatedly. You can always do this by constructing the new value and explicitly performing an assignment such as . Mathematica, however, provides special notations for incrementing the values of variables, and for some other common cases.
i++increment the value of i by
i--decrement i
++ipre-increment i
--ipre-decrement i
i+=diadd di to the value of i
i-=disubtract di from i
x*=cmultiply x by c
x/=cdivide x by c

Modifying values of variables.

This assigns the value to the variable .
In[1]:=
Click for copyable input
Out[1]=
This increments the value of by .
In[2]:=
Click for copyable input
Out[2]=
The value of has been modified.
In[3]:=
Click for copyable input
Out[3]=
This sets to , multiplies its value by , then gives the final value of .
In[4]:=
Click for copyable input
Out[4]=
The value of is the value of before the increment is done.
The value of is the value of after the increment.
x=y=valueassign the same value to both x and y
{x,y}={value1,value2}assign different values to x and y
{x,y}={y,x}interchange the values of x and y

Assigning values to several variables at a time.

This assigns the value to and to .
In[7]:=
Click for copyable input
Out[7]=
This interchanges the values of and .
In[8]:=
Click for copyable input
Out[8]=
Now has value .
In[9]:=
Click for copyable input
Out[9]=
And has value .
In[10]:=
Click for copyable input
Out[10]=
You can use assignments to lists to permute values of variables in any way.
In[11]:=
Click for copyable input
Out[11]=
When you write programs in Mathematica, you will sometimes find it convenient to take a list, and successively add elements to it. You can do this using the functions PrependTo and AppendTo.
PrependTo[v,elem]prepend elem to the value of v
AppendTo[v,elem]append elem
v={v,elem}make a nested list containing elem

Assignments for modifying lists.

This assigns the value of to be the list .
In[12]:=
Click for copyable input
Out[12]=
This appends the element to the value of .
In[13]:=
Click for copyable input
Out[13]=
Now the value of has been modified.
In[14]:=
Click for copyable input
Out[14]=
Although AppendTo is always equivalent to v=Append[v, elem], it is often a convenient notation. However, you should realize that because of the way Mathematica stores lists, it is usually less efficient to add a sequence of elements to a particular list than to create a nested structure that consists, for example, of lists of length 2 at each level. When you have built up such a structure, you can always reduce it to a single list using Flatten.
This sets up a nested list structure for .
In[15]:=
Click for copyable input
Out[15]=
You can use Flatten to unravel the structure.
In[16]:=
Click for copyable input
Out[16]=