do while bug?



Le 17/12/2010 01:40, Niitsuma Hirotaka a ?crit :
> It is seem to do while loop result depend on selection initial variable
>
> For example
>
> Case init variable k
> ------------------
> (%i34) a:1;
> (%i35) for k:1 while a<  10 do (a:a*3,display(a) ) ;
>                                       a = 3
>
>                                       a = 9
>
>                                      a = 27
>
> (%o35)                               done
> ------------------
>
> Case init variable a
> ------------------
> (%i36) for a:1 while a<  10 do (a:a*3,display(a) ) ;
>                                       a = 3
>
>                                      a = 12
>
> (%o36)                               done
> ------------------
>
> In this case display(a=27),does not executed.
> Is this bug?
>    
No, it is the normal behaviour : in the second command, you change the 
value of "a" in two different ways : increasing it as a loop index and 
multiplying it by 3 explicitely.

First command :
k=1  a=1       since a<10 then  a-->1*3=3. And k-->k+1=2 at end of loop
k=2  a=3 since a<10 then  a-->3*3=9. And k-->k+1=3 at end of loop
k=3  a=9 since a<10 then  a-->9*3=27. And k-->k+1=4 at end of loop
k=4  a=27 since a>=10 then stop

Second command
a=1       since a<10 then  a-->1*3=3 (display it). And a-->a+1=4 at end 
of loop
a=4 since a<10 then  a-->4*3=12 (display it). And a-->a+1=13 at end of loop
a=13 since a>=10 then stop

Note that if you ask for the value of a after both commands, you get 27 
each time.
This is expected for the first one. For the second try, it shows that 
the only variable "a" changing during the loop is the local loop 
variable, not the global "a" which stays with its value 27 of the first try.

Eric