Do while to while loop

Feb 5, 2014 at 2:30am
Could someone tell me if the folllowing looks corrects
i changed this to a while loop:
int n;
cin >> n;
double x = 0;
double s;
do
{
s = 1.0 / (1 + n * n);
n ++;
x = x + s;
}
while ( s > 0.01);




loop?
1
2
3
4
5
6
7
8
9
10
11
 int n;
cin >> n;
double x = 0;
double s;
s = 1.0/(1 + n * n)
while ( s > 0.01)
{
    s = 1.0/(1 + n * n)
n++
 x = x + s
}	


Thanks!
Last edited on Feb 5, 2014 at 2:57am
Feb 5, 2014 at 2:35am
Looks correct to me.
Feb 5, 2014 at 12:48pm
earlier code!
no matter s>0.01 or not, the loop will execute one time INCREMENTING n and executing x=x+s..

new code!

s = 1.0/(1 + n * n);

will be executed one time no matter s>0.01 or not BUT! increment in n and
x=x+s will be done only if s>0.01;
So,
you should better look at your objective(what u want to do)
either make x and n dependent on s>0.01 condition the first time or not...
Topic archived. No new replies allowed.