When should we use for loop and when while in C ? In terms of speed up and memory consideration in embedded systems.

They are identicdal in terms of speed and memory consumption. You can always substitute a while loop for a for loop, but not the other way around. The for loop is preferable when you know exactly how many loop iterations there will be. The while loop may be necessary when the exact number of iterations is not known. Otherwise except for the syntax the two loops are the same.

I agree with "Ancient Dragon": you generally use for loops when you know ahead of time exactly how many loops will be executed.

However, I would like to add another couple aspects you might want to consider.

1) If the body of the loop is small, and you want to be really anal about speeding up code execution, loop unrolling might be worthwhile. By reducing the number of condition checks, that code block executes faster.

2) I have read a few times that turning a for loop into a reverse while loop makes the program execute faster (but I haven't done any software timing experiments to test it myself).

Say you have a loop you know will execute 10,000 times.
Normally, you'd code it as a for loop:

for (i = 0; i < 10000; i++){
  do something;
} // End for loop

However, you could also code it as a reverse while loop:

i = 10000;
while (i){
  do something;
} // End while loop

Keep in mind that if you are using the loop variable, i, to also serve as a index to a variable you are working with, like a matrix, it might not work in the while loop. So you'd have to use another variable.

Anyhow, it is something else to think about.

I would add that loop unrolling is a technique that is now best left to your compiler. As are optimizations such as moving the test to the beginning or end of the loop -- today's compilers will almost always produce better, faster, code than you will if you use the language constructs in a natural manner and avoid trying to tweak them for better speed -- in fact, many of those speed improvements get in the way of final optimization.

Optimization tends to be not quite as good with some older (>10 years old) compilers, as well as some vendor-specific ones. So this advice may not apply if you're working on an embedded system, for example.

There are three types of the loops:
(1) for loop
(2) while loop
(3) do..while loop

Main difference between for loop and while loop is that in for loop there is the fixed number of counts and in while loop there is the infinite number of counts.

I have to disagree. There's no reason a for loop should be preferred when you know the exact number of iterations -- the only difference between the three loops is when you do the test to see if it's time to stop looping: before any iterations have been run, at the start of the loop, or at the end.

In the case of a known number of iterations, it's certainly convenient to put the count at the start as well as having all of the initialization in a single statement, but there's no reason you have to do it that way other than convenience -- and for loops are preferred for many cases where the exact number of iterations is NOT known (such as list or tree traversal).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.