Question:
is a for loop more efficient that a while loop?
kinkb04
2010-03-12 09:57:18 UTC
I was told today that a for loop is more efficient that a while loop. Is this true? I have never herd this before. We are talking about the C# language also, so I dont know if this would be different across languages like java, C++, actionscript, etc...
Seven answers:
?
2010-03-19 17:38:32 UTC
Both the loops are very efficient, it all depends on your application and the usage, sometimes a do while loop is necessary where u need to run the loop at least once before quiting. For cannot do that



But other times its very easy to write multiple nester for loops than while loops, so both have same effectiveness its the application and implementation which makes them more effective is some places than another
Frecklefoot
2010-03-12 18:08:54 UTC
It depends on how each are setup, but in practice, it doesn't really matter. Just use whichever is best for your situation. I use both all the time. With today's multicore, hyper-threaded CPUs and super-optimized compilers, trying to second-guess the technology or the compiler is just fruitless. Just use whichever is right for the situation.



That being said, there are just some things that you can do out of habit to make things go smoother. Take, for example, this:



while( foo == doAnIncrediblyLongCalculationHere() )

{

}



Can easily be optimized to this:



int result = doAnIncrediblyLongCalculationHere();

while( foo == result )

{

}



Will any reasonable compiler do the optimization anyway? Yes. Does it pay to take the chance? No. Plus anyone else inspecting the code wouldn't be able to see the otpmization you hope is taking place for you.



Don't stress over which is more efficient. Used correctly, they both rock. The do...while loop is rarely employed, but it's useful in some situations too. HTH
djp32563
2010-03-12 18:06:46 UTC
A For loop involves not only comparison, but also changes to the index variable as well. The While loop only involves a comparison. So if you are looping through an array, then the For loop will probably be more efficient. But if you are searching a linked list, a while loop will probably be a better choice. They both have advantages and disadvantages. You just have to use the right tool for the conditions at hand.
?
2010-03-12 18:07:09 UTC
You have to look at what are called "registers". These registers are used to store things that are used often (registers have a very quick recall time, so it makes sense to put the most commonly used things in registers for quick access in programs). Every time a computer has to check a value or make a comparison, it takes a bunch of extra steps (which you don't actually see, even as the programmer). Efficiency and speed aren't the same, so while you can say that for loops are more efficient (they do more work per unit time) they aren't necessarily faster in terms of computing speed. But, yes, for loops are more efficient because they bundle together an initialization, comparison, and increment/effect all in one.
Yasser Almohammad
2010-03-12 18:01:52 UTC
in general for loop is more efficient, simply because of the compiler optimization of the counter and conditions which could be assigned to a CPU register for faster performance.



in reality, this doesnt matter, you can pick up for or while depening on what suites you, these minor performance differences are highly dependent on the compiler performance, and practically none existent.



most performance issues comes from heavy data fetching-calculations not from the simple counter incrementation or the difference between using while or for for a certain problem, so dont worry about minor issues like this.
murphy
2010-03-12 18:18:56 UTC
If you want to test the loop's condition prior to executing the loop's body, then a while loop is more efficient.



If you want to test the loop's condition after executing the loop's body, then a for loop is more efficient.
voldemort
2010-03-12 17:59:29 UTC
I think, may be because u don't have to check the condition to exit the loop in "for loop".


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...