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

• To: mathgroup at smc.vnet.net
• Subject: [mg68379] Re: Strange problem with "for" and "while"
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Thu, 3 Aug 2006 06:06:50 -0400 (EDT)
• Organization: The Open University, Milton Keynes, UK
• References: <eapru8\$s5i\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```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[]]

```

• Prev by Date: Re: "No more memory available" -- a recurring problem
• Next by Date: Re: Re: Finding the Number of Pythagorean Triples below a bound
• Previous by thread: Re: Strange problem with "for" and "while"
• Next by thread: find area bounded by three linear functions