Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++][Question]Increments

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
wunder312355
Grandmaster Cheater
Reputation: -1

Joined: 14 May 2007
Posts: 568

PostPosted: Mon May 25, 2009 6:45 pm    Post subject: [C++][Question]Increments Reply with quote

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
View user's profile Send private message
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Mon May 25, 2009 6:50 pm    Post subject: Re: [C++][Question]Increments Reply with quote

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
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Mon May 25, 2009 8:09 pm    Post subject: Reply with quote

pre vs post increment, it works exactly like expected.
Back to top
View user's profile Send private message
manc
Grandmaster Cheater
Reputation: 1

Joined: 16 Jun 2006
Posts: 551

PostPosted: Mon May 25, 2009 10:05 pm    Post subject: Reply with quote

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
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Tue May 26, 2009 11:40 am    Post subject: Reply with quote

Remember that the pre increment might be faster than the post increment :)
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Tue May 26, 2009 1:42 pm    Post subject: Reply with quote

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
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Tue May 26, 2009 3:46 pm    Post subject: Reply with quote

Jani wrote:
Remember that the pre increment might be faster than the post increment Smile


Maybe if your compiler is hilariously bad at it's job...
Back to top
View user's profile Send private message
talkerzero
Grandmaster Cheater
Reputation: 1

Joined: 24 Jul 2008
Posts: 560
Location: California

PostPosted: Sat Jun 06, 2009 11:17 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites