While
Details
- While[test] does the loop with a null body.
- If Break[] is generated in the evaluation of body, the While loop exits.
- Continue[] exits the evaluation of body, and continues the loop.
- Unless an explicit Return is used, the value returned by While is Null.
- Note that in an example like i=0;While[i<0,tot+=f[i];i++] the roles of ; and , are reversed relative to C-like programming languages.
Examples
open all close allBasic Examples (1)
Scope (2)
Generalizations & Extensions (1)
Applications (2)
Properties & Relations (4)
If the test is False, the body is never evaluated:
While[False, Print[x]]FullForm[n = 1;While[n < 5, n++]]Use Print to see the iterations:
n = 17;While[(n = Floor[n / 2]) != 0, Print[n]];nDo iterates a fixed number of times:
x = 1.0;
Do[x = (x + 2 / x) / 2, {5}];xNestWhile is a functional form of While:
NestWhile[Floor[# / 2]&, 10, # > 0&]x = 10;While[x > 0, x = Floor[x / 2]];xTech Notes
Related Guides
Related Links
History
Introduced in 1988 (1.0)
Text
Wolfram Research (1988), While, Wolfram Language function, https://reference.wolfram.com/language/ref/While.html.
CMS
Wolfram Language. 1988. "While." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/While.html.
APA
Wolfram Language. (1988). While. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/While.html
BibTeX
@misc{reference.wolfram_2026_while, author="Wolfram Research", title="{While}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/While.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_while, organization={Wolfram Research}, title={While}, year={1988}, url={https://reference.wolfram.com/language/ref/While.html}, note=[Accessed: 12-June-2026]}