Incrementing by 2 not 1

This is a discussion on Incrementing by 2 not 1 within the C++ Programming forums, part of the General Programming Boards category; I'm creating a for loop that has to increment by 2 not by 1. For example something like this. for ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    4

    Incrementing by 2 not 1

    I'm creating a for loop that has to increment by 2 not by 1.
    For example something like this.
    for ( i = 3; i < sqrt(n); i++ ) "this adding by one"
    could i write
    for ( i = 3; i < sqrt(n); i2++ )
    What is the proper way of writing this so the counter increments by 2
    Thanks
    Matt

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    i+=2

    gg

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Code:
    for ( i = 3; i < sqrt(n); i+=2 )

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    4
    Thanks man. quick response, very cool.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    This is valid in C++ (and just about every programming language) too:

    i = i + 2; // Increment by 2
    Code:
    for ( i = 3; i < sqrt(n); (i = i+2) )
    But, people will know you're a C++ wimp if you do it that way.

    And FYI, you can put any expression (anything that evaluates to a number) in the for-statement.

    This would be valid C++ too:
    Code:
    for ( i = 3; i < sqrt(n); (i = (i*2) +5 ) )
    Last edited by DougDbug; 10-14-2004 at 02:15 PM.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    this is a much more efficient way to increment by 2

    i = (i*10)/(2.5*4)+((2^0*2))
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    .....nah, just kidding
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. setting timers and incrementing values
    By djayz in forum C Programming
    Replies: 7
    Last Post: 03-23-2005, 07:58 PM
  2. Another new person - incrementing
    By MB1 in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2005, 05:54 AM
  3. incrementing file names
    By Rogue5 in forum C Programming
    Replies: 3
    Last Post: 02-11-2005, 07:42 AM
  4. Incrementing a number each time a function is called.
    By Coder87C in forum C++ Programming
    Replies: 17
    Last Post: 01-31-2005, 06:04 PM
  5. Incrementing variables
    By Bazz in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2001, 08:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21