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
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?
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.
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Joined: Sep 22, 2010
Thanks W. Joe,
I tried that and I see that it prints out
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.
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.
Joined: Sep 22, 2010
fred rosenberger wrote:Where this really starts to make a difference is when you have more complicated expressions
7 + a++
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.
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
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)