Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: If-statement problems

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49528] Re: If-statement problems
  • From: Paul Abbott <paul at physics.uwa.edu.au>
  • Date: Thu, 22 Jul 2004 02:47:06 -0400 (EDT)
  • Organization: The University of Western Australia
  • References: <cdlj0l$83c$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

In article <cdlj0l$83c$1 at smc.vnet.net>,
 "Aaron Fude" <aaronfude at yahoo.com> wrote:

> New to Mathematica. I can't figure out how to use the if-statement for pure
> flow control.

It's usually better to use Switch.

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

You could use 

 A = {5, 6, 7, 8};

 For [n = 1, n <= 4, n++, 
  Switch[n, 
   1,  a = A[[n]], 
   2,  b = A[[n]], 
   3,  c = A[[n]], 
   4,  d = A[[n]]]
 ]

However, it is much simpler to use Set directly. First clear the values:

 Clear[a,b,c,d]

Then use Set:

 {a,b,c,d} = A

Cheers,
Paul

-- 
Paul Abbott                                   Phone: +61 8 9380 2734
School of Physics, M013                         Fax: +61 8 9380 1014
The University of Western Australia      (CRICOS Provider No 00126G)         
35 Stirling Highway
Crawley WA 6009                      mailto:paul at physics.uwa.edu.au 
AUSTRALIA                            http://physics.uwa.edu.au/~paul


  • Prev by Date: RE: Diophantic Equations with Constraints
  • Next by Date: Quantum Mechanics, Boundary Value Problem
  • Previous by thread: RE: If-statement problems
  • Next by thread: RE: If-statement problems