Skip to main content
2 of 4
added 62 characters in body

Strange behavior of Print, Do, and merged output cells

The command Do[Print[i], {i, 1, 3}] does what you expect: creates 3 output lines of 1, 2 and 3, in that order.

If one merges these output cells, then evaluates the original Do command, the output is now 3, then 2, then 1. When one deletes the output cells, the regular behavior returns.

It isn't that the Do loop is running backwards. When one does "the same thing" with

temp = {}
Do[AppendTo[temp, i];
Print[i], {i, 1, 3}];
temp

the order of the Print command is reversed, but the contents of temp are still {1, 2, 3}, not {3, 2, 1}.

Why does this happen? Does Print write to a queue that is sometimes dumped in reverse order?

I am running Mathematica 11.0.0 on Windows 10

GregH
  • 1.9k
  • 14
  • 26