       Re: simple question if/while loop

• To: mathgroup at smc.vnet.net
• Subject: [mg40131] Re: [mg40105] simple question if/while loop
• From: Dr Bob <drbob at bigfoot.com>
• Date: Fri, 21 Mar 2003 02:39:24 -0500 (EST)
• References: <200303200835.DAA06871@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```The function returns While[...]*tb, and the value of While[...] is Null, so
multiplying that by a matrix, you get the output you asked for.

Each time a is encountered, a new random number is generated, so the a in
"a > .5" isn't the same as the a in "tb[[n]] = a".  Maybe that's what you
intended, but I wonder.

Continue serves no purpose here, and I recommend against capitalizing names
you make up, to make it easier to distinguish your variables and functions
from the built-ins.

I don't know what you really want the function to do, but here's a
possibility:

testFun[] := Module[{a, tb = Table[0, {4}], n = 1},
While[n < 5,
a = Random[]; If[a > .5, tb[[n]] = a; n++]
];
tb]

However, this does exactly the same thing:

testFun[] := Table[Random[Real, {1/2, 1}], {4}]

Bobby

On Thu, 20 Mar 2003 03:35:00 -0500 (EST), s kp <kpsj2002 at yahoo.com> wrote:

> I am trying to do the following for a test purpose so I am a learning
> mathematica
>
> TestFun[] := Module[{a, tb, n},
> tb = Table[0.0, {4}];
> n := 1;
> a := Random[];
> While[n < 5,
> If[ a > .5,
> (tb[[n]] = a;
> n++;), Continue[]
> ]
> ]
> tb
> ]
>
> The output that I get is
>
> {0.871577 Null, 0.512558 Null, 0.00441806 Null, 0.520545 Null}
>
> Why am I doing wrong here and what is the correct way to do the above
> thing using If and While loop?
>
> thanks
>
>

--
majort at cox-internet.com
Bobby R. Treat

```

• Prev by Date: Re: Re: Simplify[x.Cross[x,y]] does not work.
• Next by Date: Mathematica newbie question : NDSolve
• Previous by thread: simple question if/while loop
• Next by thread: Re: simple question if/while loop