Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > for(;;) or while(1)?

Reply
Thread Tools

for(;;) or while(1)?

 
 
Minti
Guest
Posts: n/a
 
      10-01-2003
Thomas Matthews <(E-Mail Removed)> wrote in message news:<AOheb.1533$(E-Mail Removed) om>...
> Noah Roberts wrote:
> > Rick wrote:
> >
> >> Hi,
> >>
> >> For portability, can an issue arise if we use while(1) for an infinite
> >> loop in C? If so, should we then use for(;? Thanks,
> >>
> >> Rick
> >>

> >
> > Well using it directly in code it has no matter, but what about this:
> >
> > #define forever for(;
> >
> > do
> > {
> > ...x...
> > } forever;
> >
> > NR
> >

>
> I didn't think this was valid syntax (after macro substitution):
> do
> {
> /* ...x... */
> } for (;; /* after preprocessor substitution */
>
> My understanding is the syntax is:
> do
> {
> } while();
>


Nitpick: replace while() with while(expr)

> I don't believe you can replace the "while" with "for" in this
> syntax. I've never heard of a "do-for" loop.


Me neither. A lot of books would need to be appended if it existed.


--
Imanpreet Singh Arora
iimmaannpprreeeett_aarroorraa@@yyaahhoooo..ccoo..i inn

Singularize the letters above to send mail.
 
Reply With Quote
 
 
 
 
Minti
Guest
Posts: n/a
 
      10-01-2003
"Mike Wahler" <(E-Mail Removed)> wrote in message news:<PZieb.8965$(E-Mail Removed) link.net>...
> "Rick" <rrquick@nospam-com> wrote in message
> news:3f79749d$(E-Mail Removed)...
> > Hi,
> >
> > For portability, can an issue arise if we use while(1) for an infinite
> > loop in C? If so, should we then use for(;? Thanks,

>
> for(; has seven characters, and
> while(1) has eight, so the former should
> weight slightly less, thus be a bit more
> portable.


Mine for loop

for ( ; ; )

has 11 characters

so does my while

while ( 1 )

including whitespace I might add.

>
> Also, if carrying the construct in a backback,
> the rounded edges of 'f' 'o' and 'r' should
> be more comfortable than those pointy 'w', 'h',
> 'i', and 'l' characters.


I guess it all boils down to this *VERY* difference only.


> Seriously, either one is equally portable. Use
> whichever you find most natural, unless you have
> coding standards to follow.


--
Imanpreet Singh Arora
imanpreet_arora AT yahoo DOT co DOT in
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      10-01-2003
Richard Heathfield wrote:
> Thomas Matthews wrote:
>
>
>>Noah Roberts wrote:
>>>Well using it directly in code it has no matter, but what about this:
>>>
>>>#define forever for(;
>>>
>>>do
>>> {
>>> ...x...
>>> } forever;
>>>
>>>NR
>>>

>>
>>I didn't think this was valid syntax (after macro substitution):

>
>
> That was, I believe, the point he was making (i.e. that for(; and while(1)
> are not interchangeable in all cases).


Yeah, that was the point I was making. Some people like to create a
macro like that, I usually just use the actual loop when I am testing
code. I don't like to use forever loops in real code but if I where to
do so I might define something like that.
>
> For what it's worth, I think while(condition) is clearer than either for(;
> or while(1).


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 */

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). Sometimes it is better to break structure, but it always
sacrifices maintainability and readability; this is why goto was
declaired to be evil, yet in some algorithms (for instance verified null
move pruning in chess AI's) it is useful.

99.9999999% of the time it is not necissary to write unstructured code
and since it is a mistake to do so you shouldn't except for that
..000000001% of the time when you need to.

NR

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-01-2003
Joe Wright wrote:
>
> pete wrote:
> >
> > Rick wrote:
> > >
> > > Hi,
> > >
> > > For portability, can an issue arise if we use while(1)
> > > for an infinite loop in C?

> >
> > My compiler generates a warning for that kind of code,
> > when I have the warning level high, where I like it.
> >
> > > If so, should we then use for(;?

> >
> > My compiler doesn't generate a warning for that code,
> > so that's what I use.


> It's just your compiler's rant about programming style. The while (1)
> and for (; constructs are identical.


That's why I let the rant be the tie breaker.

--
pete
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-01-2003
"Minti" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> "Mike Wahler" <(E-Mail Removed)> wrote in message

news:<PZieb.8965$(E-Mail Removed) link.net>...
> > "Rick" <rrquick@nospam-com> wrote in message
> > news:3f79749d$(E-Mail Removed)...
> > > Hi,
> > >
> > > For portability, can an issue arise if we use while(1) for an infinite
> > > loop in C? If so, should we then use for(;? Thanks,

> >
> > for(; has seven characters, and
> > while(1) has eight, so the former should
> > weight slightly less, thus be a bit more
> > portable.

>
> Mine for loop
>
> for ( ; ; )
>
> has 11 characters
>
> so does my while
>
> while ( 1 )
>
> including whitespace I might add.
>
> >
> > Also, if carrying the construct in a backback,
> > the rounded edges of 'f' 'o' and 'r' should
> > be more comfortable than those pointy 'w', 'h',
> > 'i', and 'l' characters.

>
> I guess it all boils down to this *VERY* difference only.


Everyone knows that whitespace characters are weightless.

-Mike


 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      10-01-2003
osmium wrote:
> Noah Roberts writes:
>
>
>>but it is much better do do something like this:
>>
>>boolean quit = false /* or continue = true */

>
>
> I wouldn't expect the alternative to work.
> Pedants, especially, should test their code before positng.
>
>


If you use continue = true you must alter the loop just slightly.

while (continue)
{

if (wanttoquit) continue = false;
else dostuff...
}

I guess some people must have it spelled out for them, I assumed too
much. And no, I was not being pedantic but you have the right to that
opinion.

NR

 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      10-01-2003
Noah Roberts writes:

> but it is much better do do something like this:
>
> boolean quit = false /* or continue = true */


I wouldn't expect the alternative to work.
Pedants, especially, should test their code before positng.


 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      10-01-2003
Noah Roberts <(E-Mail Removed)> wrote:

> while (continue)


Erm... choose a different name.

Richard
 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      10-01-2003
In article <blepj0$ish$(E-Mail Removed)>, Noah Roberts wrote:
> osmium wrote:
>> Noah Roberts writes:
>>
>>
>>>but it is much better do do something like this:
>>>
>>>boolean quit = false /* or continue = true */

>>
>>
>> I wouldn't expect the alternative to work.
>> Pedants, especially, should test their code before positng.
>>
>>

>
> If you use continue = true you must alter the loop just slightly.
>
> while (continue)
> {
>
> if (wanttoquit) continue = false;
> else dostuff...
> }
>
> I guess some people must have it spelled out for them, I
> assumed too much. And no, I was not being pedantic but you
> have the right to that opinion.


continue is a keyword.

--
Neil Cerutti
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      10-01-2003
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 ...

> 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.

Assuming `always` is well-named, the first one has on exit. N-and-a-half
loops are just as structured as N loops. The idiom

while (yourChoiceForTrue)
{ preamble;
if (doneExpression) break;
postamble; }

is well-structured and worth knowing.

> 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.
Usually you don't need to make the choice.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments