MathGroup Archive 2003

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

Search the Archive

Re: simple question if/while loop


Hi,

you forgott a semicolon

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[]]
     ]; (* <==== here ! *)
    tb
  ]

Regards
  Jens

s kp 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
> 
>


  • Prev by Date: Re: UnitStep
  • Next by Date: Re: Finding solutions to differential eqns
  • Previous by thread: Re: simple question if/while loop
  • Next by thread: Re: simple question if/while loop