what's the difference between this two code?
usr.VARIABLE++
usr.VARIABLE1 += 1
ID:173948
![]() Oct 22 2003, 12:52 pm
|
|
Just to add to Theodis's explanation, in DM you can use var++ as an expression whereas var+=1 is a statement. In C there's no distinction; everything is an expression. In DM, however, you can't do this:
DoSomething(i += 1) DoSomething(++i) Lummox JR |
These both have the same effect.
The wierdness of ++ comes in when you use it in a statement where precidence comes into play.
var/i = 1
world << "[++i]"
world << "[i]"
This should print
2
2
var/i = 1
world << "[i++]" // Note the ++ is after the i now
world << "[i]"
This should print
1
2
When you put ++ before a variable it has a high precidence so the variable is increment before many of the other things on the same line happin. If you put the ++ after the variable the variable is incremented much later so if you do something like
MyProc(i++)
MyProc is called with the initial value of i then after it is done it's incremented.
This causes many problems for C++ programmers and I'm sure it has caused it's fair share of confusion here too. Since you usually want the variable to be incremented earlier on it's best to get in the habbit of doing ++i instead of i++ since the latter can lead to some nasty problems if you don't catch it.