for(;;) or while(1)?

R

Rick

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
 
A

Andreas Kahari

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,

Some C compilers may generate slightly different machine code,
but they ought to always be functionally identical. Choosing
one instead of the other will not break a program or make it
unportable.
 
P

pete

Rick said:
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.
 
I

Ivan Vecerina

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

It should come down to the same.

However, I use for(;;) because I use some compilers which send out
warnings of the kind: "Constant used as conditional expression".

go figure...
 
J

Jeremy Yallop

Rick said:
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,

There aren't any real portability issues, but the `1' seems too
arbitrary to me (as pointed out in <
which is where I first saw that objection, I think. Using `true'
seems less objectionable, somehow, if you have a C99 (or C++) compiler
(or define it yourself).

Jeremy.
 
T

Tristan Miller

Greetings.

Jeremy Yallop said:
There aren't any real portability issues, but the `1' seems too
arbitrary to me

More so than while(42)? There's plenty of precedent for using '1' as a
generic truth constant; less so for other nonzero numbers.
 
J

j

Rick said:
Hi,

For portability, can an issue arise if we use while(1) for an infinite
loop in C?
No.

If so, should we then use for(;;)? Thanks,

You use for(;;) when you do not want to imply a test, otherwise for that
purpose, you use while(1)
The practical result will be the same for all but the very stupidest
compilers.
 
N

Noah Roberts

Rick said:
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
 
R

Rick

Jeremy said:
I wouldn't expect *any* constant to get rid of the warning message.

Wasn't that what Ivan said.. the compiler issues a "Constant used as
conditional expression" warning. How will *any* constant get rid of the
warning when the warning is about the use of a constant itself?
I much prefer the while(1) construct. The for:)::) looks to me like someone is
trying to impress someone with their arcane knowledge.

I don't think it's about making impressions, I'd say it's a matter of
taste. I myself use for(;;).. I believe it's much clearer to what's
happening; and like you said "while implies a condition that will vary",
that's just why while(constant) can be a little misleading at first
glance. My compiler once gave me a warning for while(constant), I
wondered if using such a constract was non-portable, but that's clearly
not the case.

Rick
 
T

Thomas Matthews

Noah said:
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();

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

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
O

osmium

Jeremy said:
There aren't any real portability issues, but the `1' seems too
arbitrary to me (as pointed out in
which is where I first saw that objection, I think. Using `true'
seems less objectionable, somehow, if you have a C99 (or C++) compiler
(or define it yourself).

I wouldn't expect *any* constant to get rid of the warning message. while
implies a condition that will vary. I can see the reason for the message
but I'm glad the compiler I have doesn't issue such a warning. I much
prefer the while(1) construct. The for:)::) looks to me like someone is
trying to impress someone with their arcane knowledge.
 
M

Mike Wahler

Rick said:
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.

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

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

-Mike
 
J

Jeremy Yallop

[snip]

I didn't write any of the quoted text. Please be more careful with
your attributions.

Jeremy.
 
J

Joe Wright

pete said:
My compiler generates a warning for that kind of code,
when I have the warning level high, where I like it.


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

Minti

Rick said:
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 I use

while ( 1 )

if I wish to.

BUT

I use

for ( ; ; )

If I wish to.

In terms of performance any *intelligent* compiler would produce the
same binary code. Though a compiler made possibly as a *experimental
project* **might** not do the optimization and might include a sort of
condition for checking the truth value of the expression in while,
which would be true, always. OTOH if you love goto you might also
wanna use

inf_loop:
......
goto inf_loop;

The last one would have the same performence level as that of a for( ;
; ).

why not use

for ( ; 1 ; )

which is *completly* semantically equivalent to the while ( 1 )
 
R

Rick

Sorry about that Jeremy, I meant to qoute Osmium.


Rick

Jeremy said:
[snip]

I didn't write any of the quoted text. Please be more careful with
your attributions.

Jeremy.
 
R

Richard Heathfield

Thomas said:
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).

For what it's worth, I think while(condition) is clearer than either for(;;)
or while(1).
 
A

Arthur J. O'Dwyer

Yea you did! Read your reply again! It's attached just in case
you need to see which one!

Maybe osmium means that someone else has been posting to Usenet
using his account... ;-)

Please don't post attachments to non-binaries groups.
A simple link to Google Groups, or, even better, a quick
reminder to "look three messages up the darn thread and
*read* it" would have sufficed.

And in general, don't use spaces in filenames. Some
uudecode clients might not even parse your attachment
correctly (I dunno what any RFC on the topic says; I'm
just pointing out the possibility).

-Arthur
 
M

Minti

Thomas Matthews said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top