|
|
back to boardCommon Boardi++ and ++i 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 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 |
|
|