(++) != (+=1) ?

Robert

Active Member
Moderator
Joined
Apr 1, 2005
Messages
10,817
Reaction score
6,536
I have a bit of code where I'm trying to increment values by one.

to achieve this I'd been using (variable++) but was not getting the expected outcome.
As an experiment, I changed it to (variable+=1) and now get the expected outcome.

I was under the impression that both expressions mean exactly the same thing.

Anyone seen this before or have an explanation for a coding newbie?
 
I have a bit of code where I'm trying to increment values by one.

to achieve this I'd been using (variable++) but was not getting the expected outcome.
As an experiment, I changed it to (variable+=1) and now get the expected outcome.

I was under the impression that both expressions mean exactly the same thing.

Anyone seen this before or have an explanation for a coding newbie?

There are both prefix and postfix increment/decrement operators in C and related languages. It's an easy gotcha for those new to it:

++variable increments the variable by one and gives the incremented value
variable++ increments the variable by one and gives the value it had before being incremented.
 
First, which language are you using? Second, in C/C++ and Java, the ++ operator is done after reading the value of variable. So something like:
printf("%d", variable++);
Would print the value of variable BEFORE it was incremented. If you want to increment before the print, use ++ operator before the variable:
printf("%d", ++variable);

Note, that some languages don't clearly define if the ++ operator placed before the variable is calculated when it is referenced or before the entire statement. So this may give different results depending on compiler:
printf("%d %d", variable, ++variable);

I think Java is more consistent.

Also, if you're using C++, and the variable you're incrementing is some funky object, then well, good luck. :D
 
@Mike:
It's Java.

Here's a snippet:
if (matches.getGoalsA() == matches.getGoalsB())
{
System.out.println(sortedTeams[j].getTeamPoints()+ " pts & " + sortedTeams[j].getTeamDrawn() + " drawn before draw");
sortedTeams[j].setTeamPoints(pts += 1);
sortedTeams[j].setTeamDrawn(drawn += 1);
System.out.println(sortedTeams[j].getTeamPoints()+ " pts & " + sortedTeams[j].getTeamDrawn() + " drawn after draw");
}

This outputs:
1 pts & 1 drawn before draw
2 pts & 2 drawn after draw

If I change (pts +=1) to (pts++) and do the same for "drawn", I get :
1 pts & 1 drawn before draw
1 pts & 1 drawn after draw

Seems weird.
 
@Rob

Use ++pts and ++drawn. That will increment the variable first and the result of the expression will be the incremented value in each case.
 
@Karlos
Will do but any explanation why +=1 increments and ++ doesn't, given they both come after the variable?
 
@Karlos
Will do but any explanation why +=1 increments and ++ doesn't, given they both come after the variable?

var++ does increment the variable. You don't see it because you aren't using the variable again afterwards, you are using the immediate result of the var++ operation as your method call parameter. That means it gets the value var had before var was incremented. As you don't reference var again in that block of code, you don't observe it.
 
var++ does increment the variable. You don't see it because you aren't using the variable again afterwards, you are using the immediate result of the var++ operation as your method call parameter. That means it gets the value var had before var was incremented. As you don't reference var again in that block of code, you don't observe it.

OK, I'm learning here so thanks. :D

So what is it about +=1 that's differtent to ++?
 
OK, I'm learning here so thanks. :D

So what is it about +=1 that's differtent to ++?

Other than the result of the expression, nothing. It's a historical C quirk. The prefix and postfix operators often had direct mappings to specific instruction types, particularly where pointers were concerned. Think of the 68000 and it's predecrement/postincrement addressing modes for example.
 
Other than the result of the expression, nothing. It's a historical C quirk. The prefix and postfix operators often had direct mappings to specific instruction types, particularly where pointers were concerned. Think of the 68000 and it's predecrement/postincrement addressing modes for example.

Thanks. Are there any other quirky occassions when ++ and +=1 give different results?

-EDIT-
and does this mean that "var+=1" is treated like a prefix operator, whilst "var++" is postfix?
 
Thanks. Are there any other quirky occassions when ++ and +=1 give different results?

If it's postfix "var++" then it will always be one off relative to "var+=1".

-EDIT-
and does this mean that "var+=1" is treated like a prefix operator, whilst "var++" is postfix?

Yes. Postfix is the one that seems odd. "++var" and "var+=1" are essentially equivalent (they will differ in terms of precedence in more complex expressions only).

When you write something like this:

result = var++;

what it basically ends up being like is something more like this:

result = var; var+=1;

In your example code, you did this:

someFunction(var++);

which, if you follow the above "expansion", will reveal that "someFunction()" receives the original value of "var" as it's argument - it never gets to see the value var "ends up" with.

Instead you could write:

someFunction(++var);

Since it's a temporary and you aren't using "var" elsewhere in your code block after you incremented it, probably the clearest way to write it would be:

someFunction(var+1);

which makes it a lot easier to understand what the intention was.
 
@Karlos:

Thanks very much. Makes more sense now.
 
-EDIT-
and does this mean that "var+=1" is treated like a prefix operator, whilst "var++" is postfix?

Personally I have no idea why ++var and var++ were included in Java. Back in the early days of C it could be of use for writing tight code that a compiler could easily generate code for that ran on a CPU that implemented pre-increment and post-increment instructions but modern compilers and optimizers are pretty good at finding those tricks themselves.

When used on there own var++; and ++var; have the same effect and if your intent is to increment a variable it's best to do it as a separate operation than to be clever and mix it with another.

var++;
fUse(var);

and

fUse(var);
var++;

are easier to read and understand when scanning a lot of code than the equivalent fUse(++var); and fUse(var++);

If you are passing something in a function then it is generally best to avoid performing mathematics on it in the function call. Most code that is doing significant work out in the real world will be read much more often than it is written.
 
@Fluffy:

That's useful info. Cheers.
Like I say, I'm just getting into this so every day is a (somewhat busy) school day. :)
 
@Fluffy:

That's useful info. Cheers.
Like I say, I'm just getting into this so every day is a (somewhat busy) school day. :)

Whatever I say, you'll find people who say the opposite - but I've outclevered myself a few times trying to be more concise than necessary.
 
Ya, all good comments above. I'm not sure if a proper explanation as to why the apparent differnce in results. Even if you adopt Fluffy's advice of avoiding the ++ operator, it's still good to know why you see a difference.

The ++ postfix operator is performed after the statement, where as the (var += 1) operation is performed mid-statement, in fact, performed before the call to setTeamPoints().

Code:
sortedTeams[j].setTeamPoints(pts++);
is translated to:
Code:
sortedTeams[j].setTeamPoints(pts);
pts = pts + 1;

where as
Code:
sortedTeams[j].setTeamPoints(pts += 1);
is translated to:
Code:
pts = pts + 1;
sortedTeams[j].setTeamPoints(pts);

Does that make sense? Using the prefix ++ operator would get you the same results as the (pts += 1) because the prefix operator calculates before accessing the variable in the statement.

Here's a link to a more thorough explanation: Java Quick Reference
 
Back
Top