Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'd like to define a function that controls if a certain number is an integer, rational, algebraic and so far. First, I tried generating a list of those numbers:

ZL = {1, 3, Pi, E, Sqrt[2], Zeta[3]} 

I then created my functions like so:

TestI[x_] := If[x ∈ Integers, x "is Integer", x "is no Integer"]

It works so far.

I now tried to define a For-function like this:

For[i = 1, i <= Length[ZL], i++, Print[TestZ[Part[ZL, i]]]]

However, when I evaluate the above expression, the output is like

is integer
3 is integer
is no integer E 

How can I achieve an output that says:

1 is Integer
3 is Integer
\e is no Integer

and so on?

share|improve this question
TestI[x_] := If[x \[Element] Integers, ToString[x] <> " is Integer", ToString[x] <> " is no Integer"] Also, BAD idea to use uppercase for your symbol initials - might clash with built-in symbols. –  rasher Apr 10 at 9:40
zl = {1, 3, Pi, E, Sqrt[2], Zeta[3]}; testi[x_] := If[IntegerQ[x], x "is Integer", x "is not Integer"]; For[i = 1, i <= Length[zl], i++, Print[testi[Part[zl, i]]]] –  martin Apr 10 at 9:40
or If[Element[x , Integers] –  martin Apr 10 at 9:49
@martin Your hint wont work. I get the same results. –  K. L. Apr 10 at 10:18
@K.L. If you copy & paste entire first comment what do you get? –  martin Apr 10 at 10:20

2 Answers 2

You could achieve without If, e.g.:

f[x_Integer] := StringForm["`` is an integer", x];
f[x_] := StringForm["`` is not an integer", x];


test = {1, 3, Pi, E, Sqrt[2], Zeta[3], Zeta[-2]}


Column[f /@ test]


enter image description here

Please note IntegerQ[3] is True, however IntegerQ[3.] is False

share|improve this answer
Please consider Listable too :) –  Kuba Apr 10 at 9:53

In the interest of pursuing your expressed long term goal, you might consider using Mathematica's capability for pattern matching and for overloading function definitions. For more information on what you need to know to go down this route, look at this topic in the Documentation Center and especially this sub-topic.

data = {1, 3, 3., 2/3, Pi, E, Sqrt[2], Zeta[3], Zeta[-2], {42}};
test[x_Integer] := Row[{x, " is an integer"}]
test[x_Rational] := Row[{x, " is a rational"}]
test[x_Real] := Row[{x, " is a real"}]
test[x_?NumericQ] := Row[{x, " is a numerical object"}]
test[x__] := Row[{"type of", x, " can not be determined"}]

Column[test /@ data]


Note that there is no need to use a for-loop, If, or Print to implement this kind of code Mathematica.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.