programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Liutauras Vilda
Sheriffs:
• Rob Spoor
• Junilu Lacar
• paul wheaton
Saloon Keepers:
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Carey Brown
• Scott Selikoff
Bartenders:
• Piet Souris
• Jj Roberts
• fred rosenberger

I don't get the concept of Increment Operators

Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:
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?

Ranch Hand
Posts: 710
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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.

lowercase baba
Posts: 13053
67
• Number of slices to send:
Optional 'thank-you' note:
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.

Saloon Keeper
Posts: 13979
315
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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.

Marshal
Posts: 75836
361
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 13053
67
• Number of slices to send:
Optional 'thank-you' note:
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)

Ranch Hand
Posts: 231
• Number of slices to send:
Optional 'thank-you' note:
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

 Don't get me started about those stupid light bulbs.