Possible Duplicate:
Why use a for loop instead of a while loop?
I am currently using embedded c. Software that i am using is keil uvision.
So i have a question regarding on which loop will u use?
Both loop does the exact same thing. As long as signal = 0, ‘i’ will increase by 1.
Firstly,
for(;signal==0;)
{
i++;
}
The next program:
while(signal==0)
{
i++;
}
So which loop would you use and Why? What is the difference between both of them?
Does it have any difference in terms of time taken to execute? Or it is purely based on your preference?
Generally speaking,
forloops are preferred when the number of iterations is known (i.e. for each element in an array), andwhileloops are better for more general conditions when you don’t know how many times you’ll run the loop. However, aforloop can do anything awhileloop can, and vice versa; it all depends on which one makes your code more readableIn this case, a
whileloop would be preferable, since you’re waiting forsignal == 0to become false, and you don’t know when that will occur.