which is the fastest loop?

loops - Which is fastest
@gagana (757)
India
January 15, 2007 8:55am CST
for,while,do while
1 person likes this
11 responses
@dholey (1383)
• India
15 Jan 07
my first ever earning from mlot - mylot earning
i know the truth, all the loop executes in similar way (their execution time is same), we have to make initialization of variable, we have to check a condition and we have to increase or decrease value, the only difference is these points are handled in different lines in different loops, noting else, if you want to make fast loop you have to make a register variables, it takes less time as it is in the memory of cpu. but, while do wile and for loop has same execution time, the advantage i for loop is only convenience of putting all three main points of loop in one line nothing else.
2 people like this
@stonehr (818)
• Croatia (Hrvatska)
15 Jan 07
Yes You are right dholey, as I said the best is to try to avoid loops if it possible, and of course 'for' loop will have the smallest consumption of memory. You are rated. :)
1 person likes this
@dholey (1383)
• India
16 Jan 07
thanks dude
1 person likes this
@paki143 (793)
• Pakistan
16 Jan 07
it is for loop,i am not sure but i think its for loop
@dholey (1383)
• India
20 Jan 07
THERE IS NO DIFFERENCE ALL THREE POINTS (INITIALIZATION, CONDITION AND INCREMENTATION) are handled in different time so even in for loop it is same the control initializes the variable then checks the condition then runs the body then increments or decrements the value that is same as while , the only advantage is.... in for we can see all thee points in one single line where in other loops they are in different line ... so there is no difference in speed of different loops
@pvinay (210)
• India
20 Jan 07
while statment is the fastest because let for (i=0;i=10;i++){ statments....} there r three two statments which r excuted at the start first is i=0; initialization and second is i=10;comparision now if comparision statment is true then inside statments are excuted . so at least two statment will excuted in for loop .now in do while loop do{ statments....}while(i=10) so all body statment r excuted atleast one time weather comparision statment is true or false. so more stament r excuted as compare to for loop . now in while loop while(i=10){ statments....} in while loop if coparison statment is false (i=10)then body statment is not excuted so atleast one statment is excuted in this case . so u see that if first time comprasion statment is false for loop excuted ......2 statments (initialization and comparision) do while excuted......whole body statment while loop excuted....1 statment(comparision) so while statment is the fastest one
@zal3x89 (280)
• Romania
15 Jan 07
I think it is for.But im not sure. You should do some benchmarks to be sure. Peace..
@gagana (757)
• India
15 Jan 07
ok may be it is thanx for the response
• India
17 Jan 07
i think all the loops are same in fastness. but a compiler can does a little bit optimization in case of For loop because it know some condition in advance whereas in while to do-while loop comppiler is not aware of the increment values.
• Mauritius
20 Jan 07
It is the For loop because it knows how many times it should carry on.
• India
16 Jan 07
its depands on ur code under loop.
• Malaysia
15 Jan 07
Oh this is the programming or computer language issue..I hate my programming subject so much. I took C++ Programming and I got D- for that subject! I hate it so much!
@coolvin (21)
• India
15 Jan 07
I dont understand what u mean by fastest loop. Does mean the block of code in the loop ,or the efficiency of the for ,while statements. anyway, Do-while if the loop executes only once , because a comparison comes only at the end of the loop once in do-while when translated to machine code. for means (usually) initialization also included with the loop.so slower. But,speed of execution depends on a specific implementation.it varies with number of loops or how/when you initialize and increment counters ...
@raveemenon (1071)
• India
15 Jan 07
without doubt it is 'For'. then it depends upoon the circumstance i hope.let linguists comment!
@stonehr (818)
• Croatia (Hrvatska)
15 Jan 07
Very interesting question you have. I can't say that for loop will be faster than while.Anyhow You can't always use for loop. In many cases you must use while loop.If You use while than You'll have following: x++; while x=100{... exit;}, so same elements You have in for loop also. for (x=0; x=100; x++; ){....} You always have a counter and some condition that is checked everytime after is variable changed. So it would be great to avoid use of loop whenever you can.