As of Version 13.1, Until is a built-in symbol.

Function Repository Resource:

Until

Source Notebook

Provide a loop construct that is similar to While, but performing the action prior to rather than after the termination test

Contributed by: Jon McLoone

ResourceFunction["Until"][test,body]

evaluates body then test repeatedly until test returns True.

Examples

Basic Examples (1) 

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

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

Scope (1) 

Until can be used as an operator form:

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

Options (1) 

The option MaxIterations limits the number of times the loop is repeated:

In[7]:=
x = 0;
ResourceFunction["Until"][x > 1000, x++, MaxIterations -> 20]
x
Out[9]=

Properties and Relations (2) 

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[10]:=
x = 7;
ResourceFunction["Until"][PrimeQ[x], x++];
x
Out[12]=
In[13]:=
x = 7;
While[! PrimeQ[x], x++];
x
Out[15]=

The equivalent While statement would be:

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

Publisher

Jon McLoone

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 2.0.0 – 08 April 2020
  • 1.0.0 – 06 March 2019

License Information