Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# I don't get the concept of Increment Operators

Papa Miller
Greenhorn
Posts: 11
when it comes to prefix and postfix increments I guess that I understand it when a value is by itself it doesn't matter which side the operator is on

for example A=7; A++; A value is now 8
or A=7; ++A; A value is now 8

but the way I am reading this, there is a difference in how these two terms operate

A++ means "use the current value of A first and then increment A"
++A means "increment A first and then use it"

and now I get

A=7;
B=A++;
The Value of B is 7, and the value of A is 8.

Maybe somebody can shed some light on this??? I am able to write a little program that returns the expected answer, but I still don't understand exactly what is going on here! It says right there that A= 7, how can A be 8?

W. Joe Smith
Ranch Hand
Posts: 710
Because you incremented A using A++. To see the difference, write something along the following:

You will see that in the first s.o.p statement, the value 1 is printed for a, then a is incremented because the second s.o.p. statement prints 2. For the third s.o.p. statement you will see that b is incremented first, as it prints 2. The fourth s.o.p. statement is there to reinforce that b was incremented before printing in the third statement.

Papa Miller
Greenhorn
Posts: 11
Thanks W. Joe,
I tried that and I see that it prints out
1
2
2
2

so your statement makes sense. I am starting to understand it better. I will definitely play around with it some and put some different variables in there.

fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Where this really starts to make a difference is when you have more complicated expressions

The expressions:

7 + a++

and

7 + ++a

will give you different results.

Stephan van Hulst
Bartender
Posts: 6320
78
In my humble opinion, I think the incremental and decremental operators should never be used as part of a larger statement, with the exception of the for-loop.

Always put ++a / a++ / --a / a-- on a new line. Never evaluate them in an expression.

Papa Miller
Greenhorn
Posts: 11
fred rosenberger wrote:Where this really starts to make a difference is when you have more complicated expressions

The expressions:

7 + a++

and

7 + ++a

will give you different results.

Yeah, this it the part that really throws me being totally new to programming. To take Algebra, you assign one value to a variable, period. So here you assign a value to a variable, then something else comes along and changes it, plus there's a question of exactly WHEN does it change. So now I am paranoid b/c a value that I thought I could depend on forever has changed, and I don't know if it can keep changing? Also, depending on when you look at the variable, it might be one thing, or it might be the other. You could blink your eyes and miss it. Is it going to change back when I'm not looking? I don't know. I have some trust issues I guess.

Campbell Ritchie
Sheriff
Posts: 50196
79
If you search you will find questions like this come up frequently. People forget that a++ means there is a value for a and also a value for the whole expression a++, and on that particular line, the two values are different.
There are many people who do use those operators as part of an expression; you can use them in array indices like this

fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
programs would be pretty boring if the value of a variable could never change. and after all, the word 'variable' implies that it could...well...VARY.

if it is important that the value not change, there are ways you can 'lock' it down so that it can never change - and by that, I mean NEVER change. But even then there are subtleties you need to be aware of (eventually, not as a beginner).

One GOOD thing about programming is that the rules are solid and well defined (most of the time)

James Elsey
Ranch Hand
Posts: 228
I asked a similar question recently in this forum, did a bit of reading and wrote up an explanation, if your still intersted in this topic check out my blog post