Re: What is going on!?!

*To*: mathgroup at smc.vnet.net*Subject*: [mg104748] Re: [mg104688] What is going on!?!*From*: Murray Eisenberg <murray at math.umass.edu>*Date*: Sun, 8 Nov 2009 06:49:49 -0500 (EST)*Organization*: Mathematics & Statistics, Univ. of Mass./Amherst*References*: <200911071146.GAA09847@smc.vnet.net>*Reply-to*: murray at math.umass.edu

The reference page for For says, "Unless an explicit Return is used, the value returned by For is Null." Your inclusion of the final semicolon inside the For doesn't change that. In fact, there's no reason for that semicolon: remember, in Mathematica, a semicolon is NOT a line terminator; rather, it is a separator for forming compound expressions, and an expression such as expr1; expr2; means the same thing as expr1; expr2; Null. Ordinarily, when an expression returns a Null, it is suppressed in the output. As in: 1+1; (* means same thing as 1+1; Null *) However, you don't have just the Null-returning For expression, but you multiply its result by x. And so Mathematica does exactly what you asked it to do, namely, to multiply that returned value Null by the current value x, namely 10. And Mathematica sorts the factors of the product in its usual way, outputting 10 Null rather than Null 10. To obtain as result just the current value (10) of x after the loop terminates, you can do one of two things: (* Version 1: put x on a separate line *) x = 0; For[i = 1, i <= 10, i++, x = x + 1] x (* Version 2: build compound expression ending in x *) x = 0; For[i = 1, i <= 10, i++, x = x + 1]; x (* Version 3: [same thing as Version 2!]: *) x = 0; For[i = 1, i <= 10, i++, x = x + 1]; x What happens in those three ways is slightly different -- although not different in the effect you see. In the first, the For returns one result, a Null, which gets suppressed in output, and then the x returns the 10. In the second, only the complete compound expression returns a result, namely the value 10 of x. But why does your original version, which I condense here to... x = 0; For[i = 1, i <= 10, i++, x = x + 1; ] x act differently from my first version? Because in my first version, Mathematica keeps going swallowing one line after another until it reaches the end of an entire, complete expression -- here with the final ] of For, and it then evaluates that expression. But in your original version, you have an (implicit) multiplication between the closing ] and the x, so Mathematica keeps on swallowing until it reaches the end of the line. Hope this helps. Joe Hays wrote: > Can anyone tell me why the output is not a simple "10" instead of "10 Null"? > > In[3]:= x = 0; > For[i = 1, i <= 10, i++, > x = x + 1; > ] x > > Out[4]= 10 Null > > -- Murray Eisenberg murray at math.umass.edu Mathematics & Statistics Dept. Lederle Graduate Research Tower phone 413 549-1020 (H) University of Massachusetts 413 545-2859 (W) 710 North Pleasant Street fax 413 545-1801 Amherst, MA 01003-9305

**References**:**What is going on!?!***From:*Joe Hays <joehays@vt.edu>

**Re: fitting**

**Re: Function returns null**

**Re: What is going on!?!**

**Re: Re: What is going on!?!**