I think @Sander gave a nice summary. The last examples he gave are arguably showing a big disadvantage of For
: bad readability. Unless you look at those examples very very carefully, you might think that it's a standard counting loop. If they were written using While
, they would be less likely to confuse and mislead people. I would argue that writing such for
loops is bad practice even in C.
A few more things that Sander didn't mention:
Do
can have multiple iterators, and doesn't require nesting.
It's really common that people coming form C mix up the ,
and ;
in For
. This not only leads to errors, it is also very hard to spot, visually. Bad readability again.
Do
provides a clean path to learning how to write better, more idiomatic Mathematica code. It is typical when a newcomer to Mathematica searches for what he considers a fundamental programming construct, "for", and never even looks at other solutions. If they used Do
, it would give a smooth transition to Table
(which has identical syntax). Once they understand Table
, it's a small jump to Map
, using Listable
functions, parallelization, etc.
After having seen countless and countless misuses of For
by beginners, I am convinced that Mathematica would benefit significantly from banishing For
from its vocabulary. What would we lose? There's Do
and While
, which can express an equivalent program more readably. People would effective Mathematica more quickly if they didn't go down the blind alley that is For
... As it is, many get stuck writing bad Mathematica and come away with the impression that it is an inferior and frustrating language.