NestWhile
✖
NestWhile
NestWhile[f,expr,test]
starts with expr, then repeatedly applies f until applying test to the result no longer yields True.
NestWhile[f,expr,test,m]
supplies the most recent m results as arguments for test at each step.
NestWhile[f,expr,test,m,max]
applies f at most max times.
NestWhile[f,expr,test,m,max,n]
applies f an extra n times.
NestWhile[f,expr,test,m,max,-n]
returns the result found when f had been applied n fewer times.
Details

- NestWhile[f,expr,test] returns the first expression f[f[… f[expr]…]] to which applying test does not yield True.
- If test[expr] does not yield True, NestWhile[f,expr,test] returns expr. »
- NestWhile[f,expr,test,m] at each step evaluates test[res1,res2,…,resm]. It does not put the results resi in a list. »
- The resi are given in the order they are generated, with the most recent coming last.
- NestWhile[f,expr,test,m] does not start applying test until at least m results have been generated.
- NestWhile[f,expr,test,{mmin,m}] does not start applying test until at least mmin results have been generated. At each step it then supplies as arguments to test as many recent results as possible, up to a maximum of m. »
- NestWhile[f,expr,test,m] is equivalent to NestWhile[f,expr,test,{m,m}]. »
- NestWhile[f,expr,UnsameQ,2] is equivalent to FixedPoint[f,expr]. »
- NestWhile[f,expr,test,All] is equivalent to NestWhile[f,expr,test,{1,Infinity}]. »
- NestWhile[f,expr,UnsameQ,All] goes on applying f until the same result first appears more than once.
- NestWhile[f,expr,test,m,max,n] applies f an additional n times after test fails, or max applications have already been performed. »
- NestWhile[f,expr,test,m,max,-n] is equivalent to Part[NestWhileList[f,expr,test,m,max],-n-1]. »
- NestWhile[f,expr,test,m,Infinity,-1] returns, if possible, the last expression in the sequence expr, f[expr], f[f[expr]], … for which test yields True.
Examples
open allclose allBasic Examples (2)Summary of the most common use cases
Scope (5)Survey of the scope of standard use cases
Compare the last two values generated:

https://wolfram.com/xid/0cg5nevbq-lgmldb

Always compare all values generated:

https://wolfram.com/xid/0cg5nevbq-k60s56

Start comparisons after 4 iterations, and compare using the 4 last values:

https://wolfram.com/xid/0cg5nevbq-t2u457

Start comparisons after 4 iterations, and compare using the 6 last values:

https://wolfram.com/xid/0cg5nevbq-zhom6v

Stop after at most 4 iterations, even if the test is still True:

https://wolfram.com/xid/0cg5nevbq-8krxt6

Generalizations & Extensions (2)Generalized and extended use cases
Continue until the result is no longer greater than 1:

https://wolfram.com/xid/0cg5nevbq-0bqu8h

Perform one more step after the condition is no longer True:

https://wolfram.com/xid/0cg5nevbq-8xuhnc

Return the last value for which the condition was still True:

https://wolfram.com/xid/0cg5nevbq-ev4bxa

Applications (3)Sample problems that can be solved with this function
Find the next prime after 888:

https://wolfram.com/xid/0cg5nevbq-uvq

Find the next twin prime after 888:

https://wolfram.com/xid/0cg5nevbq-ga

Find the index of the first Fibonacci number above a million:

https://wolfram.com/xid/0cg5nevbq-qt2


https://wolfram.com/xid/0cg5nevbq-mto


https://wolfram.com/xid/0cg5nevbq-cq2

Find the index of the last Fibonacci number below a million:

https://wolfram.com/xid/0cg5nevbq-e060as

Properties & Relations (5)Properties of the function, and connections to other functions
These two forms are equivalent:

https://wolfram.com/xid/0cg5nevbq-pvvciq


https://wolfram.com/xid/0cg5nevbq-3jni4a

NestWhile returns if the condition returns anything other then True:

https://wolfram.com/xid/0cg5nevbq-te7kib

The outcome of a condition need not be True or False:

https://wolfram.com/xid/0cg5nevbq-qlkpr9

FixedPoint always compares the last two values; these two forms are equivalent:

https://wolfram.com/xid/0cg5nevbq-q9ips1


https://wolfram.com/xid/0cg5nevbq-qmo6dq

NestWhileList applies the same stopping criteria, but returns all values generated:

https://wolfram.com/xid/0cg5nevbq-pzsdgx

NestWhile can be expressed in terms of a While loop:

https://wolfram.com/xid/0cg5nevbq-fm75fz


https://wolfram.com/xid/0cg5nevbq-o8ed7y

Wolfram Research (1999), NestWhile, Wolfram Language function, https://reference.wolfram.com/language/ref/NestWhile.html.
Text
Wolfram Research (1999), NestWhile, Wolfram Language function, https://reference.wolfram.com/language/ref/NestWhile.html.
Wolfram Research (1999), NestWhile, Wolfram Language function, https://reference.wolfram.com/language/ref/NestWhile.html.
CMS
Wolfram Language. 1999. "NestWhile." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NestWhile.html.
Wolfram Language. 1999. "NestWhile." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NestWhile.html.
APA
Wolfram Language. (1999). NestWhile. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NestWhile.html
Wolfram Language. (1999). NestWhile. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NestWhile.html
BibTeX
@misc{reference.wolfram_2025_nestwhile, author="Wolfram Research", title="{NestWhile}", year="1999", howpublished="\url{https://reference.wolfram.com/language/ref/NestWhile.html}", note=[Accessed: 06-June-2025
]}
BibLaTeX
@online{reference.wolfram_2025_nestwhile, organization={Wolfram Research}, title={NestWhile}, year={1999}, url={https://reference.wolfram.com/language/ref/NestWhile.html}, note=[Accessed: 06-June-2025
]}