+ 13
Is there a speed difference between while loop and for loop?
I'm just curious about the speed of while and for. There are some loops which can have the same functionality. Either you code with "while" or "for". When they have the same functionality, which one is faster or can they be compared?
14 odpowiedzi
+ 21
Yunus Temurlenk Assuming the while loop and for loop being compared in C/C++ are equivalents with incrementing or decrementing values and an equivalent break condition, there won't be any difference.
Both loops will compile to the same assembly and/or machine code instructions as well as have the same compiler optimizations applied.
+ 10
It may be little speed difference .
+ 7
For the case when the loop condition is not satisfied at the beginning, a do-while will always be slower than a for or a while due to the extra unnecessary iteration before checking the condition, but if you are doing this most likely the code is badly designed.
+ 6
~ swim ~ Yeesh.. I hope that's not what Pathmaja S was referring to. 😂
But now I need your help to understand how counter is initialized on each loop iteration.
Isn't the counter in the expression...
(counter = counter < 5 ? counter : 5)
... already initialized in the line above the loop?
So... in that expression, counter is being assigned either, its current value or 5.
Or maybe you were making a joke and it completely went over my head. 😅
+ 5
Actually, ıf there is only a little difference, it is important in big loops. So i just need to be sure
+ 5
Pathmaja S Regarding your statement:
"In while loop if initialization is done during condition checking..."
Would you mind clarifying a scenario where a variable is initialized in the condition expression?
Ultimately, a variable would have to be declared and initialized before it can be evaluated as a break condition.
For example:
----
int counter = 5;
while (--counter > 0) {
//Keep doing something while
// counter is greater than 0...
}
----
How would you initialize int counter in the while loop condition expression above?
+ 4
David Carroll Right
+ 3
To be honest there is not much difference in speed. Both are almost equal.
+ 2
"Fast" is relative, what you claim a faster loop can also be slower if you put a heavy work on it. And a "slower" loop may run faster if you put less workload on it, or your algorithm be more optimized.
+ 2
What i feel is that 'while' loop should be regarded as both finite and infinite loop because if u look this program
a=1
while a>0:
Print(a)
This outputs an infinite no. of time.
But 'for' loop is a finite loop in this we give range i.e. a group of elements or finite no.
+ 1
~ swim ~ Ah... yes... I see what you were demonstrating now. Indeed... assigning the meaningful value could be done in the condition expression. 😉
The clarification I was looking for is if Pathmaja S statement was suggesting the counter variable was somehow being declared, initialized, then evaluated in condition expression on each iteration cycle. While this wouldn't be possible, it's how I interpreted the explanation.
----
Harsh Sharma I see what you mean. But, technically, `for` loops and `while` loops could be written as finite, logically terminating expressions or as infinite, non-terminating expressions.
https://code.sololearn.com/cup99FAVItmP/
The real differences are that `for` loops provide explicit constructs for setting the initializer, break condition, and incrementer and `while` loops only provide the break condition. 😉
0
In while loop if initialization is done during condition checking, then initialization is done each time the loop iterate. In 'for' loop iteration statement is written at top, hence, executes only after all statements in loop are executed. In 'while' loop, the iteration statement can be written anywhere in the loop.
0
In general there is no difference in run time of two different loops.