This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
 Documentation / Mathematica / Built-in Functions / Programming / Flow Control  /
Do

  • Do[ expr , imax ] evaluates expr imax times.
  • Do[ expr , i , imax ] evaluates expr with the variable i successively taking on the values 1 through imax (in steps of 1).
  • Do[ expr , i , imin , imax ] starts with i = imin. Do[ expr , i , imin , imax , di ] uses steps di.
  • Do[ expr , i , imin , imax , j , jmin , jmax , ... ] evaluates expr looping over different values of j, etc. for each i.
  • Do uses the standard Mathematica iteration specification.
  • Do evaluates its arguments in a non-standard way (see Section¬†A.4.2).
  • You can use Return, Break, Continue and Throw inside Do.
  • Unless an explicit Return is used, the value returned by Do is Null.
  • See the Mathematica book: Section 1.7.3,¬†Section 2.5.9.
  • See also: For, While, Table, Nest, Fold.

    Further Examples

    Here is a simple Do loop to add the first 1000 integers.

    In[1]:=

    In[2]:=

    In[3]:=

    Out[3]=

    In[4]:=

    This loop creates a series of graphics that can be animated. Double-click on the graphic to start the animation, or select the cell brackets containing the graphics and choose Animate Selected Graphics in the Cell menu.

    Evaluate the cell to see the graphic.

    In[5]:=