Do
更多信息
- Do 使用标准的 Wolfram 语言迭代规则.
- 您可以在 Do 中使用 Return、Break、Continue 和 Throw.
- 除非使用明确的 Return,由 Do 返回的值是 Null.
- Do[expr,Infinity] 连续运算 expr,直到明确通过函数 Break、Return、Throw、Abort 或 Quit 等被告知 退出运行.
- Do[expr,spec] 首先计算 spec,然后局部化指定的变量,依次对其连续赋值,每次计算 expr.
- Do 实际上用 Block 来实现值或变量的局部化.
- Do[expr,spec1,spec2] 实际上等价于 Do[Do[expr,spec2],spec1].
- Parallelize[Do[expr,iter]] 或 ParallelDo[expr,iter] 在所有子核中并行计算 Do[expr,iter]. »
范例
打开所有单元 关闭所有单元基本范例 (3)
范围 (8)
计算 Print[x] 两次:
Do[Print[x], {2}]Do[Print[i], {i, 10, 8, -1}]Do[Print[n], {n, x, x + 3y, y}]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 保持循环,但不执行主体的剩余部分:
t = 1;Do[t *= k;Print[t];If[k < 2, Continue[]];t += 2, {k, 5}]Do[If[k ^ 2 > 100, Return[k]], {k, ∞}]推广和延伸 (2)
Do[Print[k], {k, {a, b, c, d}}]ParallelDo 并行计算 Do:
ParallelDo[Print[i];i, {i, 4}]Do 有效地使用 ParallelDo 自动并行运行:
Parallelize[Do[Print[i];i, {i, 4}]]应用 (5)
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}];tReap[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} ]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]}];list属性和关系 (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]]i = 1;Do[If[i ^ 2 > 100, Return[i], i++], ∞]i = 1;While[True, If[i ^ 2 > 100, Return[i], i++]]历史
1988年引入 (1.0) | 在以下年份被更新:2014 (10.0) ▪ 2015 (10.2)
文本
Wolfram Research (1988),Do,Wolfram 语言函数,https://reference.wolfram.com/language/ref/Do.html (更新于 2015 年).
CMS
Wolfram 语言. 1988. "Do." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2015. https://reference.wolfram.com/language/ref/Do.html.
APA
Wolfram 语言. (1988). Do. Wolfram 语言与系统参考资料中心. 追溯自 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: 15-September-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: 15-September-2026]}