| View previous topic :: View next topic |
| Author |
Message |
wunder312355 Grandmaster Cheater
Reputation: -1
Joined: 14 May 2007 Posts: 568
|
Posted: Mon May 25, 2009 6:45 pm Post subject: [C++][Question]Increments |
|
|
I am having trouble understanding what is the difference between
| Code: | | cout << ++i << endl; |
and
| Code: | | cout << i++ << endl; |
Can anybody properly explain this?
|
|
| Back to top |
|
 |
dnsi0 I post too much
Reputation: 0
Joined: 04 Jan 2007 Posts: 2674
|
Posted: Mon May 25, 2009 6:50 pm Post subject: Re: [C++][Question]Increments |
|
|
| thefun25 wrote: | I am having trouble understanding what is the difference between
| Code: | | cout << ++i << endl; |
and
| Code: | | cout << i++ << endl; |
Can anybody properly explain this? |
It simply means whether the variable will be increased by 1 BEFORE OR AFTER the instruction is executed.
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon May 25, 2009 8:09 pm Post subject: |
|
|
| pre vs post increment, it works exactly like expected.
|
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Mon May 25, 2009 10:05 pm Post subject: |
|
|
| http://www.cplusplus.com/doc/tutorial/operators/ wrote: | Increase and decrease (++, --)
Shortening even more some expressions, the increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Thus:
c++;
c+=1;
c=c+1;
are all equivalent in its functionality: the three of them increase by one the value of c.
In the early C compilers, the three previous expressions probably produced different executable code depending on which one was used. Nowadays, this type of code optimization is generally done automatically by the compiler, thus the three expressions should produce exactly the same executable code.
A characteristic of this operator is that it can be used both as a prefix and as a suffix. That means that it can be written either before the variable identifier (++a) or after it (a++). Although in simple expressions like a++ or ++a both have exactly the same meaning, in other expressions in which the result of the increase or decrease operation is evaluated as a value in an outer expression they may have an important difference in their meaning: In the case that the increase operator is used as a prefix (++a) the value is increased before the result of the expression is evaluated and therefore the increased value is considered in the outer expression; in case that it is used as a suffix (a++) the value stored in a is increased after being evaluated and therefore the value stored before the increase operation is evaluated in the outer expression. Notice the difference:
Example 1
B=3;
A=++B;
// A contains 4, B contains 4
Example 2
B=3;
A=B++;
// A contains 3, B contains 4
In Example 1, B is increased before its value is copied to A. While in Example 2, the value of B is copied to A and then B is increased. |
_________________
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue May 26, 2009 11:40 am Post subject: |
|
|
| Remember that the pre increment might be faster than the post increment :)
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Tue May 26, 2009 1:42 pm Post subject: |
|
|
It just means that it will increment before or after. This is most easily demonstrated in for loops.
| Code: |
for(int i = 0; i<10; i++) {
cout<<i;
}
//...
for(int i = 0; i<10; ++i) {
cout<<i;
}
|
In the first loop, it will display i's value, then increase it's value, while in the second one it will increase i's value and then display it.
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Tue May 26, 2009 3:46 pm Post subject: |
|
|
| Jani wrote: | Remember that the pre increment might be faster than the post increment  |
Maybe if your compiler is hilariously bad at it's job...
|
|
| Back to top |
|
 |
talkerzero Grandmaster Cheater
Reputation: 1
Joined: 24 Jul 2008 Posts: 560 Location: California
|
Posted: Sat Jun 06, 2009 11:17 am Post subject: |
|
|
Quick example..
| Code: | j = 0;
string[j++] = 'x'; |
| Code: | j = 0;
string[++j] = 'x'; |
The above code will assign 'x' to string[j], when j = 0, then increment j. The below code will increment j to 1, then assign 'x' to string[j] when j now = 1.
|
|
| Back to top |
|
 |
|