f



Strange problem with "for" and "while"

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.

0
8/2/2006 9:40:24 AM
comp.soft-sys.math.mathematica 28821 articles. 0 followers. Follow

3 Replies
199 Views

Similar Articles

[PageSpeed] 10

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.

0
marxer (143)
8/3/2006 10:10:37 AM
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

0
petsie (836)
8/3/2006 10:17:41 AM
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.
> 

Mathematica is right, since it follows and does exactly what you coded 
although possibly not what you intended!

Think carefully about the real value of k; in other word try to think as 
if you were the processor. During the *first* iteration, the While 
statement is *not* executed since k is equal to 1; therefore, 
Mathematica prints 01 -- you can verify that by adding an unconditional 
Break[] expression after the Print statement [1]. Then, the increment 
expression of the For loop is executed and the value of k reaches 2.

 From the second iteration onwards, the While loop is going to be 
executed every time since k will always be equal to two when reaching 
the While statement. Let's see what happens here. k is equal to two. i 
is set to to zero. Is k greater than one? Yes, thus the While loop is 
executed: i is incremented by one from zero to one, and k is decremented 
by one from two to one. Then, the condition to the While loop is tested 
again and failed to be met since k is now equal to one. Mathematica ends 
the While loop and print 11.

We are back to the For loop now, and k is incremented from one, its 
current value, to two. Then, i is set to zero and the answer to the 
question "Is k greater than one?" is positive since the current value of 
k is two. Then... but I imagine you have seen the pattern by now, 
pattern of a never ending loop indeed.

One way of solving this issue is using an additional variable in place 
of k within the While loop. In the following example, you can see that j 
is always displayed with the value one. However, k is correctly 
incremented and the iterations end.

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

yields

0" "1" "1
1" "1" "2
2" "1" "3
3" "1" "4
4" "1" "5
5" "1" "6
6" "1" "7
7" "1" "8
8" "1" "9

HTH,
Jean-Marc

[1] The following loop ends after the first iteration:
For[k = 1, k < 10, k++, i := 0; While[k > 1, i++; k--]; Print[i, k]; 
Break[]]

0
8/3/2006 10:22:45 AM
Reply: