Go Down

Topic: Coding conventions (Read 416 times) previous topic - next topic

bobthebanana

Which of these two loops is "better"?

Code: [Select]

  while (!var)
  {
    //code
    if (something)
    {
      var = 1;
    }
  }


or

Code: [Select]
 
  while (1)
  {
    //code
    if (something)
    {
      break;
    }
  }


They both do the same thing obviously, and I can use either one for part of my code so I was just wondering if one is prefered over the other. I would guess the first one is better since break seems to have the same reputation as goto. I've had a lot a similar situations while coding in the past; Is there a place where all these coding conventions are written out?

UKHeliBob

I personally prefer the first style and would never use the second form.

The condition(s) for the loop to end are set out at the very beginning, which makes it easier to read through the loop looking for the exit condition(s) to be met rather than looking for a break.  The names of the variable(s) used in the condition(s) is also a great help in understanding the code.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

afremont

They don't do exactly the same thing necessarily.  The second form is more like a do...while() loop, whereas the first is more conventional.  The first may not execute at all depending upon "var".  The second will always execute one iteration.  They may work the same for you now, but they are not truly "the same".
Experience, it's what you get when you were expecting something else.

TanHadron

I only use the first form if var is already declared and being used for something else.  I have a hard time creating a variable whose only reason for existence is as a flag to set to break out of a loop.  UKHeliBob has a point about only having one exit from a loop, but then I have to scan the code anyway to see where the flag got set.

I use the bottom form because it is usually more efficient, and I use for (;;)  instead of while (1) because I like the way it looks.

If I have to break two levels out of nested loops, sometimes I will create a function and use return.

I usually value efficiency over readability.  It doesn't mean it's "better", it's just what I prefer.

UKHeliBob

Quote
I have a hard time creating a variable whose only reason for existence is as a flag to set to break out of a loop.

If you need to create the variable specifically as a means of exiting the while loop then you almost certainly don't need a while loop at all.  The whole point of a while is surely that the code inside it is going to change its control variable to another value, and that in the meantime the code should be executed repeatedly.

To clarify what I said in my earlier post.  I use the first form but would not create a variable specifically to use it.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

tobyb121

Why not:
Code: [Select]

while(!something){

}

PeterH

I would only use the break approach if it made the code simpler. For example, if the logical design provided several reasons for terminating the loop but some of them were exceptional, I might use breaks to handle the exceptional termination so that the normal control logic was more obvious. Or I might choose to break out of the middle of the loop if it enabled me to structure the code inside the loop to avoid duplication. These are pretty rare situations though, and I wouldn't use break to terminate the loop unless there was a compelling reason.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up