plz, guys~
who could tell the difference between something like i++ and ++i ? I'm at a loss now.
plz, guys~
who could tell the difference between something like i++ and ++i ? I'm at a loss now.
Last edited by black; 05-27-2002 at 07:17 PM.
Never end on learning~
They are just different ways to increment a variable. ++i is a pre increment, i++ is a post increment. Perhaps an example will clear it up.
Code:int main( void ) { int x = 0; // This will output 0 , then increment variable cout << "Post Increment: " << x++ << endl; // Reset the value x = 0; // This will output 1 because it increment first cout << "Pre Increment: " << ++x << endl; return ( 0 ); }
when using the ++ operator, putting it before the name of variable means that the variable is incremented before it's used, and putting it after means that the variable is incremented after it's used.
If you are simply incrementing the variable by itself like this:
then it doesn't matter if you use ++i or i++Code:i++;
here are some examples of how it's used:
watch out for this when using for loops! especially if you are using STL linked lists in C++.Code:int i = 10; int j = i++; // j now is equal to 10, and then i is incremented to 11 int k = ++i; // i is incremented to 12 first, then the value is stored in K
hope this helps!
U.
Quidquid latine dictum sit, altum sonatur.
Whatever is said in Latin sounds profound.
To addition to the stuff the other guys said:
When optimising, use ++i when you can. why? simple really, i++ is stored in an temporary variable, ++i is not
for(i = 0; i < 10; ++i)
(increase i, then use it)
is faster than
for(i = 0; i < 10; i++)
store i in temp, increase temp, use i and then set i = temp. (might be store i in temp, use temp and increase i, or somethin like that)
But.... Most modern compilers will optimize this for you anyway... but you want to look like an cool optimize-code guy right? I sure want to... Just make sure you don't do logic errors because of this...
/dave
When is i incremented? Before the next statement, or before the next occurence?
Example
Code:int i = 2; int p = i++ + i; int q = i++; q += i;
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
why don't you try it?
Regards,
Mario Figueiredo
Using Borland C++ Builder 5
Read the Tao of Programming
This advise was brought to you by the Comitee for a Service Packless World
Originally posted by Sang-drax
When is i incremented? Before the next statement, or before the next occurence?
Example
Code:int i = 2; int p = i++ + i; int q = i++; q += i;PHP Code:
int i = 2; // i = 2
int p = i++ + i; // p = 5 (2 + 3)
int q = i++; // q = 3
q += i; // q = 7
Read this thread
B.t.w. Xmevs, on my system:
Code:int p = i++ + i; // p = 4 (2 + 2)
Mmm... maybe I should read more.Originally posted by Uraldor
when using the ++ operator, putting it before the name of variable means that the variable is incremented before it's used, and putting it after means that the variable is incremented after it's used.
If you are simply incrementing the variable by itself like this:
then it doesn't matter if you use ++i or i++Code:i++;
here are some examples of how it's used:
watch out for this when using for loops! especially if you are using STL linked lists in C++.Code:int i = 10; int j = i++; // j now is equal to 10, and then i is incremented to 11 int k = ++i; // i is incremented to 12 first, then the value is stored in K
hope this helps!
U.
Never end on learning~
really? so it finishes the entire expression before it increments, then? hmm...Originally posted by Monster
Read this thread
B.t.w. Xmevs, on my system:
Code:int p = i++ + i; // p = 4 (2 + 2)
> so it finishes the entire expression before it increments
No it doesn't mean that at all. Don't try and guess from what one particular compiler appears to do.
http://www.eskimo.com/~scs/C-faq/q3.2.html
Given the sub-expression
i++
i will be incremented at some point "after" the sub-expression has been evaluated, and before the next sequence point. All sorts of things can happen in the mean time, and the compiler is free to choose any convenient moment at which to actually increment i, after it has evaluated the sub-expression i++
So when you introduce another sub-expression
i++ + i
You're really out of luck, since there's nothing say whether it's the left or right sub-expression which gets evaluated first. Even if the left is evaluated first, nothing says that the increment has to happen before the right is evaluated. Remember it only has to happen before the next sequence point.