Learn Objective-C: 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:

  1. n += 3;    // Same as n = n + 3;
  2. n -= 3;    // Same as n = n - 3;
  3. n *= 3;    // Same as n = n * 3;
  4. n /= 3;    // Same as n = n / 3;
  5. 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:

  1. n++;     // Increment by 1
  2. 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:

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

The output is:

  1. n is 5
  2. 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.

This post is part of the Learn Objective-C in 24 Days course.

Author: Feifan Zhou