• 0

Why is a prefix operator better in C++ loops


Question

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

  • 0

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

  • 0

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

  • Like 2
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.