Re: for(;;) or while(1)?
From: Chris Dollin (kers_at_hpl.hp.com)
Date: 10/02/03
- Next message: Ed Davis: "programming style: which is clearer? (iteration vs. recursion)"
- Previous message: Chris Dollin: "Re: Differences between 'const char' and 'char'"
- In reply to: Noah Roberts: "Re: for(;;) or while(1)?"
- Next in thread: Micah Cowan: "Re: for(;;) or while(1)?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Ed Davis: "programming style: which is clearer? (iteration vs. recursion)"
- Previous message: Chris Dollin: "Re: Differences between 'const char' and 'char'"
- In reply to: Noah Roberts: "Re: for(;;) or while(1)?"
- Next in thread: Micah Cowan: "Re: for(;;) or while(1)?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|