Condition 
Examples
open all close allBasic Examples (2)
Make a definition with the condition that x should be positive:
f[x_] := ppp[x] /; x > 0f[5]f[-6]Replace all elements which satisfy the condition of being negative:
{6, -7, 3, 2, -1, -2} /. x_ /; x < 0 -> wScope (2)
Share a variable between a condition and function body:
f[x_] := Module[{u}, u ^ 2 /; ((u = x - 1) > 0)]f[0]f[6]Use Condition inside a function body to control evaluation:
f[x_] := Module[{a}, a = Prime[10 ^ x];
(FactorInteger[a + 1]) /; a < 10 ^ 6]f[4]f[5]Properties & Relations (2)
Condition evaluates a Boolean expression on named parts of a pattern:
Cases[{{a, b}, {1, 2, 3}, {{d, 6}, {d, 10}}}, {x_, y_} /; !ListQ[x] && !ListQ[y]]PatternTest applies test functions to patterns, which need not have names:
Cases[{{a, b}, {1, 2, 3}, {{d, 6}, {d, 10}}}, {_, _} ? VectorQ]Use Except to effectively negate Condition:
Replace[{1, 7, "Hi", 3, z}, Except[x_ /; NumericQ[x]] :> 0, 1]Possible Issues (1)
Repeated[p /; test] requires that every named pattern in p have the same value throughout the sequence:
MatchQ[{3, 3, 3}, {(x_ /; IntegerQ[x / 3])..} ]MatchQ[{3, 6, 9}, {(x_ /; IntegerQ[x / 3])..} ]The same is true of RepeatedNull:
MatchQ[{3, 6, 9}, {(x_ /; IntegerQ[x / 3])...} ]Use PatternTest in combination with unnamed patterns to allow a sequence of nonidentical elements:
MatchQ[{3, 6, 9}, {(_ ? (IntegerQ[# / 3]&))..} ]MatchQ[{3, 6, 9}, {(_ ? (IntegerQ[# / 3]&))...} ]See Also
Except If Switch Which PatternTest Restricted Element Piecewise
Tech Notes
Related Guides
Related Workflows
- Write a Function That Can Return Unevaluated
History
Introduced in 1988 (1.0)
Text
Wolfram Research (1988), Condition, Wolfram Language function, https://reference.wolfram.com/language/ref/Condition.html.
CMS
Wolfram Language. 1988. "Condition." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Condition.html.
APA
Wolfram Language. (1988). Condition. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Condition.html
BibTeX
@misc{reference.wolfram_2026_condition, author="Wolfram Research", title="{Condition}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/Condition.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_condition, organization={Wolfram Research}, title={Condition}, year={1988}, url={https://reference.wolfram.com/language/ref/Condition.html}, note=[Accessed: 12-June-2026]}