while (1) vs. for ( ;; )

M

Michael B Allen

Should there be any preference between the following logically equivalent
statements?

while (1) {

vs.

for ( ;; ) {

I suspect the answer is "no" but I'd like to know what the consensus is
so that it doesn't blink through my mind anymore when I type it.

Mike
 
W

Walter Roberson

Should there be any preference between the following logically equivalent
statements?
while (1) {

for ( ;; ) {
I suspect the answer is "no" but I'd like to know what the consensus is
so that it doesn't blink through my mind anymore when I type it.

Just my opinion, but I would tend to use them in slightly different
contexts.

I would tend to use while(1) when I was looping for something that
will occur at an indeterminate future time, such as EOF or an
alarm signal.

I would tend to use for(;;) when I was looping for something that
will occur within a bounded time, but which is inconvenient to
express through loop control variables, such as iterating through
a set of data structures until a certain property is detected.

To give it a different slant: for(;;) conveys "iteration", whereas
while(1) conveys "repetition" that is not necessarily iterative.

But as the two work out the same in the end, the choice amounts
to no more than a hint to a human reader.
 
G

Guillaume

Question of style, nothing else.
Personally, I tend to prefer 'for (;;)', but that's just my style.

It's like asking which is better:

if () {
}

or:

if ()
{
}

I use the second form exclusively for various reasons, but it's a matter
of style. Doesn't change the code one bit.
 
R

Robert Gamble

Michael said:
Should there be any preference between the following logically equivalent
statements?

while (1) {

vs.

for ( ;; ) {

I suspect the answer is "no" but I'd like to know what the consensus is
so that it doesn't blink through my mind anymore when I type it.

Many consider the latter preferable, it can be read as "for-ever" and
makes the intention of the programmer clearer. "while" implies a
predicate which may not always be true whereas "for" does not.

Robert Gamble
 
C

Chris McDonald

Michael B Allen said:
Should there be any preference between the following logically equivalent
statements?
while (1) {
vs.
for ( ;; ) {
I suspect the answer is "no" but I'd like to know what the consensus is
so that it doesn't blink through my mind anymore when I type it.


Preference? Why not move on to something whose intention is clear:

while(true) {
 
B

Ben Pfaff

Chris McDonald said:
Preference? Why not move on to something whose intention is clear:

while(true) {

In what way are the former two statements' intentions not clear?
Both of them obviously loop "forever".
 
P

pete

Michael said:
Should there be any preference between the following logically equivalent
statements?

while (1) {

vs.

for ( ;; ) {

I suspect the answer is "no" but I'd
like to know what the consensus is
so that it doesn't blink through my mind anymore when I type it.

while(1) gets a warning on my compiler.
for(;;) doesn't.
 
C

Chris McDonald

In what way are the former two statements' intentions not clear?
Both of them obviously loop "forever".


I would suggest that both while(1) and for(;;) are potentially unclear
to a person (undergrad. student) seeing C for the first time
(while assuming they haven't seen Java or C++, either).

OK, I suggest that while(true) is *clearer*.
 
K

Keith Thompson

Chris McDonald said:
I would suggest that both while(1) and for(;;) are potentially unclear
to a person (undergrad. student) seeing C for the first time
(while assuming they haven't seen Java or C++, either).

I would suggest that anyone who doesn't understand while(1) or for(;;)
isn't going to have much chance of understanding what follows it.
 
C

Chris McDonald

I would suggest that anyone who doesn't understand while(1) or for(;;)
isn't going to have much chance of understanding what follows it.


I'm sure that everyone reading this newsgroup understands the *role*
of while(1) and for(;;) but, as suggested, consider how a beginner
may read these the first few times.

I believe that it's these little points that can make one textbook a
better introduction than another. It's difficult to argue for the
continued writing of new code that could be written in a clearer fashion.
 
R

Randy Howard

Robert Gamble wrote
(in article
Many consider the latter preferable, it can be read as "for-ever" and
makes the intention of the programmer clearer. "while" implies a
predicate which may not always be true whereas "for" does not.

This is where Pascal has an advantage. (I probably have some of
the syntax wrong, I haven't written any Pascal since the late
80s)

const hell_freezes_over = false;

repeat
something;
until hell_freezes_over;

Seems to convey the intent quite clearly. :)
 
R

Richard Heathfield

Michael B Allen said:
Should there be any preference between the following logically equivalent
statements?

while (1) {

vs.

for ( ;; ) {

I suspect the answer is "no" but I'd like to know what the consensus is
so that it doesn't blink through my mind anymore when I type it.

while(1) will be flagged by many compilers as "conditional expression is
constant" or some such wording, whereas for(;;) will not be. Consequently,
for(;;) is preferable out of these two choices.

Personally, I prefer neither choice! I would rather have the loop control
statement explicitly document the exit condition (unless there genuinely
isn't one, such as might be the case in an electronic appliance like a
microwave oven, where "forever" can roughly be translated as "whilst power
is being supplied to the appliance").
 
C

Christian Bau

pete said:
while(1) gets a warning on my compiler.
for(;;) doesn't.

Could you tell us which compiler? And could you check what it thinks
about

do {
...
} while (0);

?
 
A

akarl

Randy said:
Robert Gamble wrote
(in article



This is where Pascal has an advantage. (I probably have some of
the syntax wrong, I haven't written any Pascal since the late
80s)

const hell_freezes_over = false;

repeat
something;
until hell_freezes_over;

Seems to convey the intent quite clearly. :)

Well, in C we have the similar do-while statement. In Modula on the
other hand there is a real exit-in-the-middle loop construct:

LOOP
...
IF someCondition THEN EXIT END;
...
END


August
 
R

Richard Tobin

Chris McDonald said:
I'm sure that everyone reading this newsgroup understands the *role*
of while(1) and for(;;) but, as suggested, consider how a beginner
may read these the first few times.

If you're writing code for beginners to read you may well come to
different conclusions from those where you're writing a program to be
maintained by experienced programmers.

-- Richard
 
G

Guillaume

Randy said:
const hell_freezes_over = false;

repeat
something;
until hell_freezes_over;

Seems to convey the intent quite clearly. :)

That is, if hell means anything to you. If it doesn't, then it does not
convey much. ;-)
 
A

Anton Petrusevich

Chris said:
I would suggest that both while(1) and for(;;) are potentially unclear
to a person (undergrad. student) seeing C for the first time
(while assuming they haven't seen Java or C++, either).

What about following?
#defined loop_forever for(;;)
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top