# Dynamic[] and Print[] [duplicate]

I'm trying to print an output from every iteration of a Do[] loop, but without it continuing down the notebook. I want the Print[] output to be overwritten in the same cell with the new value. Here is a MWE of what I'm trying, but I can't seem to get the expected behavior.

Do[
If[i==1,
Print["Output = ", Dynamic[i]]
]
,{i,10}
]


So in other words, on the first iteration, I want to see "Output = 1", and then every subsequent iteration I want to see the 1 change to a 2, 3, 4, etc. in the same cell.

-

## marked as duplicate by The Toad♦, Sjoerd C. de VriesSep 5 '12 at 22:09

One idea is to use PrintTemporary. –  PlatoManiac Sep 5 '12 at 13:32
@PlatoManiac Post an answer! –  belisarius Sep 5 '12 at 13:44
@Verde I am so so incorrigibly lazy! Thx for motivating... –  PlatoManiac Sep 5 '12 at 13:49
Some more related near duplicates (if others want to choose alternatives): mathematica.stackexchange.com/q/5985/5, mathematica.stackexchange.com/q/5978/5, mathematica.stackexchange.com/q/1489/5 –  The Toad Sep 5 '12 at 18:10

Here is one way,

Print["Output = ", Dynamic[j]];
Do[j = i; Pause[0.1];, {i, 10}]

-

You could use Monitor like :

Monitor[Do[Pause[3], {i, 1, 10^3}], i]

-

There are many, many possibilities to achieve something like that. If you want to use Dynamic, you could set a global value which you observe

Dynamic[globalWatch]

Do[
If[OddQ[i], globalWatch = i];
Pause[.1];
ComplicatedCalculation[i],
{i, 100}
]


I always suspect, whether Do is really needed, but sometimes when I for instance iterate over a list of files I want to process, I use it too. In such a situation, I use PrintTemporary as suggested by PlatoManiac to see the progress. PrintTemporary is nice because you can print all information you like and no matter it fills the screen, it gets erased after the computation is done.

-