Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using a while block to do nothing a bad thing?

Tags:

c

while-loop

People also ask

Are empty while loops bad?

Empty while loopsAn empty while loop is a bad idea. This isn't the best approach but it is an acceptable one (in the right scenario). This ensures that you only perform the check at a reasonable interval. Note that the precise length of that interval is up to you to decide.

Can you leave a while loop empty?

Yes an empty while loop would generally be seen as a bad thing, though there are a few useful cases too. No, you should not do something like that. It uses 100% of the CPU and that is not useful.

Is it good practice to use while true?

"while True" in itself is not bad practice/style, but using a "while True" loop in conjunction with a "break" could be considered bad practice because it can almost always be rewritten as a "while something" loop, which improves readability and maintainability.

Do While vs while true?

There are two variations of the while loop – while and do-While. The difference between the two is that do-while runs at least once. A while loop might not even execute once if the condition is not met. However, do-while will run once, then check the condition for subsequent loops.


Not at all - I believe you'll find do-nothing loops like these in K&R, so that's about as official as it gets.

It's a matter of personal preference, but I prefer my do-nothing loops like this:

while(something());

Others prefer the semicolon to go on a separate line, to reinforce the fact that it's a loop:

while(something())
  ;

Still others prefer to use the brackets with nothing inside, as you have done:

while(something())
{
}

It's all valid - you'll just have to pick the style you like and stick with it.


I think it is perfectly acceptable.

I would either write it:

//skip all spaces
while ((c = getchar()) == ' ') {} 

to make it obvious that this one line of code does one thing.

Or I would write it like this:

while ((c = getchar()) == ' ') {
    //no processing required for spaces
}

so that it matches the rest of your code's format.

Personally, I am not a fan of the

while ((c = getchar()) == ' ');

format. I think it is to easy to overlook the semi-colon.


Your question "Is using a while block to do nothing a bad thing?" may also be answered in terms of wasting CPU cycles. In this case the answer is "No", since, the process will sleep while it waits for the user to input a character.

The process will wake only after a character is input. Then the test will occur and if the test passes, i.e. c == ' ', the process will go to sleep again until a the next character is entered. This repeats until a non-space character is entered.


Well if you really don't like the empty braces, you could refactor that inner loop into

while (c == ' ') {c = getchar();}

This costs one extra comparison though, so a do while loop would be better.


A while that does nothing probably is a bad thing:

while(!ready) {
   /* Wait for some other thread to set ready */
}

... is a really, really, expensive way to wait -- it'll use as much CPU as the OS will give it, for as long as ready is false, stealing CPU time with which the other thread could be doing useful work.

However your loop is not doing nothing:

while ((c = getchar()) == ' ')
    {};  // skip

... because it is calling getchar() on every iteration. Hence, as everyone else has agreed, what you've done is fine.


I don't think the procedure is, but your formatting is pretty weird. There's nothing wrong with:

/* Eat spaces */
while ((c = getchar()) == ' ');

(that is, indicate there's intentionally not a body)


I would favor:

while ((c = getchar()) == ' ') /* Eat spaces */;

I've also been known to have a procedure named DoNothing specifically for calling in cases like this. It makes it very clear that you really mean to do nothing.

While non-existent loop bodies are perfectly acceptable it should be VERY clear that it's intentional.