Sign in to follow this  

Why is a prefix operator better in C++ loops

Recommended Posts

SekaiStory    42

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?

Share this post


Link to post
Share on other sites
Lord Method Man    1,613

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.

Share this post


Link to post
Share on other sites
Lord Method Man    1,613

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.