# Problem in Dynamo 2. Loop while together with Integer Slider doesn't work

if the variable n in the code below is initialized from e.g. Integer Slider

n;
[Imperative]
{
Sn = 0;
i = 1;
while (i <= n)
{
si = 2 * i - 1;
Sn = Sn + si;
i = i + 1;
}
return Sn;
};
the correct result is calculated only the first time (e.g. for n = 5 you get Sn = 25 ). After changing the value on the Integer Slider, e.g. 6 (i.e. n = 6) the calculated value doesn’t change.
But, if the value of n is initialized in the code directly, i.e.
n = 6;
[Imperative]
{
Sn = 0;
i = 1;
while (i <= n)
{
si = 2 * i - 1;
Sn = Sn + si;
i = i + 1;
}
return Sn;
};
the correct results are obtained at each initialized value of variable n.
What’s even more shocking, the described problem does not appear in the case of loop for.
Initializing the variable n from both the Integer Slider and manually each time causes the correct execution of the code and the correct value of the sum Sn is calculated:
n;
[Imperative]
{
Sn = 0;
for (i in 1…n)
{
si = 2 * i - 1;
Sn = Sn + si;
}
return Sn;
};