Do[expr,n]
evaluates expr n 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,{i1,i2,…}}]
uses the successive values i1, i2, ….
Do[expr,{i,imin,imax},{j,jmin,jmax},…]
evaluates expr looping over different values of j etc. for each i.
Do
Do[expr,n]
evaluates expr n 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,{i1,i2,…}}]
uses the successive values i1, i2, ….
Do[expr,{i,imin,imax},{j,jmin,jmax},…]
evaluates expr looping over different values of j etc. for each i.
Details
- 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].
- Parallelize[Do[expr,iter]] or ParallelDo[expr,iter] computes Do[expr,iter] in parallel on all subkernels. »
Examples
open all close allBasic Examples (3)
Scope (8)
Evaluate Print[x] twice:
Do[Print[x], {2}]Do[Print[i], {i, 10, 8, -1}]Do[Print[n], {n, x, x + 3y, y}]Loop over i and j, with j running up to i-1:
Do[Print[{i, j}], {i, 4}, {j, i - 1}]t = 67;Do[Print[t];t = Floor[t / 2], {3}]t = 1;Do[t *= k;Print[t];If[t > 19, Break[]], {k, 10}]Continue continues the loop, without executing the rest of the body:
t = 1;Do[t *= k;Print[t];If[k < 2, Continue[]];t += 2, {k, 5}]Loop infinitely, until a condition is met:
Do[If[k ^ 2 > 100, Return[k]], {k, ∞}]Generalizations & Extensions (2)
Give a list of values to iterate over:
Do[Print[k], {k, {a, b, c, d}}]ParallelDo computes Do in parallel:
ParallelDo[Print[i];i, {i, 4}]Do can be parallelized automatically, effectively using ParallelDo:
Parallelize[Do[Print[i];i, {i, 4}]]Applications (5)
Generate a symbolic continued fraction:
t = x;Do[t = 1 / (1 + t), {5}];tNest[1 / (1 + #)&, x, 5]t = {};Do[If[PrimeQ[2 ^ n - 1], AppendTo[t, n]], {n, 100}];tAlternatively use Reap and Sow:
Reap[Do[If[PrimeQ[2 ^ n - 1], Sow[n]], {n, 100}]][[2, 1]]upperTriangularLinearSolve[ U_, v_ ] := Module[ {x, m, n}, {m, n} = Dimensions[U]; x = Range[n];
Do[x[[i]] = (v[[i]] - U[[i, i + 1 ;; n]].x[[i + 1 ;; n]]) / U[[i, i]], {i, n, 1, -1}];
x];upperTriangularLinearSolve[{{1, 2}, {0, 3}}, {1, 2} ]Implement LU triangular decomposition of matrices:
lu[A_] :=
Module[{m, n, L, U}, {m, n} = Dimensions[A];L = IdentityMatrix[n];U = A;
Do[
L[[k ;; n, k]] = U[[k ;; n, k]] / U[[k, k]];
U[[(k + 1) ;; n, k ;; n]] = U[[(k + 1) ;; n, k ;; n]] - L[[(k + 1) ;; n, {k}]].U[[{k}, k ;; n]];
, {k, 1, n - 1}];
{L, U}
];{l, u} = lu[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}]l.ulist = RandomSample[Range[10], 10]Do[If[list[[i]] > list[[j]], list[[{i, j}]] = list[[{j, i}]]], {i, Length[list]}, {j, i + 1, Length[list]}];listProperties & Relations (2)
Do[Print[i], {i, 3}]For[i = 1, i ≤ 3, i++, Print[i]]i = 1;While[i ≤ 3, Print[i];i++]Scan[Print, Range[3]]Loop infinitely, until a condition is met:
i = 1;Do[If[i ^ 2 > 100, Return[i], i++], ∞]i = 1;While[True, If[i ^ 2 > 100, Return[i], i++]]See Also
For While Until Table Nest NestWhile Fold ScheduledTask ContinuousTask
Tech Notes
Related Guides
History
Introduced in 1988 (1.0) | Updated in 2014 (10.0) ▪ 2015 (10.2)
Text
Wolfram Research (1988), Do, Wolfram Language function, https://reference.wolfram.com/language/ref/Do.html (updated 2015).
CMS
Wolfram Language. 1988. "Do." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/Do.html.
APA
Wolfram Language. (1988). Do. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Do.html
BibTeX
@misc{reference.wolfram_2026_do, author="Wolfram Research", title="{Do}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/Do.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_do, organization={Wolfram Research}, title={Do}, year={2015}, url={https://reference.wolfram.com/language/ref/Do.html}, note=[Accessed: 12-June-2026]}