Re: for(;;) or while(1)?

From: Chris Dollin (kers_at_hpl.hp.com)
Date: 10/02/03


Date: Thu, 02 Oct 2003 16:12:23 +0100

Noah Roberts wrote:

> Chris Dollin wrote:

>> I'm sure that, being a well-structuring kind of chap, you just forgot
>> that `continue` is a C keyword ...
>
> If you think that is funny:

Amusing. Not funny. An aside.

> What an odd thing to focus on, yet several responders did.

Of course. Nitpickery isn't offswitchable.

>>>while (!quit)
>>> get input
>>> if input is quit then quit = true
>>> else do something
>>>...
>>>
>>>The second version is "structured" code whereas the first is not (two
>>>exits).
>>
>> The second one is horrid. The artifical boolean variable obscures what's
>> going on.
>
> How so? It says "loop until I am told to quit" instead of "loop
> forever". In my opinion "while (true)" is the lie because true never
> stops being true yet you exit at some point.

`while(true)` is the signal that says "less common loop structure
here". Using a quit variable says "there's some boolean you have to
track through this code to look for assignments and check their
value". With `while (true)` you know it's breaks you have to look
for, which you knew anyway.

`lie` doesn't come into it.

> Also you are assuming that quit is set to false within the loop, as it
> is in the /pseudo/ code I wrote. This is only the case for very simple
> programs or problems. More often your stop switch will be turned on
> somewhere in a function you call in your loop, maybe several levels in.

That has happened to me maybe twice in twenty years.
    
> For simple problems the break or return method is acceptable but doesn't
> hold up when things get much more complex.

Maybe I don't write complex enough code.

> I try to stay away from those though as it can really bite you in the
> ass, and I only ever use forever loops in tests, never in production code.

Tests *are* production code :-)

-- 
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html


Relevant Pages

  • Re: Alfa Romeo and fans of other makes. Lynk SFW
    ... can't decide if it is funny, retro, clever or if it is simply ... I'm going with the horrid idea. ... Kill it WITH ...
    (uk.rec.sheds)
  • Re: map/filter/reduce/lambda opinions and background unscientific mini-survey
    ... > *No more NamesError exceptions! ... So you could do lot's of funny things like: ... > *No initialization needed for a while loop! ... many places the variable could or could not be set, your program design ...
    (comp.lang.python)
  • Re: [RFC/PATCH] Fixes for ULi5261 (tulip driver)
    ... The funny thing is that it seems the _first_ phy_read call always ... returns only when the 0x8000 bit is gone (I got this while loop from the ... More majordomo info at http://vger.kernel.org/majordomo-info.html ... Please read the FAQ at http://www.tux.org/lkml/ ...
    (Linux-Kernel)
  • Re: Who Liked George Carlin?
    ... funny when he forgot to update his act. ... 24 hour loop. ... Making fun of the Catholic ...
    (rec.sport.football.college)
  • Re: best way to enumerate List<> & remove unwanted elements?
    ... "No, you don't decrement like that, you're going to eventually blow up ... It's funny how the current argument has absolutely nothing ... doing a poor job of explaining what exactly is wrong with my ... the loop iterator within the loop. ...
    (microsoft.public.dotnet.languages.csharp)