This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

NestWhile

NestWhile
starts with expr, then repeatedly applies f until applying test to the result no longer yields True.
NestWhile
supplies the most recent m results as arguments for test at each step.
NestWhile[f, expr, test, All]
supplies all results so far as arguments for test at each step.
NestWhile
applies f at most max times.
NestWhile
applies f an extra n times.
NestWhile
returns the result found when f had been applied n fewer times.
  • NestWhile returns the first expression to which applying test does not yield True.
  • NestWhile at each step evaluates . It does not put the results in a list. »
  • The are given in the order they are generated, with the most recent coming last.
  • NestWhile does not start applying test until at least m results have been generated.
  • NestWhile does not start applying test until at least 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, UnsameQ, All] goes on applying f until the same result first appears more than once.
  • NestWhile applies f an additional n times after test fails, or max applications have already been performed. »
  • NestWhile[f, expr, test, m, Infinity, -1] returns, if possible, the last expression in the sequence expr, , , ... for which test yields True.
Keep dividing by 2 until the result is no longer an even number:
Iterate taking logarithms until the result is no longer positive:
Keep dividing by 2 until the result is no longer an even number:
In[1]:=
Click for copyable input
Out[1]=
 
Iterate taking logarithms until the result is no longer positive:
In[1]:=
Click for copyable input
Out[1]=
Compare the last two values generated:
Always compare all values generated:
Start comparisons after 4 iterations, and compare using the 4 last values:
Start comparisons after 4 iterations, and compare using the 6 last values:
Stop after at most 4 iterations, even if the test is still True:
Continue until the result is no longer greater than 1:
Perform one more step after the condition is no longer True:
Return the last value for which the condition was still True:
Find the next prime after 888:
Find the next twin prime after 888:
Find the index of the first Fibonacci number above a million:
Find the index of the last Fibonacci number below a million:
These two forms are equivalent:
NestWhile returns if the condition returns anything other then True:
The outcome of a condition need not be True or False:
FixedPoint always compares the last two values; these two forms are equivalent:
NestWhileList applies the same stopping criteria, but returns all values generated:
NestWhile can be expressed in terms of a While loop:
New in 4