Wolfram Computation Meets Knowledge

WhenNot

Contributed by: Taliesin Beynon

An opposite of When

ResourceFunction["WhenNot"][test1,value1,test2,value2,][e]

evaluates each of the testi[e] in turn, returning the value of the valuei corresponding to the first test that does not yield , otherwise yields e unchanged.

Details and Options

WhenNot must have at least one test and value.
ResourceFunction["WhenNot"] has attribute HoldRest.

Examples

Basic Examples

WhenNot evaluates the corresponding value only when the specified condition is not met:

In[1]:=
ResourceFunction["WhenNot"][# < 3 &, Print["not small"]][5]
In[2]:=
ResourceFunction["WhenNot"][# < 3 &, Print["not small"]][1]
Out[2]=

Scope

Use a single test with WhenNot:

In[3]:=
ResourceFunction["WhenNot"][IntegerQ, "not integer"][1.]
Out[3]=
In[4]:=
ResourceFunction["WhenNot"][IntegerQ, "not integer"][1]
Out[4]=

Try several tests in turn:

In[5]:=
ResourceFunction["WhenNot"][# == 1 &, "not one", # == 2 &, "not two"][1]
Out[5]=

Properties and Relations

When executes its body and returns the corresponding value if the test yields True:

In[6]:=
When[# == 1 &, x][1]
Out[6]=
In[7]:=
ResourceFunction["WhenNot"][# == 1 &, x][1]
Out[7]=

Resource History