if alpha and beta were int variables, the statement alpha = --beta is equivelent to what statement?
also what would alpha = beta++ be equivilent to what statement. i know you add 1 to the first one but it doesn't seem right. any help would be appreciated.

10 Years
Discussion Span
Last Post by dblbac

When you say --x, x is decremented by one before being used in the expression. Likewise, ++x increments x by one before using it in the expression. x++ or x-- will perform the increment or decrement after using the value in the expression.

int x = 0;
int y;

y = x++; // y is set to 0
y = ++x; // y is set to 2

The -- and ++ operators are initially confusing. They are unique to C and C++.

What --a means is "make a = a - 1 before you do anything else." Same with ++a : a is modified first.

The a-- means "make a = a -1 after you do everything else." Likewise with a++ .

C and C++ like shorthand statements. So alpha = beta++ is the same as the two statements: alpha = beta; beta = beta + 1; Hope this helps.

[EDIT] Alas, too slow...


>They are unique to C and C++.
Not really. While ++ and -- were invented for B (and inherited by C), quite a few languages now use them.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.