Why does Print[n] produce output inside a For loop, while Sqrt[n] (as well as many other functions) does not?
In addition can you get Print[n] to single space its output?
Why does Print[n] produce output inside a For loop, while Sqrt[n] (as well as many other functions) does not?
In addition can you get Print[n] to single space its output?
Because Print puts text on your screen as a side effect. The result of a For loop, however, is Null.
Mathematica is an expression rewriting language, very different from C. Using C programming style is inadvisable, especially because it will confuse you as a C programmer.
I suggest:
Table[Sqrt[n], {n,1,5}]
Null.
– Szabolcs
Sep 20 '18 at 19:27
Another method, sticking with For
Reap[For[i = 0, i < 4, i++, Sow[Sqrt[i]]]][[2, 1]]
{0, 1, Sqrt[2], Sqrt[3]}
For[start,test,incr,body]evaluatesbody, but "Unless an explicit Return is used, the value returned by For is Null." – kglr Sep 20 '18 at 19:18for (i=0; i < 5; i++) { sqrt(i); }? It's exactly the same thing.printfprints to the screen andsqrtdoesn't. Furthermore,forin C is not an expression so it doesn't have a value (even if thesqrtinside does). In Mathematica everything is an expression, but where it doesn't make sense to return a value,Nullis returned. – Szabolcs Sep 20 '18 at 19:18Printwill create a separate cell in the notebook. Thus this is not about single or double-spacing but about the top and bottom margins of a"Print"style cell. This is defined by the stylesheet and can be changed. In command line mode, eachPrintoutputs on a new line (with no empty lines inbetween). – Szabolcs Sep 20 '18 at 19:32Forworks, but after that, it's probably better if you don't use it.) – Szabolcs Sep 20 '18 at 19:37Printyou must wrap the expression you want to print with one Mathematica many formatting functions. Since you new to Mathematica, I suggest you look atRowandColumnfor spacing control. Basic vertical spacing:Print[Column[{a, b, c}, Spacings -> 0]]. Basic horizontal spacing:Print[Row[{a, b, c}, " "]]. – m_goldberg Sep 21 '18 at 03:55