Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need Help: C++ incriments in a for loop

    So I'm trying to use a for loop for something, but I need the increments to increase by two, not just one.

    I.E. :
    for (int i = 0; i < number; (i++)++) and also (i+1)++

    It won't compile, always hanging up on this line; how do I do this?

  • #2
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    Perhaps:
    for (int i = 0; i < number; i+2) {...} ?

    EDIT: Oops, error above. Should read
    for (int i = 0; i < number; i += 2) {...}
    Last edited by Gox; 08-30-2008 at 11:35 PM. Reason: Bad Code

  • #3
    New Coder
    Join Date
    Aug 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, almost soon as I posted I realized it. I feel silly

  • #4
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    for (int i = 0, i < number; i = i + 2)

  • #5
    eak
    eak is offline
    Regular Coder eak's Avatar
    Join Date
    Jun 2002
    Location
    Nashville, TN
    Posts
    354
    Thanks
    0
    Thanked 26 Times in 26 Posts
    even shorter:
    Code:
    for (int i = 0, i < number; i+=2)
    eak | "Doing a good deed is like wetting your pants; every one can see the results, but only you can feel the warmth."


  •  

    Posting Permissions

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