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
Anton said:Chris McDonald wrote:
What about following?
#defined loop_forever for(;
akarl said:Defining a new language with the macro preprocessor is seldom a good idea.
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.
Anton Petrusevich said:What about following?
#defined loop_forever for(;
They do exactly the same thing. I use the "for ever" form because someShould 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.
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").
(in article said:That is, if hell means anything to you. If it doesn't, then it does not
convey much. ;-)
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.)
Randy said:It seems that the expression "when hell freezes over" is not as
widespread as I thought.
Anton said:What about following?
while(1 == 1) {
...
}
and
do {
...
} while(1 == 0);
Alan Balmer said:
Yes: done = 1;
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.
Chris said:Preference? Why not move on to something whose intention is clear:
while(true) {
Kevin Handy said:Why not use
while(does_a_bear_shit_in_the_woods) {
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;
Randy said:Guillaume wrote
It seems that the expression "when hell freezes over" is not as
widespread as I thought.
Anton Petrusevich said:akarl wrote:
I agree with you, but I was asking Chris McDonald.
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.
Only because you then have to chase after call_the_real_function(). OnI 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;
I suppose, but I miss the relevance.The 'relatively long functions' are where the bugs typically breed, no?
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.
Anton Petrusevich said:What about following?
#defined loop_forever for(;
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.