Click to See Complete Forum and Search --> : using while statements


kevingnap
April 1st, 2008, 11:02 PM
I need some help writing a programme that uses the while statement.
I need to use the "while" statement to sum the numbers from 1 to 10
using iSum variable for sum and iNumber for number.
I would really appreciate it if anyone could help me out as this is my first time writing code.

Thanks
Laters

potatoCode
April 1st, 2008, 11:12 PM
hello kevingnap,

this is one way you can do.

create two int variables,
use one to store accumulated values
something like

int x, sum;
while x < 11;
sum += x++; // is equivelent to:
// sum = sum + x;
// ++x;

print sum
hope this helps

PS.
I think I might have overlooked what you said.
If this is your first time writing code and you are serious about programming,
I recommend getting yourself a book because there are benefits of having one instead of reading up on the tutorials on the internet.

good luck!

zennehoy
April 2nd, 2008, 02:50 AM
Be warned, potato's Code won't necessarily work, for possibly non-obvious reasons to a beginner.
Let's just say it has to do with variable initialization, see if you can figure it out from there.
Cheers,
Zen

potatoCode
April 2nd, 2008, 01:50 PM
Be warned, potato's Code won't necessarily work, for possibly non-obvious reasons to a beginner.
Let's just say it has to do with variable initialization, see if you can figure it out from there.
Cheers,
Zen

Hello kevingnap,

zennehoy has a really, very good point here.
(although the example I gave you was in pseudo-code and the variable sum would have been likely initialized in real code before it was used as an right operand in a compound assignment)
it illustrates a common mistake about declaration/definition.

hope you can see what he means.

kempofighter
April 2nd, 2008, 02:03 PM
hello kevingnap,

this is one way you can do.

create two int variables,
use one to store accumulated values
something like

int x, sum;
while x < 11;
sum += x++; // is equivelent to:
// sum = sum + x;
// ++x;

print sum
hope this helps

PS.
I think I might have overlooked what you said.
If this is your first time writing code and you are serious about programming,
I recommend getting yourself a book because there are benefits of having one instead of reading up on the tutorials on the internet.

good luck!

Since the while loop is terminated with a semicolon, I'm not sure how the summation will work at all. Won't this result in an infinite loop since x will never get incremented? The while loop has no body and will loop forever.

Also, is this supposed to be c++ code? Why did you indent the while underneath the variable declarations? That makes no sense to me.

potatoCode
April 2nd, 2008, 02:23 PM
Hello kevingnap,

my code is what sometimes referred to as "psuedo-code"
(I might not have done a good job about it)
to illustrate the flow and the steps involved in it.

I'm a beginner myself,
and one thing I realized is that understanding the flow of the program we design is as much important as wrting a correct code.

and to kempofighter,
I wonder, why indenting the while statement makes no sense to you.
I think you are blowing things out of proportion here which will only make OP more confused and that's not helping him at all.

kempofighter
April 2nd, 2008, 02:41 PM
and to kempofighter,
I wonder, why indenting the while statement makes no sense to you.
I think you are blowing things out of proportion here which will only make OP more confused and that's not helping him at all.

Because it looks bizarre, and it would have been much simpler to write code that works instead of "psuedo-code". In C++ code, I normally only see a line indented when it is within a new block. I didn't realize you were writing "psuedo-code".


#include <iostream>

int main()
{
int x(0), sum(0);
while (x < 11)
{
sum += x++; // is equivelent to:
// sum = sum + x;
// ++x;
}
std::cout << "sum = " << sum << std::endl;
}