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

This question already has an answer here:

I have the following code:

a = Dynamic[Floor[Clock[2]]] (*So this basically alternates between 0 and 1*)
If[a==1, Print["true"]]

But this doesn't work, it instead prints the following If[a==0, Print["true"]] and then changes to If[a==1, Print["true"]] and then back.

How can I get the value from the clock?

share|improve this question

marked as duplicate by Mr.Wizard Apr 8 '13 at 10:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Related: – Mr.Wizard Apr 8 '13 at 10:25
up vote 3 down vote accepted

Well, looking at the other answers, I am not sure if I correctly understand what you want. But I'll give it a go:

a = Dynamic[Floor[Clock[2]]](*same thing*)
Dynamic[If[Setting@a == 1, "true", "false"]]

Does this yield what you wanted? The problem in your case is, that a is no "plain" integer, see FullForm[a]. Thus, the test a==1 is not executing. Using Setting lets you retrieve the value of a Dynamic var, see e.g. here: Dynamic Values

share|improve this answer
Thank you very much, this works! – user1626227 Apr 8 '13 at 8:11

Well, the problem locates in the difference between Equal and SameQ. When Equal can't determin whether the args are equal or not, it returns the expression back with no changes. What you really need is SameQ here, and the code looks like this:

a = Dynamic[Floor[Clock[2]]] (*So this basically alternates between 0 and 1*)If[a === 1, Print["true"]]
share|improve this answer

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