# What is the order of execution of Animate?

I used to think the execution order of Animate is the same as For until I find:

Animate[var++,{n,0,10,1}];


the var increases while n not. But

Animate[var=n,{n,0,10,1}];


the n keeps increasing.

Why would this happen?

If I want a expression which don't contain n to be executed only once in every step, what should I do? (Actually, I want to embed a code in my website using CDF. It will be OK if there are other solutions without using Animate.)

-
You should initialize var, i.e., var = 1; Animate[var++, {n, 0, 10, 1}] works as exptected. Or, if you want to rest var in every cycle you can use var = 1; Animate[Mod[var++, 10, 1], {n, 0, 10, 1}]. Also look at Clock, e.g. Dynamic@Clock[{0, 10, 1}] – kglr Aug 24 '14 at 14:22
@kguler It works as expected but is not what the OP wants? I think the OP already had initialized var, or he wouldn't have observed the behavior that he observed. – C. E. Aug 24 '14 at 14:47
@Pickett, very good observation -- should have read the question carefully.. – kglr Aug 24 '14 at 14:51
Does my answer address your question, or is there still some issue that needs explanation? – Michael E2 Aug 27 '14 at 12:41
Never mind. Just saw your comment. How funny -- at the same time! :) – Michael E2 Aug 27 '14 at 12:42

### The problem

Animate returns a Manipulate:

Animate[var++, {n, 0, 9, 1}] // InputForm
(*
Manipulate[
var++,
{{n, 1}, 0, 9, 1,
AppearanceElements -> {"ProgressSlider", "PlayPauseButton",
"FasterSlowerButtons", "DirectionButton"}},
ControlType -> Animator, AppearanceElements -> None,
DefaultBaseStyle -> "Animate", DefaultLabelStyle -> "AnimateLabel",
SynchronousUpdating -> True, ShrinkingDelay -> 10.]
*)


Manipulate in turn wraps its body var++ with Dynamic. Thus the displayed expression depends on var and the body will be updated whenever var changes value. Since evaluation leads to incrementing var each time, updating occurs as rapidly as possible. Another issue, which might or might not be important, is that an Animator updates its variable n according to a Clock. This is so that equal changes in n occur in equal times. It is not guaranteed to increment by 1 at each step, if the animation rate is relatively high.

### Solutions

The desired behavior requires var to be incremented whenever n changes. One approach is to make the body depend on n but not on var. TrackedSymbols will help restrict dependency to explicitly listed symbols.

Potentially buggy solutions

The simplest approach is to add the expression n; to the body, and restrict the tracked symbols to n:

var = 0;
Animate[n; var++, {n, 0, 9, 1}, TrackedSymbols :> {n}]


This will behave in the desired way on most or all computer systems.

The problem is that n will be incremented at a constant rate no matter how long it takes n; var++ to execute and be redisplayed. It is possible for n to be increased by more than var. This can be observed in the following. The AnimationRate might need to be adjusted for a particular computer. On mine, the variables start to slip apart around a rate of 20 or 25 and constantly slip at a rate of 100.

var = 0;
Animate[var++; {Mod[n - var, 10], n, var}, {n, 0, 9, 1},
AnimationRate -> 100, TrackedSymbols :> {n}]


Synchronized, but might not always be updated by one

If the variables need to be synchronized, then the updating var at the same time as n is the way to go. This can be done with the second argument of Dynamic. Since Animate automatically sets up the control for n, I'll adapt the Manipulate code above to customize the Animator control.

var = 1;
Manipulate[
{Mod[var - n, 10], n, var},
{{n, var},
Animator[Dynamic[n, (var += # - n; n = #) &],
{0, 9, 1},
AppearanceElements -> {"ProgressSlider", "PlayPauseButton",
"FasterSlowerButtons", "DirectionButton"}] &},
AppearanceElements -> None, DefaultBaseStyle -> "Animate",
DefaultLabelStyle -> "AnimateLabel", SynchronousUpdating -> True,
ShrinkingDelay -> 10.]


Synchronized, updated by one

One can override the default update of n in the previous code to ensure that n and var are incremented by 1 each time. This breaks the synchronization with Clock and the direction button of the Animator. (One probably ought to omit "DirectionButton" from AppearanceElements, but I'll leave it in so one can try it.)

var = 1;
Manipulate[
{Mod[var - n, 10], n, var},
{{n, var},
Animator[Dynamic[n, (var++; n = Mod[n + 1, 10]) &],
{0, 9, 1},
AnimationRate -> 100,
AppearanceElements -> {"ProgressSlider", "PlayPauseButton",
"FasterSlowerButtons", "DirectionButton"}] &},
AppearanceElements -> None, DefaultBaseStyle -> "Animate",
DefaultLabelStyle -> "AnimateLabel", SynchronousUpdating -> True,
ShrinkingDelay -> 10.]

-
I changed 10 to 9 because I found it more convenient for n to take on ten different values. – Michael E2 Aug 24 '14 at 16:08
Exactly what I want. Thank you very much! – user18935 Aug 27 '14 at 12:40
@user18935 You're welcome. – Michael E2 Aug 27 '14 at 12:42