SekaiStory Posted November 27, 2012 Share Posted November 27, 2012 I come from a C# background and learning C++ in school. Our programming teacher has us to do iterations with a prefix like this: for(int i = 0; i < 10; ++i) {} Teacher says this is way better than using a postfix (with i++) and only makes a difference in C++ loops. He never explained why and I'd love to know. Anyone can explain me why ++i is better than i++ in C++ loops? Link to comment Share on other sites More sharing options...
0 Lord Method Man Posted November 27, 2012 Share Posted November 27, 2012 Odd, I've never been told this before. I don't see what the difference would be other than you are now iterating from 1-10 instead of 0-9 which just requires you to implement an additional step when looping through an array. Link to comment Share on other sites More sharing options...
0 ZakO Posted November 27, 2012 Share Posted November 27, 2012 The post-increment operator (i++) may use a temporary variable, the pre-increment operator (++i) won't. However, in a lot of cases a good compiler will optimize the temporary variable away. Discussion: http://stackoverflow...en-i-and-i-in-c - http://stackoverflow...en-i-and-i-in-c Phouchg and +Majesticmerc 2 Share Link to comment Share on other sites More sharing options...
0 Lord Method Man Posted November 27, 2012 Share Posted November 27, 2012 Interesting, I've never heard that before though it makes sense that a temp variable would be used. I'm not so hardcore about optimization that I'd worry about it though. Link to comment Share on other sites More sharing options...
Question
SekaiStory
I come from a C# background and learning C++ in school. Our programming teacher has us to do iterations with a prefix like this:
for(int i = 0; i < 10; ++i) {}
Teacher says this is way better than using a postfix (with i++) and only makes a difference in C++ loops. He never explained why and I'd love to know.
Anyone can explain me why ++i is better than i++ in C++ loops?
Link to comment
Share on other sites
3 answers to this question
Recommended Posts