|
-
April 1st, 2008, 11:02 PM
#1
using while statements
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
-
April 1st, 2008, 11:12 PM
#2
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!
Last edited by potatoCode; April 2nd, 2008 at 12:06 AM.
-
April 2nd, 2008, 02:50 AM
#3
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
-
April 2nd, 2008, 01:50 PM
#4
Re: using while statements
 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.
-
April 2nd, 2008, 02:03 PM
#5
Re: using while statements
 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.
-
April 2nd, 2008, 02:23 PM
#6
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.
-
April 2nd, 2008, 02:41 PM
#7
Re: using while statements
 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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|