The For loop does not produce any result, that is - it produces Null. The space is understood as a multiplication operator by the Mathematica parser. Thus your result. Put a semicolon after the closing bracket of the For loop and you will get what you expect.
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 mur...@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
You will probably get a lot of answers to this. By including only a space between the For[] expression and the x, you have multiplied these two things together. x is, as you expect, worth 10, but the For[] expression has no value. When you multiply these together, you get 10 Null.
You can illustrate this by doing something like
In[]:= x = 0; s = For[i = 1, i <= 10, i++, x = x + 1;]
In[]:= s
There will be no result, as the For[] expression itself has no value.
Also, though this seems to be just a test problem, whatever you're trying to accomplish here may better be done with Table[] or, in the really simple case, Range[].
I think it is generally a good technique to put multiple statements in one cell, but you need to insert a line return (Enter) before the last x. That is, the last x should be on a separate line - but in the same cell.
Because of the last ";" the For statement returned a Null, which would normally suppress output from a line, but you multiplied it by x and that's why you got 10 Null.
Generally, in Mathematica, it is better to use Procedural programming statements. Something similar could be done with:
x = 0; Do[x++, {10}] X
x = 0; numbers = {x}; While[x < 10, x = x + 1; numbers = Join[numbers, {x}]] numbers
On 11/7/09 at 6:46 AM, joeh...@vt.edu (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
Per the documentation, For returns Null. So For[....] x evaluates to x times Null which is what you got. If you want simply the result then change the code to be:
The output of the For is Null (from documentation on For: "Unless an explicit Return is used, the value returned by For is Null."). When multiplied by x (i.e., 10) gives 10*Null. Put the semi-colon after the right bracket.
> 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
you are multiplying the result of the For loop with x, which happens to be 10. And from the documentation: "Unless an explicit Return is used, the value returned by For is Null." which is why the result of the loop times 10 is 10*Null...
> 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 mur...@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