Do
詳細
- Doは,Wolfram言語の標準的な反復指定を使う.
- Return,Break,Continue,ThrowをDoの中で使用することができる.
- Returnが明示的に使用されない限り,Doが返す値はNullとなる.
- Do[expr,Infinity]は,Break,Return,Throw,AbortあるいはQuitのような関数から退出するように明示的に指示されるまで expr を評価し続ける.
- 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]を2回評価する:
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 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: 20-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: 20-June-2026]}