While(1) or for(;;) for infinite loops?

N

Not Really Me

I assume this has come up before but I am getting nowhere with searches.

To make an infinite loop, while(1) tends to generate lint/compiler warnings,
for(;;) does not.

Other than personal preference, are there any other technical reasons to
pick one over the other?

I will stipulate for the argument that performance is identical.

--
Scott
Validated Software
Lafayette, CO




__________ Information from ESET NOD32 Antivirus, version of virus signature database 4661 (20091204) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
K

Keith Thompson

Not Really Me said:
I assume this has come up before but I am getting nowhere with searches.

To make an infinite loop, while(1) tends to generate lint/compiler warnings,
for(;;) does not.

Other than personal preference, are there any other technical reasons to
pick one over the other?

I will stipulate for the argument that performance is identical.

No. Both forms are common C idioms; use whichever is more convenient.
("while(1)" might be clearer than "for(;;)" to someone who doesn't
know C very well, but I wouldn't worry about that.)
 
A

Antoninus Twink

for(;;) is meaningless to anyone who doesn't know C, or even to
someone with a reasonable but not extensive knowledge of the language.
while(1) or while(true) is used by several programming lanauges to
indicate infinite loops.

This sounds pretty bogus to me, Malcolm.

Plenty of C-like languages (C++ and Java certainly, probably C# too)
will accept for(;;) as an infinite loop.

And it's such a common idiom in C, that if someone reading C code
doesn't understand it and doesn't have the gumption to look up what it
means, then they're not very likely to get much out of the rest of the
code.

for(;;) is also much more elegant and compact than while(1), avoiding as
it does that ugly pseudo-boolean constant.
 
B

bartc

for(;;) is also much more elegant and compact than while(1), avoiding as
it does that ugly pseudo-boolean constant.

While we're talking about aesthetics, then both for() and while() are
cleaner.

There's a condition missing from the while statement, but then there's also
one missing from the for(;;).
 
K

Keith Thompson

bartc said:
While we're talking about aesthetics, then both for() and while() are
cleaner.

There's a condition missing from the while statement, but then there's
also one missing from the for(;;).

If you mean literally "for()" and "while()", I hardly think that the
aesthetics of code that won't compile is relevant. I personally like

loop
...
end loop;

but that's not C either.
 
B

bartc

Keith Thompson said:
If you mean literally "for()" and "while()", I hardly think that the
aesthetics of code that won't compile is relevant.

That's a minor detail. I don't think allowing an empty condition on while(),
to match the empty one in for(;;), or taking those two semicolons as read
(since they are not separating or terminating anything), is too taxing for
compiler maintainers.

(And for all I know, extensions might already exist for those.)
I personally like

loop
...
end loop;

but that's not C either.

No, that's a completely different syntax style and certainly not C. I'm
talking about being allowed to leave out one or two characters which don't
contribute anything.
 
B

Beej Jorgensen

bartc said:
While we're talking about aesthetics, then both for() and while() are
cleaner.

Sorry for the digression, but:

Using Pascal in college, my friend liked to use the significantly
less-clean:

repeat
...
until false;

which always made me giggle.

-Beej
 
W

Willem

bartc wrote:
) )> for(;;) is also much more elegant and compact than while(1), avoiding as
)> it does that ugly pseudo-boolean constant.
)
) While we're talking about aesthetics, then both for() and while() are
) cleaner.
)
) There's a condition missing from the while statement, but then there's also
) one missing from the for(;;).

I think, if you're going to extend the language, that:

do { ... }

without the while(...), would be the most intuitive infinite loop.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
E

Eric Sosman

bartc said:
That's a minor detail. I don't think allowing an empty condition on
while(), to match the empty one in for(;;), or taking those two
semicolons as read (since they are not separating or terminating
anything), is too taxing for compiler maintainers.

(And for all I know, extensions might already exist for those.)


No, that's a completely different syntax style and certainly not C. I'm
talking about being allowed to leave out one or two characters which
don't contribute anything.

You'll achieve *far* greater brevity by jettisoning all
that silly white space (comments are white space, too). Try
that first, then come back if you feel a need for even more
compression.
 
K

Keith Thompson

Joe Wright said:
Certainly not C. It is xBASE (dBASE, FoxBASE, FoxPro, Clipper,
et. al.) and is used in FOR/NEXT and DO WHILE/ENDDO constructs. LOOP
is equivalent to continue; and EXIT equivalent to break;.

I prefer C but am paid to do xBASE.

<OT>
Actually, it's Ada. In this context, "loop" is like C's "while (1) {",
and "end loop;" is like C's "}". I'm not familiar with xBASE, but
from the snippets I just Googled it doesn't like it uses the same
syntax; I'm seeing see end-of-loop markers like "ENDDO" and "ENDFOR",
not "end loop".
</OT>

All of which is, of course, beside the point.
 
K

Kaz Kylheku

I assume this has come up before but I am getting nowhere with searches.

To make an infinite loop, while(1) tends to generate lint/compiler warnings,
for(;;) does not.

Other than personal preference, are there any other technical reasons to
pick one over the other?

Even MIPS assembly language disguises the fact that in the instruction
set, an unconditional branch is actually represented like this:

BEQ zero, zero, target ;; zero denotes the always-zero R0 pseudo-register

in MIPS assembly language (and dis-assembly, e.g. by GNU binutils objdump),
it's usually written like this, which generates the same instruction:

B target

Thus, while (1) is less abstract than the unconditional branch in MIPS
assembly.
 
L

Lew Pitcher

Sorry for the digression, but:

Using Pascal in college, my friend liked to use the significantly
less-clean:

repeat
...
until false;

which always made me giggle.

In C, however, we have

do
...
while(1);

which amounts to the same thing, doesn't it?

<grin>
 
B

bartc

Willem said:
bartc wrote:
) )> for(;;) is also much more elegant and compact than while(1), avoiding
as
)> it does that ugly pseudo-boolean constant.
)
) While we're talking about aesthetics, then both for() and while() are
) cleaner.
)
) There's a condition missing from the while statement, but then there's
also
) one missing from the for(;;).

I think, if you're going to extend the language, that:

do { ... }

without the while(...), would be the most intuitive infinite loop.

Yes. But that's a more substantial syntax change.

And anyway someone will be along in a minute with some tacky macros to
emulate any loop construct you can think of, so the chances of even my
trivial suggestion making it into the language proper are minimal.
 
C

Curtis Dyer

for(;;) is meaningless to anyone who doesn't know C,

Why do you say that? This construct works in PHP, Perl, ECMAScript,
and probably other languages that have C-like for loops.
or even to
someone with a reasonable but not extensive knowledge of the
language.

Is it really that esoteric? Personally, it never seemed that way to
me.

<snip>
 
K

Kaz Kylheku

bartc wrote:
) )> for(;;) is also much more elegant and compact than while(1), avoiding as
)> it does that ugly pseudo-boolean constant.
)
) While we're talking about aesthetics, then both for() and while() are
) cleaner.
)
) There's a condition missing from the while statement, but then there's also
) one missing from the for(;;).

I think, if you're going to extend the language, that:

do { ... }

without the while(...), would be the most intuitive infinite loop.

Oh goodie, just what we need: another if/else ambiguity!

do
do { }
while (condition); /* oops, goes with the inner do. right? */

:)
 
J

James

bartc said:
Yes. But that's a more substantial syntax change.

And anyway someone will be along in a minute with some tacky macros to
emulate any loop construct you can think of, so the chances of even my
trivial suggestion making it into the language proper are minimal.


#define FOREVER for (;;)


void foo()
{
FOREVER
{
/* [...] */
}
}


lol! I could not resist.
 
R

root

That's pretty funny Richard - for all your negative words about Antonius,
you've just copied his post almost word for word!


/root
 
B

bartc

James Tursa said:
It's actually pretty close to Fortran:

do
...
enddo

Fortran must have changed somewhat since the last time I used it (ie.
version IV).

But that brings up an interesting point: some languages seem happy to evolve
and to embrace new syntax, but some, like C, don't.

And I feel sure that the presence of the preprocessor in C has some
responsibility for this, by providing a poor man's way of adding 'new'
language constructs.
 
B

bartc

Tim Streater said:
I'm not saying its illegible, but to me a for-loop implies that a loop is
going to run some predefined number of times, whereas a while-loop to me
implies that there will be some break/exit condition within it.


Ah, you mean while(true==true) or perhaps while(false==false) ??

Somehow I don't think RH would ever write such an infinite loop, as that
would imply having to escape from it using break, return, goto or exit()
instead.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top