CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2008
    Posts
    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

  2. #2
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    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.

  3. #3
    Join Date
    Mar 2003
    Posts
    126

    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

  4. #4
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    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.

  5. #5
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    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.

  6. #6
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    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.

  7. #7
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    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;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured