Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
What is going on!?!
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
Joe Hays  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 11:45
Newsgroups: comp.soft-sys.math.mathematica
From: Joe Hays <joeh...@vt.edu>
Date: Sat, 7 Nov 2009 11:45:37 +0000 (UTC)
Local: Sat 7 Nov 2009 11:45
Subject: What is going on!?!
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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Leonid Shifrin  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 11:49
Newsgroups: comp.soft-sys.math.mathematica
From: Leonid Shifrin <lsh...@gmail.com>
Date: Sun, 8 Nov 2009 11:49:43 +0000 (UTC)
Local: Sun 8 Nov 2009 11:49
Subject: Re: What is going on!?!
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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Murray Eisenberg  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 11:51
Newsgroups: comp.soft-sys.math.mathematica
From: Murray Eisenberg <mur...@math.umass.edu>
Date: Sun, 8 Nov 2009 11:51:53 +0000 (UTC)
Local: Sun 8 Nov 2009 11:51
Subject: Re: What is going on!?!
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

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stern  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 11:52
Newsgroups: comp.soft-sys.math.mathematica
From: Stern <nycst...@gmail.com>
Date: Sun, 8 Nov 2009 11:52:29 +0000 (UTC)
Local: Sun 8 Nov 2009 11:52
Subject: Re: What is going on!?!
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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Park  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 12:07
Newsgroups: comp.soft-sys.math.mathematica
From: "David Park" <djmp...@comcast.net>
Date: Sun, 8 Nov 2009 12:07:36 +0000 (UTC)
Local: Sun 8 Nov 2009 12:07
Subject: Re: What is going on!?!
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
djmp...@comcast.net
http://home.comcast.net/~djmpark/  

From: Joe Hays [mailto:joeh...@vt.edu]

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bill Rowe  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 12:08
Newsgroups: comp.soft-sys.math.mathematica
From: Bill Rowe <readn...@sbcglobal.net>
Date: Sun, 8 Nov 2009 12:08:45 +0000 (UTC)
Local: Sun 8 Nov 2009 12:08
Subject: Re: What is going on!?!
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:

=46or[i = 1, i <= 10, i++,
   x = x + 1;
   ]; x

Or even better

Sum[n,,{n,10}]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Hanlon  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 12:10
Newsgroups: comp.soft-sys.math.mathematica
From: Bob Hanlon <hanl...@cox.net>
Date: Sun, 8 Nov 2009 12:10:30 +0000 (UTC)
Local: Sun 8 Nov 2009 12:10
Subject: Re: What is going on!?!

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 <joeh...@vt.edu> 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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Bailey  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 12:10
Newsgroups: comp.soft-sys.math.mathematica
From: David Bailey <d...@removedbailey.co.uk>
Date: Sun, 8 Nov 2009 12:10:54 +0000 (UTC)
Local: Sun 8 Nov 2009 12:10
Subject: Re: What is going on!?!
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

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Albert Retey  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 12:27
Newsgroups: comp.soft-sys.math.mathematica
From: Albert Retey <a...@gmx-topmail.de>
Date: Sun, 8 Nov 2009 12:27:04 +0000 (UTC)
Local: Sun 8 Nov 2009 12:27
Subject: Re: What is going on!?!
Joe Hays schrieb:

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

hth,

albert


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joe Hays  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 10:55
Newsgroups: comp.soft-sys.math.mathematica
From: Joe Hays <hays....@gmail.com>
Date: Mon, 9 Nov 2009 10:55:05 +0000 (UTC)
Local: Mon 9 Nov 2009 10:55
Subject: Re: What is going on!?!
Thanks all. Pretty dump mistake...  <sheepish grin>

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google