       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?
>
>
>Aaron Fude
>
>
>

You may do

In:=
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:= {a, b, c, d}
Out= {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:= Clear[a, b, c, d]

In:= {a, b, c, d}
Out= {a, b, c, d}

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

In:= {a, b, c, d}
Out= {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