This may be a stupid question, but how does the efficiency of a while loop compare to that of a for loop? I\'ve always been taught that if you can use a for loop,
That depends on the exact compiler that you use. In your example, a good compiler will create the same machine code for both options.
It will depend slightly on the language, possibly, and maybe on the compiler, but most modern compilers will treat them as exactly the same and there will be no difference.
The performance difference between for loop
and while loop
is not a big issue because modern compilers can generate same machine code for both loops and secondly both loops require same operations:
In general you should use for loop
in your code for the following reasons:
while loop
, hence it helps in better memory management.I hope that makes sense and would help.
Well, if the loop is large and complexe it won't matter since the overhead of the loop code (for or while) will be very low...
Anyway, if you really want to know, I guess it's up to you to check in your ide, in assembly code. or you can use a dissembler to look yourself in an executable. http://www.caesum.com/files/borg228.zip (warning: there is usually lots of bloat in .exe,so good luck!)
As mentioned in all the answers here, any decent compiler would compile both loops into the same machine code.
Your machine code (taking MIPS as an ex) would be a bunch of normal assembly statements followed by a branch (onequal/notequal) in both cases making your efficiency consistent.
However, you could debate over the coding style issue over here (Not efficiency).
For Loops :
*Probable Usage : When a collection of items already exists and you want to go over it and retrieve the number of times a certain property appears.
While Loops :
You're not aware of how many times the loop is going to run. There is an exit case which is set/reached sometime while the loop is running (if you want to simulate a for loop, you'll use something like a counter (extra code))
Do not know by how much your loop is going to increment by. Your increment/next move can be set dynamically. While you can do this in a for loop, you'll have to account for the increment at each iteration as well causing some unreadable code which can be avoided when if you use a while loop.
*Probable Usage : Grepping a stream for some data. You're not aware of how long the stream is so your exit case is when the stream ends. Since it's a stream and you might be getting data line by line, you might want to skip over white lines altogether making your increments not consistent.
I think you're drawing the wrong conclusion from the advice you've been given.
The reason (in this instance at least) to prefer the for
construct over the while
has nothing to do with efficiency; it's all about writing code that expresses your intentions in a clear and easy to understand manner.
The for
places the initial condition, increment, and exit condition all in one place, making it easier to understand. The while
loop spreads them around. For example, in your sample, what is the initial value of i? -oh, you forgot to specify it? --that's the point.