MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: If-statement problems

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49502] RE: [mg49472] If-statement problems
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Thu, 22 Jul 2004 02:45:21 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

>-----Original Message-----
>From: Aaron Fude [mailto:aaronfude at yahoo.com]
To: mathgroup at smc.vnet.net
>Sent: Wednesday, July 21, 2004 12:40 PM
>To: mathgroup at smc.vnet.net
>Subject: [mg49502] [mg49472] If-statement problems
>
>
>Hi,
>
>New to Mathematica. I can't figure out how to use the 
>if-statement for pure
>flow control.
>
>Here's an example:
>
>A = {5, 6, 7, 8}
>For [n = 1, n <= 4, n++,
> if [n == 1, a = A[[n]]];
> if [n == 2, b = A[[n]]];
> if [n == 3, c = A[[n]]];
> if [n == 4, d = A[[n]]];
>]
>
>I want to assign to a b c and d the 4 values from A. 
>Obviously, that's not
>what happens. How do I accomplish what I need?
>
>Many thanks in advance!!!
>
>Aaron Fude
>
>
>

You may do

In[1]:=
A = {5, 6, 7, 8};
For[n = 1, n <= 4, n++,
  If[n == 1, a = A[[n]]];
  If[n == 2, b = A[[n]]];
  If[n == 3, c = A[[n]]];
  If[n == 4, d = A[[n]]];]

In[3]:= {a, b, c, d}
Out[3]= {5, 6, 7, 8}

So it works, if you happen to spell If right.


But this certainly isn't the way we do in Mathematica. Observe

In[4]:= Clear[a, b, c, d]

In[5]:= {a, b, c, d}
Out[5]= {a, b, c, d}

In[6]:= {a, b, c, d} = A;

In[7]:= {a, b, c, d}
Out[7]= {5, 6, 7, 8}


--
Hartmut


  • Prev by Date: RE: Applying my function to elements of a vector
  • Next by Date: Re: If-statement problems
  • Previous by thread: Re: If-statement problems
  • Next by thread: Re: If-statement problems