C++/c++

Advertisement


Question
Sir, i want to know which is faster
for(int i=5;i>0;i--)
{
.
.
}

or
for(int i=0;i<5;i++)
{
.
.
}

Answer
Hello Avinash, thank you for the question.

Both of those for loops are equal. In fact, depending on how the compiler optimizes the looping, the one counting down could be reversed in machine code.

That aside, both for loops have a complexity of O(N), and in this case N is 5. So from the algorithm analysis perspective, they are equal.

I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.