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