Wolfram Research

Function Repository Resource:

RepeatUntil

Source Notebook

Evaluate an expression repeatedly until a test is satisfied

Contributed by: Taliesin Beynon

ResourceFunction["RepeatUntil"][body,test]

will evaluate body once, and then continue to evaluate it as long as test returns True.

Details and Options

RepeatUntil has the attribute HoldAll.

Examples

Basic Examples

Basic usage:

In[1]:=
i = 1; ResourceFunction["RepeatUntil"][i++, Print[i]; i < 5]; i
Out[1]=

Applications

The number of iterations to get an approximation:

In[2]:=
x = 1.0; n = 1; r = 1.0;
ResourceFunction["RepeatUntil"][r = (1 + x/n)^n; n++, If[Exp[x] - r > 10^-6, True, Break[]]]; {r, n}
Out[3]=

Searching sequences of primes:

In[4]:=
k = -1;
ResourceFunction["RepeatUntil"][
 k++, (Print[#]; PrimeQ[#]) &[4180566390 k + 8297644387]]

Properties and Relations

Here is an equivalent structure using While:

In[5]:=
i = 1; While[i++; Print[i]; i < 5]; i
Out[5]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information