+ 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?

4th Nov 2019, 4:36 AM
Yunus Temurlenk
Yunus Temurlenk - avatar
17 Antworten
+ 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.
4th Nov 2019, 7:47 AM
David Carroll
David Carroll - avatar
+ 10
It may be little speed difference .
4th Nov 2019, 5:32 AM
Rohitman
Rohitman - avatar
+ 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.
4th Nov 2019, 11:15 PM
Sonic
Sonic - avatar
+ 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. 😅
4th Nov 2019, 11:19 PM
David Carroll
David Carroll - avatar
+ 5
Actually, ıf there is only a little difference, it is important in big loops. So i just need to be sure
4th Nov 2019, 5:04 AM
Yunus Temurlenk
Yunus Temurlenk - avatar
+ 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?
4th Nov 2019, 4:41 PM
David Carroll
David Carroll - avatar
+ 4
David Carroll Right
4th Nov 2019, 8:01 AM
Andrew Smith
Andrew Smith - avatar
+ 3
To be honest there is not much difference in speed. Both are almost equal.
4th Nov 2019, 4:56 AM
Avinesh
Avinesh - avatar
+ 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.
4th Nov 2019, 6:48 AM
Ipang
+ 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.
5th Nov 2019, 12:26 PM
Harsh Sharma
Harsh Sharma - avatar
+ 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. 😉
5th Nov 2019, 3:38 PM
David Carroll
David Carroll - avatar
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.
4th Nov 2019, 3:54 PM
Pathmaja S
0
In general there is no difference in run time of two different loops.
6th Nov 2019, 3:51 AM
Hasibul Hasan Shanto
Hasibul Hasan Shanto - avatar