evaluates expr n times.


evaluates expr with the variable i successively taking on the values 1 through imax (in steps of 1).


starts with i=imin.


uses steps di.


uses the successive values i1, i2, .


evaluates expr looping over different values of j etc. for each i.


  • Do uses the standard Wolfram Language iteration specification.
  • You can use Return, Break, Continue, and Throw inside Do.
  • Unless an explicit Return is used, the value returned by Do is Null.
  • Do[expr,Infinity] continues evaluating expr until explicitly told to exit through a function such as Break, Return, Throw, Abort, or Quit.
  • Do[expr,spec] 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.
  • Do[expr,spec1,spec2] is effectively equivalent to Do[Do[expr,spec2],spec1].


open allclose all

Basic Examples  (3)

Print the first four squares:

n goes from -3 to 5 in steps of 2:

Print 4 random integers:

Scope  (8)

Evaluate Print[x] 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:

Loop infinitely, until a condition is met:

Generalizations & Extensions  (1)

Give a list of values to iterate over:

Applications  (5)

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:

Properties & Relations  (2)

Equivalent loops:

Loop infinitely, until a condition is met:

Wolfram Research (1988), Do, Wolfram Language function, (updated 2015).


Wolfram Research (1988), Do, Wolfram Language function, (updated 2015).


@misc{reference.wolfram_2020_do, author="Wolfram Research", title="{Do}", year="2015", howpublished="\url{}", note=[Accessed: 02-March-2021 ]}


@online{reference.wolfram_2020_do, organization={Wolfram Research}, title={Do}, year={2015}, url={}, note=[Accessed: 02-March-2021 ]}


Wolfram Language. 1988. "Do." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015.


Wolfram Language. (1988). Do. Wolfram Language & System Documentation Center. Retrieved from