Repetitive Operations

In using the Wolfram Language, you sometimes need to repeat an operation many times. There are many ways to do this. Often the most natural is in fact to set up a structure such as a list with many elements, and then apply your operation to each of the elements.

Another approach is to use the Wolfram Language function (RefLink[Do, paclet:ref/Do]), which works much like the iteration constructs in languages such as C and Fortran. (RefLink[Do, paclet:ref/Do]) uses the same Wolfram Language iterator notation as (RefLink[Sum, paclet:ref/Sum]) and (RefLink[Product, paclet:ref/Product]), described in "Sums and Products".

Implementing repetitive operations.

This prints out the values of the first five factorials:

Do[Print[i!],{i,5}]

It is often more useful to have a list of results, which you can then manipulate further:

Table[i!,{i,5}]

If you do not give an iteration variable, the Wolfram Language simply repeats the operation you have specified, without changing anything:

r=1;Do[r=1/(1+r),{100}];r