[Date Index]
[Thread Index]
[Author Index]
Re: Strange problem with "for" and "while"
*To*: mathgroup at smc.vnet.net
*Subject*: [mg68367] Re: Strange problem with "for" and "while"
*From*: "Norbert Marxer" <marxer at mec.li>
*Date*: Thu, 3 Aug 2006 06:06:24 -0400 (EDT)
*References*: <eapru8$s5i$1@smc.vnet.net>
*Sender*: owner-wri-mathgroup at wolfram.com
Hello
You will never leave the Foor loop because the While loop in the body
of For[start,test,incr,body] always resets k to 1.
When you enter the Foor loop you
(1) (start) set k equal to 1
(2) (test) test if k < 10 (which is True)
(3) (body) evaluate the body, where your While loop does nothing (for
k=1)
(4) (incr) increment k, which sets k equal to 2
(5) (test) test if k < 10 (which is True)
(6) (body) evaluate the body, where your While loop sets k equal to 1
and increases i to 1
... and repeatedly evaluate steps (4) to (6)
If you use two different variables (kk, k) in the For and While loop
you get what you want:
For[kk = 1, kk < 10, kk++,
i := 0; k := kk;
While [k > 1, i++; k--];
Print[i, k]]
Best Regards
Norbert Marxer
www.mec.li
LordBeotian wrote:
> 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.
Prev by Date:
** Re: Combining differnt plots in one graphic**
Next by Date:
** Re: NonLinearRegress Problem**
Previous by thread:
** Re: Strange problem with "for" and "while"**
Next by thread:
**Re: Strange problem with "for" and "while"**
| |