MathGroup Archive 2006

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

Search the Archive

Re: Strange problem with "for" and "while"

  • To: mathgroup at smc.vnet.net
  • Subject: [mg68374] Re: Strange problem with "for" and "while"
  • From: Peter Pein <petsie at dordos.net>
  • Date: Thu, 3 Aug 2006 06:06:34 -0400 (EDT)
  • References: <eapru8$s5i$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

LordBeotian schrieb:
> When I try to compute this:
> 
> For[k = 1, k < 10, k++,
>     i := 0;
>     While [k > 1, i++; k--];
>     Print[i, k]]
> 
> Mathematica outputs a neverending sequence of "1,1" (and it is impossible to 
> abort the computation!)
> 
> When instead I try to manually do the "for" and compute
> 
> k := ...;
> i = 0;
> While [k > 1, i++; k--];
> Print[i, k]
> 
> giving to k all the values 1,2,...,9 the computation are all correct (the 
> outputs are "01", "11", "21", ..., "81".
> 
> I cannot understand what is the problem.
> 

Hi,

your While-loop destroys the k used in For[].

try either
For[k = 1, k < 10, k++,
   i = 0; j = k;
   While[j > 1, i++; j--];
   Print[i, j]]

or simply

Print[#, 1] & /@ Range[0, 8];

hth,
   Peter


  • Prev by Date: Re: problem with Quaternion polynomial root solver
  • Next by Date: Beginner--Adaptive Filters
  • Previous by thread: Re: Strange problem with "for" and "while"
  • Next by thread: Re: Strange problem with "for" and "while"