Extension 6: Incrementation

In the last Lesson, I used the following expression on Line 11 of the first example:

n = n + 1

This is perfectly valid code. The associativity of the equals operator (=) is right-to-left—this means that whatever is on the right of the operator is evaluated first, then the result is assigned back to the variable or identifier on the left side. So in this case, the program first determines the value of n + 1, and assigns this new value back to n. This method is fine, but there are more elegant solutions.


The following is completely equivalent to the above example:

n += 1;

What this is saying is to evaluate the result of n + 1 and assign it back to n—the identical meaning to the above example. The difference is that in this case the code is clearer, and much more concise. The following are all valid:

n += 3;    // Same as n = n + 3;
n -= 3;    // Same as n = n - 3;
n *= 3;    // Same as n = n * 3;
n /= 3;    // Same as n = n / 3;
n %=3;     // Same as n = n % 3;

In each case, the value of n operator 1 is evaluated, then the result is assigned back to n. This construct is highly recommended, and is used almost exclusively in place of the original.

++ and ––

That should be two minus signs; WordPress is over-optimizing the typography.

Returning to the concept of loops, most loops execute a specific number of times; this number is often determined by an upper limit value, and a counter is used to keep track of the increments until the maximum is reached. In these cases, the counter would be incremented by 1. In the original example, the code to do this was

n = n + 1;

As described above, n += 1; is also valid.

But incrementing (or decrementing) by 1 is such a popular thing to do in code (believe it or not) that Objective-C (and plain C) has a special construct to do just that:

n++;     // Increment by 1
n--;     // Decrement by 1 

As with the above construct, the double-plus or double-minus is used almost exclusively when the program needs to increment or decrement by 1. At the very least, it saves you typing; at best, compilers can produce more concise and efficient code when the double-plus or double-minus is used (Prata, C Primer Plus, 5th Edition, SAMS: Page 146). Note that these operators increment by exactly 1, and can only be used with integers.

One thing to note is that the increment and decrement operators have a prefix and postfix version.

Prefix versus Postfix

Compare the following statements:

int n = 4;
if (n++ < 5)     // Case 1
   NSLog(@"n is %d", n);
n = 4;           // "Reset" value of n
if (++n < 5)     // Case 2
   NSLog(@"n is %d", n);
NSLog(@"n is %d", n);     

The output is:

n is 5
n is 5 

There are three NSLogs in the code, yet only two lines of output. This reflects a subtlety in the increment/decrement operators. Why does this happen?

In Case 1, the value of n is compared with 5 first, and then incremented. n was initially set to 4; 4 is less than 5, so the initial NSLog executes. However, by that point, n had been incremented to 5.

In Case 2, the value of n is first incremented, then compared to 5. So n first becomes 5; 5 is not less than 5, and so the second NSLog does not execute. However, the final NSLog displays the value of n, which is now 5.

In the majority of cases, where only the end result is concerned, using the prefix or postfix does not matter—in either case, the end result is 5. However, for intermediate cases, such as the code within the if() statements, the choice does matter.

Finally, happy holidays to all my readers! Thanks for stopping by, and I hope you’ll come back for more. We’ll be getting into the good stuff (objects and more advanced programs) soon—probably before 2011 rolls around. Merry Hanukkah (and Kwanzaa, and whatever else is politically correct)! 😀

Leave a comment


  1. anonse

     /  December 23, 2010

    I really enjoyed the blog post. It is always nice when you can not only be informed, but also entertained!

  2. Iain Henderson

     /  February 9, 2012

    Did you mean to say n *= 3; // Same as n = n * 13; as opposed to n *= 3; // Same as n = n * 3;

  3. I dont understand this at all. what does incrementing/decrementing even do? there seems to be no difference for comparing n to 5 and comparing ++n to 5 or n++ to 5 or –n to 5 or n– to 5. which brings me to another thing, whats the difference between ++ and — and which side you put them on (++n VS. n++)???

    • What does incrementing/decrementing even do?
      The increment/decrement operators increase or decrease the value of an integer variable by one. It’s really useful in a loop:
      for (int i = 0; i < 5; i++) rather than the longer for (int i = 0; i < 5; i = i + 1).

      Comparing n to 5 and ++n to 5…
      If n starts as 5, (n++ == 5) will return true. However, n will now be 6. Similarly, if n starts as 5, (n– == 5) will return true. However, n will now be 4.
      If n starts as 5, (++n == 5) will not return true. n will now be 6. Similarly, if n starts as 5, (–n == 5) will not return true. n will now be 4.

      What's the difference between ++ and — and which side you put them on
      ++ increases by one, — decreases by one.
      However, as the above section shows (try out the code! Just create a new basic project), the side that you put it on matters. Putting the operator to the right will perform the comparison first, then the increment or decrement. So (n++ == 5) will compare n to 5 first, then increase n by one. However, putting the operator to the left will perform the increment or decrement first, then the comparison. So (–n == 5) will decrease n by 1 first, then compare it to 5.

      Does that help?

  4. yes helped a lot! I completely understand now THANK YOU!

  1. Learn Objective-C in 24 Days « Programming for iOS

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

  • Welcome

    My goal is to make CupsOfCocoa into a beautiful source for beginners to the iPhone platform to get started. Subscribe below for more, and stay tuned!

  • Contact Me

    If you need to contact me for any reason, feel free to send me an email.
  • The Giving Spirit

    If you've found this site helpful, would you consider donating a little sum? Any amount is appreciated...Thanks so much!

  • Roadmap

  • Enter your email address to follow this blog and receive notifications of new posts by email.

    Join 222 other followers

  • Back to the Past

    December 2010
    S M T W T F S
    « Nov   Jan »
  • Time Machine

  • You count!

  • Worldwide Stats

    free counters
  • Advertisements
%d bloggers like this: