# Creating a Table with a list of If statements given in a certain order

I am trying to create a conditional Table with If statements, where I move to the next condition only if the first one is satisfied, otherwise I "Break[]" and move to the next element of the table. For now, I have created a table with my conditions but I can't find a way to specify an order between these conditions.

As an example, let's take the function:

f[x,y]=(1-x y)/(x y-0.1)


with 0 < x < 1 and 0 < y < 1 and I want to get a 3x3 matrix looking at the values taken by f for x={0,0.5,1} and y={0,0.5,1}. So if I calculate f for these different values, I get this board of values: I am interested only in knowing if the f[x,y] for each value pair is <0 or >0 and <2 or >2. In other words I want to get this output: However the closest I found is:

f[x_, y_] = (1 - x y)/(x y - 0.1)
TableForm[Table[
If[f[x, y] <= 0, f < 0, ## &[]] &&
If[f[x, y] <= 2, 0 < f < 2, f > 2],
{x, 0, 1, 0.5}, {y, 0, 1, 0.5}]
, TableHeadings -> {{"", y, ""}, {"", x, ""}}] where I am looking at the 2 conditions no matter what is the answer to the first one.

How can I say "If f<0, don't look at the second condition, move directly to the next element of the table (to avoid useless calculations); otherwise, don't print anything and look at the second condition. Then if f<2, print 0 < f < 2, otherwise print f > 2"?

I tried to add a Break[] in the first condition but it doesn't work...

Thank you so much in advance!

• Give TableForm[ Table[Which[f[x, y] <= 0, "f<0", 0 < f[x, y] < 2, "0<f<2", True, "f>2"], {x, 0, 1, 0.5}, {y, 0, 1, 0.5}], TableHeadings -> {{"", y, ""}, {"", x, ""}}] a whirl (I did not correct the inconsistent test vs output, btw...) This is corrected, generates example: TableForm[ Table[Which[f[x, y] < 0, "f<0", 0 <= f[x, y] <= 2, "0<f<2", True, "f>2"], {x, 0, 1, 0.5}, {y, 0, 1, 0.5}], TableHeadings -> {{"", y, ""}, {"", x, ""}}] – ciao Sep 4 '15 at 0:35
• Thanks! How do I specify to StackExchange that you've answered to my question? – Elsa Sep 4 '15 at 0:45
• Well, since it's a comment, you can't. I'll post as an answer, then you can click the check-mark... – ciao Sep 4 '15 at 0:47
• Thanks! and additional question: how can I get the value pairs {x,y}, which got True? (so here it would be {0.5,0.5}) – Elsa Sep 4 '15 at 2:52

Give

TableForm[ Table[Which[f[x, y] <= 0, "f<0", 0 < f[x, y] < 2, "0<f<2", True, "f>2"], {x, 0, 1, 0.5}, {y, 0, 1, 0.5}], TableHeadings -> {{"", y, ""}, {"", x, ""}}]


a whirl (I did not correct the inconsistent test vs output, btw...) This is corrected, generates example:

 TableForm[ Table[Which[f[x, y] < 0, "f<0", 0 <= f[x, y] <= 2, "0<f<2", True, "f>2"], {x, 0, 1, 0.5}, {y, 0, 1, 0.5}], TableHeadings -> {{"", y, ""}, {"", x, ""}}]


Just for variety and exploiting same x and y range:

g[x_, y_] := {Boole[# < 0], Boole[0 <= # < 2]} &@
f[x, y] /. {{0, 0} -> "f>=2", {1, 0} -> "f<0", {0, 1} -> "0<=f<2"}


then

With[{r = {0, 0.5, 1}},
TableForm[Partition[g @@@ Tuples[r, {2}], 3], 