ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Common Board

i++ and ++i
Posted by Neznakomec 27 Jun 2013 02:58
Sorry, can anyone explain difference
between using i++ or ++i
in loop
for(int i=1;i<=n;++i)
or
for(int i=1;i<=n;i++)

i tried print "i" in loop, all the same
Re: i++ and ++i
Posted by Erop [USU] 28 Jun 2013 00:04
int i = 5;
int a = i++;
int b = i;
i = 5;
int c = ++i;
int d = i;
cout << a << " " << b << endl;
cout << c << " " << d << endl;
Re: i++ and ++i
Posted by Bogatyr 28 Jun 2013 03:52
The difference comes in when you use it like this:

while (  i++ < n )
vs.
while ( ++i < n )

also:
 value = array[ i++ ]
vs.
value = array[ ++i ].

There is a difference in the for loop version though, more subtle, when you're using iterators/objects instead of plain ints, in short, i++ is more efficient.   Except for this efficiency corner case, when "++i" or "i++" is used all by itself, you're correct, there's no apparent difference.

Edited by author 28.06.2013 03:54