Operators -- and ++

I don't get this section. Here what's going on in my brain:
- ++ adds a number after variable (Y++ = 2) Assume that Y = 1!)
- ^ does the same thing as ++C or C++? but different meaning. The meaning I don't understand.

Don't point me to C++ tutorial lol. When I read it, I'm like "o.o what did I just read". :|
What I'm about to say is a helpful way of thinking about it. It may not be completely exactly true in all cases :)

When you do ++ or -- you don't just change the value; you also get the value back, for using with something else.

If you aren't actually using it for anything else, like this:
i++; or this ++i; then it doesn't matter.

But what if you're doing this:

1
2
3
int i = 4;
int x;
x = i++;
?

In this case, you get the value of i back to be used before it's changed; in effect, the compiler sets the value of x to equal i (which is 4), and then changes the value of i, so at the end, x = 4 and i = 5.

This:
1
2
3
int i = 4;
int x;
x = ++i;

changes the value of i before it is used, so at the end of this, x = 5 and i =5.


Last edited on
So it just adds one to the variable depends where the -- or ++ at?
so if its C++:
1
2
3
int F = 1;
int c;
c = F++;
C= 1 and f = 2? Does apply to the same thing as -- but you subtract?
Yes.
ok now I understand it..
Topic archived. No new replies allowed.