MATLAB: How Do I to Use a While Loop to Eventually Get to a Specific Number

dot operatorfprintfinputlooploopspirepeatingsumwhile loop

while S;
n=input('please input a value for n: ');
b=1./(n.^2);
S=sum(b);
if (.999*(pi^(2)/6))<=S && S<=(1.001*(pi^(2)/6))
fprintf('the sum for that value of n is %.5f\nCongrats! the sum is close enough to pi^2/6\n',S)
else
fprintf('the sum for that value of n is %.5f\n Please input a higher value for n.',S)
end
end
So, I'm trying to write a code where the user has to input a value for n until it gets with .1% of pi^2/6. I'm trying to do this using a while loop but my code repeats even when I get close enough to the number. I'm not sure why it keeps asking to input a value for n after that. Any help would be greatly appreciated!

Best Answer

There is no condition for S. Every time, it keeps operating the while loop. Change the code to the following:
while true
n=input('please input a value for n: ');
b=1./(n.^2);
S=sum(b);
if (.999*(pi^(2)/6))<=S && S<=(1.001*(pi^(2)/6))
fprintf('the sum for that value of n is %.5f\nCongrats! the sum is close enough to pi^2/6\n',S)
break;
else
fprintf('the sum for that value of n is %.5f\n Please input a higher value for n.',S)
end
end