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.

Below, is it possible to print No and have x be 2. That is, I'm looking for an And-like function that looks at all arguments before returning.

x := 1
If[And[True, False, True, False, (x := 2; True)], Print["Yes"], Print["No"]]; x
share|improve this question
add comment

1 Answer

up vote 12 down vote accepted

You can use And and still get what you want:

x := 1
If[And @@ {True, False, True, False, (x := 2; True)}, Print["Yes"], Print["No"]]; 
x

(* No
   2 *)

What happens is that the List and its arguments are evaluated before And is applied to it, hence setting the value of x.

share|improve this answer
add comment

Your Answer

 
discard

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.