Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there an equivalent to Sum for And?

That is, just like instead of Plus@@Table[f[i], {i,3,6}] (or Total@Table[f[i], {i,3,6}]) you can write Sum[f[i], {i,3,6}], is there a single pre-defined function that can replace And@@Table[f[i], {i,3,6}]?

If not, is there a better alternative for And@@Table[...]?

Note that, at least in Mathematica 8, Conjunction is not the right tool (despite the claim in the documentation that "Conjunction is to And what Product is to Times") because it only substitutes logical values, that is, it can only replace the special case And@@Table[f[var], {var, {False, True}}] (and of course multi-variable versions of the same structure).

share|improve this question
I don't think so. Are you looking for short circuiting? SetAttributes[and, HoldAll]; and[expr_, iter_] := And @@ Catch[Table[With[{e = expr}, If[e === False, Throw[False]]; e], iter]] – Szabolcs Jun 11 '14 at 15:57
You could also use Product together with Boole. – Szabolcs Jun 11 '14 at 16:00
Short-circuiting would certainly be nice. The following also works, but seems quite unelegant: Module[{},Do[If[!f[i],Return[False,Module]],{i,3,7}];True]. – celtschk Jun 11 '14 at 16:05
In version 10, the function AllTrue will do this documentation. It even short-cuts. – Daniel W Jun 11 '14 at 16:10
related… "Are there “All” and “Any” functions in Mathematica?" – Nasser Jun 11 '14 at 16:35
up vote 8 down vote accepted

If I'm not mistaken you should be happy to use Array:

Array[x, 10, 0, And]
x[0] && x[1] && x[2] && x[3] && x[4] && x[5] && x[6] && x[7] && x[8] && x[9]

It works on V9, don't know if for previous versions too.

share|improve this answer
Thanks, I wasn't aware that Array can take a head. Actually I did look for the possibility to use your own head for Table and it doesn't have that option (or I just wasn't able to figure it out), but I didn't think of looking at Array. It also works with version 8.0. – celtschk Jun 12 '14 at 8:08
@celtschk I wasn't sure what is the solution but I had an impression that I've seen one. And finally I've found it :) I think it will be useful for me too, so thanks :) – Kuba Jun 12 '14 at 14:15

A first pass at a true equivalent to And and Table, except with early exit (short circuit) behavior:

SetAttributes[and, HoldAll]

and[body_, iter__] := Module[{all = True},
   Do[If[all = all && body, , Break[]], iter];

and[Positive[i], {i, 7}]
and[Positive[i], {i, {foo, 0, 1, 2, 3}}]
and[Positive[i], {i, {foo, 1, 2, 3, bar}}]


Positive[foo] && Positive[bar]
share|improve this answer

Combining ideas already expressed in the comments:

SetAttributes[and, HoldAll]

and[body_, iter__] := (Do[If[! body, Return[False, CompoundExpression]], iter]; True)


and[PrimeQ[i], {i, {2, 3, 5, 7}}]

and[PrimeQ[i], {i, {2, 3, 4, 5}}]


This is not actually equivalent to And and Table, e.g. here:

And[True, foo, bar, True]
foo && bar
share|improve this answer
Nice. It's too easy to forget that ; also is a "function" (i.e. has its own head, CompoundExpression) in Mathematica, which of course can be used in Return. Certainly more elegant than my "dummy module". – celtschk Jun 12 '14 at 8:12

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.