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.

Recommended Answers

All 4 Replies

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.

thank you for your help. i greatly appreciate it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.