Re: for(;;) or while(1)?
From: Noah Roberts (nroberts_at_dontemailme.com)
Date: 10/02/03
- Next message: Jack Klein: "Re: Library call question"
- Previous message: Jack Klein: "Re: Programmable Logic Circuits and C"
- In reply to: Chris Dollin: "Re: for(;;) or while(1)?"
- Next in thread: Chris Dollin: "Re: for(;;) or while(1)?"
- Reply: Chris Dollin: "Re: for(;;) or while(1)?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 01 Oct 2003 20:42:47 -0700
Chris Dollin wrote:
> Noah Roberts wrote:
>
>
>>Yes, it is much better to use a condition to exit. Many times people do
>>something like the following:
>>
>>while (always)
>> get input
>> if input is quit then die
>>
>> do something
>>...
>>
>>but it is much better do do something like this:
>>
>>boolean quit = false /* or continue = true */
>
>
> 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:
-----Contents of funny.c (good name)----
boolean quit = false /* or continue = true */
while (!quit)
get input
if input is quit then quit = true
else do something
...
-----end contents----
----attempt compile----
jik-@darkstar:~$ gcc -pedantic funny.c
funny.c:1: error: parse error before "quit"
funny.c:3: error: `false' undeclared here (not in a function)
funny.c:3: error: parse error before "while"
-----end attempt----
Unfortunately it quit even trying at that point. It might have been
more entertaining had it continued. Putting it inside of a main loop is
even less entertaining because it stops trying to process the file much
sooner.
What an odd thing to focus on, yet several responders did.
>
>
>>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.
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.
If you want to exit the program at this point then you can call a
function that kills your process and never have to exit your forever
loop. Otherwise the best way is to use a stop switch use it to exit
your loop.
For simple problems the break or return method is acceptable but doesn't
hold up when things get much more complex.
>>Sometimes it is better to break structure, but it always
>>sacrifices maintainability and readability;
>
>
> Not *always*.
>
> The rule that makes sense to me is "code clearly". If it's a choice
> between between being clear and being structured, being clear wins.
I agree. I don't like "loop forever" but I do do things like this:
while test_case
if exception break
continue processing
end
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.
NR
- Next message: Jack Klein: "Re: Library call question"
- Previous message: Jack Klein: "Re: Programmable Logic Circuits and C"
- In reply to: Chris Dollin: "Re: for(;;) or while(1)?"
- Next in thread: Chris Dollin: "Re: for(;;) or while(1)?"
- Reply: Chris Dollin: "Re: for(;;) or while(1)?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|