Thread: Whats the advantage of using ++ and --?

  1. #1
    Registered User roll cast's Avatar
    Join Date
    Nov 2011
    Location
    NI
    Posts
    9

    Whats the advantage of using ++ and --?

    Why when making a loop do you use

    Code:
    for (int i = 0; i< 9; i++)
    {
    blahblahblah
    }
    Instead of

    Code:
    for (int i = 0; i < 9; i + 1)
    {blahblahblahblah
    }
    Thanks
    AL

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    In your example, one will work and the other will produce an infinite loop (you need i+= 1 instead of i+1, since the latter doesn't update i). But after fixing that, there's no real reason to prefer one over the other.

    In some contexts, it can save some typing :

    Code:
    while (i  < len)
      array[i++] = 10;
    or

    Code:
    while (++i < len)
      array[i] = i;
    compared to

    Code:
    while (i < len)
    {
      array[i] = 10;
      i += 1;
    }
    In more complex objects, pre-incrementing/decrementing (--a, ++a) can be slight more efficient than adding one since the compiler might be able to avoid allocating temporaries to do the math. But this shouldn't be a major concern - a decent compiler can eliminate most of the overhead in many cases.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by KCfromNC View Post
    In some contexts, it can save some typing :

    Code:
    while (i  < len)
      array[i++] = 10;
    Loops which advance an index on each iteration should usually be written as for-loops, not while-loops. Loops where the index is incremented in some bizarre spot in the middle of the loop, hidden inside the argument list to a function call, etc can be infuriating to debug and difficult to maintain.

    It may not seem so bad with the above example, but imagine a 30 line loop with complicated computations and if-statements, maybe some nested loops, and somewhere in there is buried an "i++".

    If i is incremented regularly, every time through the loop, then say so by writing a for-loop, assuaging my doubts and fears that something bizarre is going on that I don't understand.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User inequity's Avatar
    Join Date
    Nov 2010
    Location
    Seattle, Washington
    Posts
    59
    In terms of i++ and i+=1, they are both pretty readable, and will generally be compiled into the same thing.

    It's really your call, but because the increment operators are part of the language, I think it's more clear to use them.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by inequity View Post
    In terms of i++ and i+=1, they are both pretty readable, and will generally be compiled into the same thing.
    Not precisely true. Some machines support instructions for incrementing and decrementing, that are distinct from addition and subtraction.

    While, in principle, compilers can (and often do) reduce incrementing and adding of one to the same set of operations, they are not guaranteed to do so.

    Generally, when looping over arrays, I prefer to use ++x over x += 1. My main reason is that, generally, I prefer to avoid "magic values" in code, even in simple cases like this (the expression x += 1 results in a magic value of 1 visible in code, ++x does not, even if the two expressions achieve the same thing).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Another important aspect of ++,-- is that the l-value is only evaluated ONCE.

    Code:
    #include <stdio.h>
    int here ( ) {
      printf("here called\n");
      return 0;
    }
    int there ( ) {
      printf("there called\n");
      return 0;
    }
    int main ( ) {
      int arr[10] = { 0 };
      arr[here()]++;
      arr[there()] = arr[there()] + 1;
      return 0;
    }
    So if you have a complicated expression, the result is simpler code.

    Also, you're not likely to end up with
    arr[complex_edited_expression] = arr[complex_original_expression] + 1;
    bugs.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Widescreen advantage in multiplayer?
    By glo in forum Tech Board
    Replies: 5
    Last Post: 07-31-2009, 09:15 AM
  2. Speed/size advantage to SQL?
    By MK27 in forum Tech Board
    Replies: 15
    Last Post: 06-26-2009, 11:28 AM
  3. Advantage of C in computer graphics?
    By NoobieGecko in forum C Programming
    Replies: 8
    Last Post: 08-29-2008, 04:55 PM
  4. taking advantage of hyperthreading?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 08-21-2006, 11:49 PM
  5. Advantage of C to C++
    By nipun in forum C++ Programming
    Replies: 18
    Last Post: 09-21-2004, 06:53 AM