Wolfram Research

Function Repository Resource:

Until

Source Notebook

Similar to While, but it performs the action before the test

Contributed by: Jon McLoone

ResourceFunction["Until"][test, body]

evaluates body then test repeatedly until test returns True.

Examples

Basic Examples

Create a loop that increments a counter and then checks for primality:

In[1]:=
x = 7;
ResourceFunction["Until"][PrimeQ[x], x++];
x
Out[3]=

Properties and Relations

Until is very similar to While except it performs the action before the test. Also, Until terminates when the test is met and While continues until it is not met:

In[4]:=
x = 7;
ResourceFunction["Until"][PrimeQ[x], x++];
x
Out[6]=
In[7]:=
x = 7;
While[! PrimeQ[x], x++];
x
Out[9]=

The equivalent While statement would be:

In[10]:=
x = 7;
x++;(*Perform tha action once before the loop*)
While[! PrimeQ[x], x++];
x
Out[12]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information