We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
++ and -- a bug? (Read 810 times)
++ and -- a bug?
Oct 4th, 2009, 11:44am
 
The reference material implies that i++ should be a substitute for i+1, but it doesn't seem so. At the moment, in fact, I can't get ++ or -- to do what they are supposed to do in any circumstance. Am I missing something, or is this a bug?
Re: ++ and -- a bug?
Reply #1 - Oct 4th, 2009, 11:54am
 
To clarify, i++ is equivalent of i = i+1, or i += 1.



int i = 1;
println (i);
i++;
println (i);
i = i + 1;
println (i);
i += 1;
println (i);

would produce:

1
2
3
4


An important thing to note is that i++ would be evaluated "after" the enclosing statement, whereas ++i would be evaluated "before".  Hence:

int i = 1;
println (i++);
println (i);
println (++i);
println (i);

would produce:

1
2
3
3
Re: ++ and -- a bug?
Reply #2 - Oct 4th, 2009, 11:07pm
 
sonic wrote on Oct 4th, 2009, 11:44am:
I can't get ++ or -- to do what they are supposed to do in any circumstance.
You should say what are the circumstances so we can see what you are doing wrong... A bug in Java itself for something so fundamental is unlikely... Smiley
sw01 made a good explanation, I will add that if you do a = i++;, it won't be the same thing as a = i + 1; but a = ++i; will be the same thing, with a side effect (value of i is changed).
Re: ++ and -- a bug?
Reply #3 - Oct 5th, 2009, 1:03pm
 
Thanks, sw01 - that explains my problem. I only wish the online reference materials made this kind of thing more clear. The explanation given of ++ doesn't show how it would be used in loops, which seems pretty basic.
Page Index Toggle Pages: 1