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

From: Noah Roberts (nroberts_at_dontemailme.com)
Date: 10/02/03


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



Relevant Pages

  • Re: for(;;) or while(1)?
    ... > if input is quit then die ... a "mid-test" loop. ... syntax for a pretest loop and posttest loop, ... There is exactly one exit. ...
    (comp.lang.c)
  • Re: breaking a loop
    ... I think u need break before exit() ... so if u want break from any loop just add break ... I'm just learning Python....thanks in advance... ... when I type 'exit' the program should quit. ...
    (comp.lang.python)
  • Re: Poll: Using the Goto Delphi statement?
    ... but I dislike until false just as much as I dislike while true. ... while forever do ... I do agree the loop - exit approach would be fine. ...
    (borland.public.delphi.non-technical)
  • Re: Infinite Loops and Explicit Exits
    ... > CS> One of these proposals relaxes the current restriction that an EXIT ... > termination of the loop is not visible at that point. ... > terminating condition is visible in that context. ... > You now want to allow this remote procedure, ...
    (comp.lang.cobol)
  • Re: Houston-related rants was Re: forced merges/inside lane merges/AASHTO "tapered merges"
    ... >> Are you sure about the North Loop and I-45? ... >> in particular the westbound exit for Fondren/Gessner. ... How could HCTRA put an exit ramp for eastbound ... and the West Houston Center Blvd exit still shows Old Westheimer ...
    (misc.transport.road)