424,963 Members | 924 Online
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

# Which one is better in performance - a for loop or a while loop?

 P: 12 I wanted to know which one is better in performance between a for loop and a while loop in terms of: 1. memory requirement 2. execution speed Also wanted to know since recursion functions are not as efficient as for of while in terms of memory requirement and execution speed, what is the purpose of using them in the code? Sep 1 '10 #1
8 Replies

 P: 39 Hi with regards to while and for: - Memory requirement: The loops themselves cost no memory other than the jump statements, neglectable. - Execution speed: for simple expressions there will be no difference. But for advanced criteria to keep a loop running, a while loop could be more efficient. For example by use of switch cases or single if statements to break out of the loop. Recursions are in some cases necessary for special features, which otherwise would require extensive iterational coding. Best known example would be the solution to solve the 'towers of hanoi'. The efficiency will then be at development level. But at execution time, the efficiency loss will only be due to the call-stack. Sep 1 '10 #2

 Expert 100+ P: 2,396 I can't think of any reason for there to be a time- or space-efficiency difference between for and while loops. Sep 1 '10 #3

 Expert 100+ P: 2,396 If the loop body is executed N times, then for/while will evaluate the termination expression N+1 times but do while will only evaluate it N times. Sep 1 '10 #4

 Expert 100+ P: 983 The main difference between the for/while and the do while is when the test is executed. Let's leave out the for loop's initializer and iteration; they will have to be coded when using while loops anyway. The only difference is that do while condition does not guard the first execution pass through the body of the loop. Said loop will always execute its body at least once. In the for and while loops, the condition is executed before the first pass through the body of the loop - meaning that the body of the loop is guarded against inadvertent execution. Set that aside, do while has fallen into general disfavor for a number of reasons, as the goto has. That is not to say that it does not have its place and value, just that it's in disfavor. Likely because when people write long (e.g. 100 line) loop bodies, it's very easy to lose track of the termination condition. Still, do while has its place, and belongs in your tool-kit. Sep 1 '10 #5

 Expert Mod 5K+ P: 9,197 I don't see that do/while has fallen out of favor. It is just a loop you use when you want the loop to execute at least once. Sep 2 '10 #6

 Expert 100+ P: 983 weakness, I'm perhaps biased, but I've not seen it used in industrial code more than a dozen times in 25 years of experience. Now, I will admit that I've seen people go to ungodly lengths to simulate the do while, but actual use, almost never. Cheers! prrrrrr Sep 2 '10 #7

 Expert Mod 5K+ P: 9,197 I usuaally see this in menu processing where you want the user to provide a choice. If you use a while loop you need to get a choice outside the loop the first time and inside the loop the rest of the time. The do loop avoids the extra code. Sep 4 '10 #8

 Expert 100+ P: 983 Weakness, I do understand. Most of what I've worked with is machine control and GUI interfaces. Lots of interrupt driven code and state machines. Thank you for the insight. I do appreciate it. Cheers! Sep 7 '10 #9