+ 4
Is For loop is greater then other loops or not?
I am just confused as For loop is greater then other looks in working or just have equal preference to while and do while loops.
4 Answers
+ 3
for in most languages is syntactic sugar for an equivalent while loop, which is in turn syntactic sugar for a set of labels and gotos down in assembly or IL. Given an efficient implementation of the language spec, these will be roughly equal. Some languages include internal "comments" giving hints to decompilers/reflectors about what the original code looked like, which will have a negligible effect on performance. I think you'll find that the biggest execution time differences between these are inherent in OS scheduling.
+ 1
Most of us use for loop rather than while and do-while.
We know that the basic requirements for any looping structure are,
-Initialization
-Condition
-Incre/decrementation
In for loop the above three are placed in a single line
Whereas in while or do-while these statements are placed in different lines and at different places. Sometimes people forget to write any of the above three if the code is very long. In that case it is preferred to use for-loop
0
hahaha