Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Core Language > Procedural Programming > Looping Constructs > Do >

Do

Do
evaluates expr times.
Do
evaluates expr with the variable i successively taking on the values through (in steps of ).
Do
starts with .
Do
uses steps di.
Do
uses the successive values , , ....
Do
evaluates expr looping over different values of j, etc. for each i.
  • Do uses the standard Mathematica iteration specification.
  • Unless an explicit Return is used, the value returned by Do is Null.
  • Do first evaluates spec, then localizes the variable specified and successively assigns values to it, each time evaluating expr.
  • Do effectively uses Block to localize values or variables.
  • In Do is effectively equivalent to Do.
Print the first four squares:
n goes from to in steps of :
Print the first four squares:
 
n goes from to in steps of :
Evaluate Print twice:
The step can be negative:
The values can be symbolic:
Loop over i and j, with j running up to i-1:
The body can be a procedure:
Break breaks out of Do:
Continue continues the loop, without executing the rest of the body:
Give a list of values to iterate over:
Generate a symbolic continued fraction:
Build up a list of primes:
Alternatively use Reap and Sow:
Implement backsubstitution:
Implement LU triangular decomposition of matrices:
The selection sort algorithm:
Equivalent loops:
New in 1 | Last modified in 6
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team
Format:   HTML  |  CDF