while (1) vs. for ( ;; )

A

akarl

Anton said:
Chris McDonald wrote:




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

Defining a new language with the macro preprocessor is seldom a good idea.

August
 
K

Kenneth Brody

I typically read them as "while true" and "forever".

They are functionally identical, and will possibly generate the same
machine code.
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 typically declare an "int done=0;" and then use "while (!done)" in
such cases. It just reads better to me and allows a "clean" exit from
the loop.
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.

True.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
B

Ben Pfaff

Anton Petrusevich said:
What about following?
#defined loop_forever for(;;)

As long as you want to be "cute":
#define ever ;;
for (ever) { ... }
(I would never use this.)
 
A

Alan Balmer

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.
They do exactly the same thing. I use the "for ever" form because some
of the compilers I use give a warning on while(1).
 
A

Alan Balmer

Michael B Allen said:


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

It often happens that there are multiple exit conditions. Is it your
preference to set a "get out" flag for these situations? That's my
preference, though I don't feel strongly about it (and I'm likely to
use multiple function returns, too, when a single return is too
contrived.)
 
R

Randy Howard

Guillaume wrote
(in article said:
That is, if hell means anything to you. If it doesn't, then it does not
convey much. ;-)

It seems that the expression "when hell freezes over" is not as
widespread as I thought. :)
 
R

Richard Heathfield

Alan Balmer said:
It often happens that there are multiple exit conditions. Is it your
preference to set a "get out" flag for these situations?

Yes: done = 1;
That's my
preference, though I don't feel strongly about it (and I'm likely to
use multiple function returns, too, when a single return is too
contrived.)

I'm quite unlikely to use multiple returns, although I seem to recall that I
have done so on occasion, when in a tearing hurry.
 
P

pete

Anton said:
What about following?
while(1 == 1) {
...
}
and
do {
...
} while(1 == 0);

My compiler warns about a constant expression being
he condition in a loop.
(1 == 1) and (1 == 0) are both such constant expressions.
 
A

Alan Balmer

Alan Balmer said:


Yes: done = 1;

I've also used a flag with multiple values, when I need to know why
the loop was terminated.
I'm quite unlikely to use multiple returns, although I seem to recall that I
have done so on occasion, when in a tearing hurry.

The situation where I'm likely to use multiple returns is for sanity
checks at the beginning of a relatively long function;

if (something_strange_about_the_parameters)
return 0;

<do the real stuff>

return 1;
 
K

Kevin Handy

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

while(true) {

Why not use

while(is_the_pope_catholic) {

or

while(does_a_bear_shit_in_the_woods) {

or

while(!does_a_fish_need_a_bicycle) {

these should be just as clear, as long as you define them properly.
 
M

Mark B

Kevin Handy said:
Why not use
while(does_a_bear_shit_in_the_woods) {

I wouldn't recommend using this example as it is a little ambiguous...
Does this bear live in the woods or does he live at the circus?
There's no telling where he shits.

Mark













;-)
 
M

Mark B

Alan Balmer said:
I've also used a flag with multiple values, when I need to know why
the loop was terminated.
The situation where I'm likely to use multiple returns is for sanity
checks at the beginning of a relatively long function;
if (something_strange_about_the_parameters)
return 0;
<do the real stuff>
return 1;

I know it's only a trivial example... but why not:

if(nothing_strange_about_the_parameters)
status = call_the_real_function(passing_validated_parameters);
return status;

The 'relatively long functions' are where the bugs typically breed, no?

Mark
 
J

John Bode

Randy said:
Guillaume wrote


It seems that the expression "when hell freezes over" is not as
widespread as I thought. :)

Localization issues crop in the damndest places, don't they ;-)
 
C

Chris McDonald

Anton Petrusevich said:
akarl wrote:
I agree with you, but I was asking Chris McDonald. :)


Hi Anton,

I too don't think using the preprocessor that way helps (my) students.
There's a potential that they'll flounder at the first opportunity -
either by not adding the directive themselves, or not having a supplied
(non-standard) header file.
 
P

Peter Nilsson

Chris said:
Hi Anton,

I too don't think using the preprocessor that way helps (my) students.
There's a potential that they'll flounder at the first opportunity -
either by not adding the directive themselves, or not having a supplied
(non-standard) header file.

Then might they not also flounder with while(true) by not supplying the
standard header <stdbool.h>?
 
A

Alan Balmer

I know it's only a trivial example... but why not:

if(nothing_strange_about_the_parameters)
status = call_the_real_function(passing_validated_parameters);
return status;
Only because you then have to chase after call_the_real_function(). On
the initial writing, this would probably be only a few lines below,
but that could change. Hmm... Actually, the more I think about it, the
less I like it. Each function turns into two, and the validation's
connection to the rest of the code is more tenuous than necessary. You
could impose a convention, I suppose - every function foo() has a
corresponding function validate_foo(). Even then, you have two
functions which need to be synchronized, a bad maintenance situation.
All in all, I think I prefer the single function with two returns. I
just find it silly to have a couple of lines in an if, followed by a
hundred lines in the else, just to conform to the "single return"
rule.
The 'relatively long functions' are where the bugs typically breed, no?
I suppose, but I miss the relevance.
 
W

websnarf

pete said:
My compiler warns about a constant expression being
he condition in a loop.
(1 == 1) and (1 == 0) are both such constant expressions.

Indeed, my experience is that WATCOM C/C++ does not like the do {...}
while(0), PC-Lint does not like while(1) { ... } and most compilers
don't like the 1==1 or 1==0 constant expressions. for(;;) is the one
that wrankles me the most from an intuitive point of view because there
is *no* stated condition, so I don't see why it should be assumed to be
either true or false -- but that's the one which all the compilers and
LINTs I have agree is acceptable.

Personally, I prefer while(1) { ... } just because its explicit about
its intentions right up front, but once you know that for(;;) is
equivalent, its not a big deal to switch using it instead and move on.
(Its not like the a==0 vs 0==a choice in which there is a real issue.)
 
K

Keith Thompson

Anton Petrusevich said:
What about following?
#defined loop_forever for(;;)

I remember writing either
#define ever ;;
or
#define ever (;;)
when I was in college, but it was a youthful indiscretion.

At the time, I thought it was quite clever. I still think so, but I
no longer think its cleverness was a virtue.
 

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,777
Messages
2,569,604
Members
45,223
Latest member
Jurgen2087

Latest Threads

Top