Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What is going on!?!

2 views
Skip to first unread message

Joe Hays

unread,
Nov 7, 2009, 6:45:37 AM11/7/09
to
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


Leonid Shifrin

unread,
Nov 8, 2009, 6:49:43 AM11/8/09
to
Joe,

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.

Regards,
Leonid

Murray Eisenberg

unread,
Nov 8, 2009, 6:51:53 AM11/8/09
to
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

Stern

unread,
Nov 8, 2009, 6:52:29 AM11/8/09
to
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[].

Table[i, {i, 10}]

Range[10]

-Michael Stern
Merrin Capital Management

David Park

unread,
Nov 8, 2009, 7:07:36 AM11/8/09
to
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

David Park
djm...@comcast.net
http://home.comcast.net/~djmpark/

Bill Rowe

unread,
Nov 8, 2009, 7:08:45 AM11/8/09
to
On 11/7/09 at 6:46 AM, joe...@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:

=46or[i = 1, i <= 10, i++,


x = x + 1;
]; x

Or even better

Sum[n,,{n,10}]


Bob Hanlon

unread,
Nov 8, 2009, 7:10:30 AM11/8/09
to

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.

x = 0;
For[i = 1, i <= 10, i++, x = x + 1]; x

10


Bob Hanlon

---- Joe Hays <joe...@vt.edu> wrote:

=============

David Bailey

unread,
Nov 8, 2009, 7:10:54 AM11/8/09
to
Your For function returns Null, and this is getting multiplied by x
because you forgot the semicolon after the ] .

David Bailey
http://www.dbaileyconsultancy.co.uk

Albert Retey

unread,
Nov 8, 2009, 7:27:04 AM11/8/09
to
Joe Hays schrieb:


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...

hth,

albert

Joe Hays

unread,
Nov 9, 2009, 5:55:05 AM11/9/09
to
Thanks all. Pretty dump mistake... <sheepish grin>

On Sun, Nov 8, 2009 at 6:49 AM, Murray Eisenberg <mur...@math.umass.edu>wrote:

> 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
> >
> >
>

0 new messages