For example when would you use a Do loop over a For loop? For which tasks would you use Map, Table, Scan, et cetera? I'm quite new to Mathematica and I don't really get what the advantages of any of these loops are over the standard For loop whose use seems to be so harshly discouraged here on StackExchange.
Asked
Active
Viewed 118 times
2
m_goldberg
- 107,779
- 16
- 103
- 257
Aron
- 1,722
- 1
- 12
- 30
DovsFor: if you can useDo, do not useFor.Foris less readable and introduces global variables which you'd have to localize manually.Foris not generally standard, it is just a convenient construct in some procedural languages. If you compare code using For with code equivalent using Do, it should be clear why Do is usually much more convenient in Mathematica. – Szabolcs Jan 02 '14 at 18:49Docan directly loop over the elements of a list,Forcan't. CompareDo[Print[i],{i,myList}]vsFor[i=1,i<Length[myList],i++,Print[myList[[i]]]]. Of course,Map (/@)is shorter:Print/@myList. – Sjoerd C. de Vries Jan 02 '14 at 18:56For,Part, etc. That's great, you can – Rojo Jan 02 '14 at 19:00For, I think it's our collective experience that you very very rarely need to go "so general"; almost always there's a better alternative. – Rojo Jan 02 '14 at 19:02