0

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?

Community
  • 1
  • 1
xrainxfallx
  • 67
  • 1
  • 9
  • 2
    This question gets [asked a lot](http://stackoverflow.com/questions/3875114/why-use-a-for-loop-instead-of-a-while-loop). – chrisaycock Aug 07 '12 at 02:38
  • 1
    If you were writing a compiler and one of these took longer, wouldn't you fix your compiler so that it didn't? – Jim Balter Aug 07 '12 at 03:29

6 Answers6

6

Generally speaking, for loops are preferred when the number of iterations is known (i.e. for each element in an array), and while loops are better for more general conditions when you don't know how many times you'll run the loop. However, a for loop can do anything a while loop can, and vice versa; it all depends on which one makes your code more readable

In this case, a while loop would be preferable, since you're waiting for signal == 0 to become false, and you don't know when that will occur.

TSL
  • 916
  • 6
  • 18
3

Any for loop can be written with a while loop and vice versa. Which you do is mixture of preference, convention, and readability.

Normally, for loops are used for counting and while loops are sort of waiting for a certain condition to be met (like the end of a file). There is no performance difference.

Boilerplate for and while loops:

for(int i = 0; i < someArraysLength; i++)
{
    // Modify contents of array
}

while(lineLeftInFile)
{
   // Read and parse the line
}
Chris Dargis
  • 5,891
  • 4
  • 39
  • 63
2

Whichever is easiest to read and understand.
Keep in mind that someone (other than you) might at some point try to read your code.

My opinion: while

mariusnn
  • 1,847
  • 18
  • 30
1

Execution time is irrelevant. Any compiler that's worth a damn will generate the exact same code.

Now, as for semantics and such...computationally,

for (init; test; increment) { /* do stuff */ }

is exactly equivalent to

init;
while (test) {
    /* do stuff */
    increment;
}

And without init and increment, becomes just

while (test) {
    /* do stuff */
}

So computationally, the two are identical. Semantically, though, a for loop is for when you have a setup and/or increment stage (particularly if they make for a predictable number of iterations). Since you don't, stick with while.

cHao
  • 84,970
  • 20
  • 145
  • 172
0

I agree with mariusnn, whichever is easiest to read and the while would seem to be easier to read to me as well.

Looking at the assembler produced by Visual Studio 2005 when doing a Debug build, the assembler instructions look to be the same for both of these loops. And actually if you do the same loop using an if statement and a label with the true statement being to increment i and then goto the if statement again, it looks like that also generates the same assembler.

    for (; signal == 0; ) {
0041139C  cmp         dword ptr [signal],0 
004113A0  jne         wmain+3Dh (4113ADh) 
        i++;
004113A2  mov         eax,dword ptr [i] 
004113A5  add         eax,1 
004113A8  mov         dword ptr [i],eax 
    }
004113AB  jmp         wmain+2Ch (41139Ch) 

    while (signal == 0) {
004113AD  cmp         dword ptr [signal],0 
004113B1  jne         loop (4113BEh) 
        i++;
004113B3  mov         eax,dword ptr [i] 
004113B6  add         eax,1 
004113B9  mov         dword ptr [i],eax 
    }
004113BC  jmp         wmain+3Dh (4113ADh) 

loop: if (signal == 0) {
004113BE  cmp         dword ptr [signal],0 
004113C2  jne         loop+11h (4113CFh) 
        i++;
004113C4  mov         eax,dword ptr [i] 
004113C7  add         eax,1 
004113CA  mov         dword ptr [i],eax 
        goto loop;
004113CD  jmp         loop (4113BEh) 
      }
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
0

So which loop would you use and Why?

If I had to choose between the two, I would probably use the while loop, its simpler and cleaner, and it clearly conveys to other developers that the following block of code will be continuously executed until signal is updated.

then again one could do this: for(; signal == 0; i++); it seems to be the more concise, though thats assuming if indeed this will be production code.

Still all this methods seem well a bit dangerous because of overflow, even on embedded devices most clocks are still quite fast and will probably reach the upper bounds of the underlying data type quite soon, then again I don't know if this will be production code nor do I know if that is an acceptable outcome.

What is the difference between both of them?

Like you said both achieve the same goal though Im sure there other ways as I've shown or as others mentioned, the biggest difference between for and while is that one is usually use when we know the number iterations while the other we don't, though for better or worse I've seen some very unique uses of a for its quite flexible.

Does it have any difference in terms of time taken to execute? Or it is purely based on your preference?

As for performance, fundamentally its up to your compiler to decide how to interpret it, it may or may not produce the same binaries, and hence execution time, you could ask it to produce the assemblies or do some profiling.

It seems that uvision 4 IDE http://www.keil.com/product/brochures/uv4.pdf indeed does support disassembly, as noted at page 94, and profiling as noted at page 103, if that indeed is the version you are using.

Though if the difference is small enough please don't sacrifice readability just to squeeze a couple extra nano-secs, this is just my opinion, Im sure there others that would disagree.

The best advice I could give you is this, try as best as you can, to write clear code, meaning most can see what it conveys without much effort, thats efficient and maintainable.

Samy Vilar
  • 10,800
  • 2
  • 39
  • 34