Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics (Retired) Fundamentals of C Operators and Expressions

stella J
stella J
2,234 Points

between --a and a--

I can understand how value 'a' has been changed but don't get how other values are changed.

First why is 'c' 9? it's statement says 'c = a ++'...... I could understand this when I think of c as original value a..

but what I really don't understand is the change of value d. when it was 'd = --a'. it is 9 while 'd = a--', it is 10. how? why?

help me out.

2 Answers

Mike Baxter
Mike Baxter
4,442 Points

Oh, that's interesting, I'd never thought of that before! But it makes sense.

--a and a-- do the same operation to the variable a. They both subtract 1 from a.

The only difference is in how it affects something else, like this variable d (or whatever you want to call it, it's just a variable).

Now, when we call

d = --a;

the operation on a happens first, before the value of d gets assigned. So d takes on the modified value of a. (Not the original value).

Alternatively, when we call

d = a--;

the operation on a happens after d gets assigned whatever a was initially. That is to say, after d takes the initial value of a, only then do we subtract 1 from a.

Personally I'm going to try to memorize it by thinking like this, the d variable is going to eat up the a as soon as it can. If it can catch it before the operation happens to a , then it will take on the original value. But if the operation happens first, then d takes the modified value of a , NOT the original.

Hopefully that helps! Let me know if I can clarify anything.

stella J
stella J
2,234 Points

thank you ! it really helps me a lot : )

I haven't taken the iOS course but I'm pretty sure the problem you're having with understanding this concept is similar to javascript. --a and a-- is kinda the same thing but the only difference is when echoed you'll get the value of the variable before the arithmetic operator is applied.