# Multiple conditions in If function

I wrote a function which returns 1 if x^2 + y^2<=1:

f[x_, y_] := If[x^2 + y^2 <= 1, 1]


Now I'm trying to continue this function to take in multiple conditions like if x^2+y^2>1, |x|<=1 and |y|<=1, the function should return 0. I would also like to expand this function to take in 'or' statements such as if |x|>1 or |y|>1 then etc.

• Look up Which, Switch, And and Or. – Szabolcs Feb 9 at 16:48

You can use rules in making your definitions, as follows

f[x_, y_] := 1 /; x^2 + y^2 <= 1
f[x_, y_] := 0 /; Abs[x] < 1 || Abs[y] < 1


giving the results that you request, and remaining unevaluated otherwise

{f[0.3, 0.4], f[0.9, 1.2], f[u, v]}
(* {1, 0, f[u, v]} *)


In general, Mathematica will apply the rules in the order you define them, but you can check this using DownValues[f]. (See documentation for more details).

• No, in general Mathematica will apply the rules in the order from most specific to most general, unless no clear ordering is apparent and in which case the order of definition is used. – Roman Feb 9 at 18:43
• Also, ?f gives more information than just DownValues. – Roman Feb 9 at 18:43