Re: using while statements
hello kevingnap,
this is one way you can do.
create two int variables,
use one to store accumulated values
something like
Code:
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!
Re: using while statements
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
Re: using while statements
Quote:
Originally Posted by zennehoy
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.
Re: using while statements
Quote:
Originally Posted by potatoCode
hello kevingnap,
this is one way you can do.
create two int variables,
use one to store accumulated values
something like
Code:
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.
Re: using while statements
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.
Re: using while statements
Quote:
Originally Posted by potatoCode
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".
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;
}