A
Army1987
Chris Dollin said:Richard Heathfield wrote:
while(1)
{
preamble();
if (doneCondition()) break;
postamble();
}
How 'bout:
for (preamble(); !doneCondition(); preamble())
postamble();
Chris Dollin said:Richard Heathfield wrote:
while(1)
{
preamble();
if (doneCondition()) break;
postamble();
}
Richard said:Chris Dollin said:
And the most common one of those [1] is the venerable n-and-a-half
loop:
while(1)
{
preamble();
if (doneCondition()) break;
postamble();
}
They happen often enough that I wish there were a more constrained
syntax that said that.
Yes, so do I. Nevertheless...
Now, this loop is (we hope) not infinite; does the `while(1)` trip
your abberation detectors, and what would your approach be to the
problem?
It does, because (in my very simplistic and naive view) while(1) is
making a promise that the break breaks, and I don't like breaking
promises if I can avoid it, so I would simply do this:
int done = 0;
while(!done)
{
preamble();
done = doneCondition();
if(!done)
{
postamble();
}
}
Yes, it's slightly more code. I don't find this to be a big deal,
although some people obviously do. Yes, it uses an extra object. I
don't find this to be a big deal, either, although some people
obviously do. But I /do/ find it to be clearer, although some people
obviously don't.
Mark McIntyre said:On 29 Mar 2007 08:55:46 -0700, in comp.lang.c , "hstagni"
This is a FAQ. - 19.1
Quick answer: you can't, C's standard IO functions all require you to
press a key to tell the programme you've finished typing.
Longer answer: your OS probably has a lower level function which can
do this. You may need to find out how to multithread your code.
But it'd still block for input, something the OP doesn't want. He
wants an asynchronous notification, on a key press.
Army1987 said:How 'bout:
for (preamble(); !doneCondition(); preamble())
postamble();
Chris Dollin said:How 'bout:
for (preamble(); !doneCondition(); preamble())
postamble();
Only works if the ambles will fit inside the syntax of a
`for` [I often find the preamble has a declaration and I'm
not yet ready to commit to the C99 behaviour, even though
that's what's /right/].
Anything fits inside the syntax of a for (except declarations
The more conventional form of infinite loop prefix is
for ( ; ; )
jaysome said:The more conventional form of infinite loop prefix is
for ( ; ; )
Chris said:Richard Heathfield wrote:
.... snip ...
/That/ trips my abomination detector. Messing around with a new
state variable, just so that `repeat A until B do C endrepeat`,
which has an idiomatic encoding using `break` can be written
without using `while(1)` ...
Army1987 said:Chris Dollin said:while(1)
{
preamble();
if (doneCondition()) break;
postamble();
}
How 'bout:
for (preamble(); !doneCondition(); preamble())
postamble();
Only works if the ambles will fit inside the syntax of a
`for` [I often find the preamble has a declaration and I'm
not yet ready to commit to the C99 behaviour, even though
that's what's /right/].
Anything fits inside the syntax of a for (except declarations) if you use
commas instead of colons.
Nick said:they are funtionally identical (on any reasonable compiler).
for(; is a particular C idiom. Perhaps a bit "clever".
Apparently for(; is less likely to generate a warning.
On 30 Mar 2007 16:50:37 GMT said:Also, the for(; version allows the "clever" macro:
#define EVER ;;
Giving one (if one is inclined towards the "clever"):
for(EVER)
{
}
Richard Heathfield said:jaysome said:
I consider "infinite loops" to be an aberration, not an idiom.
Your turn.
Jorgen said:Clever, but misleading.
for(EVER) {
/* ... */
/* nothing lasts forever */
break;
}
When I look at "Lions' Commentary on Unix 6th Edition", and I pageRichard Heathfield said:I consider "infinite loops" to be an aberration, not an idiom.
through the source code to Sheet 15
...and pass my eye to main()... (line 1550)
...and let my eye read the code to line 1562.. this is what I see...
for (; {
UISA->r[0] = 1;
if(fuibyte(0) < 0)
break;
clearseg(i);
maxmem++;
mfree(coremap, 1, i);
i++;
}
Is this an abberration?
How would you write it non-abberantly?
I am new to C and would like to learn to avoid abberant coding.
Furthermore, I am learning from K&R2, as well as...
Kernighan and Pike's "The Practice of Programming",1999
On page p12 they write:
"For an infinite loop, we prefer
for(;
...
but
while(1)
...
is also popular. Don't use anything other than these forms"
Again, they seem to think its an idiom; and that it should be used.
Richard said:ilan pillemer said:When I look at "Lions' Commentary on Unix 6th Edition", and I pageRichard Heathfield said:I consider "infinite loops" to be an aberration, not an idiom.
through the source code to Sheet 15
...and pass my eye to main()... (line 1550)
...and let my eye read the code to line 1562.. this is what I see...
for (; {
UISA->r[0] = 1;
if(fuibyte(0) < 0)
break;
clearseg(i);
maxmem++;
mfree(coremap, 1, i);
i++;
}
Is this an abberration?
I consider it to be so, yes. It uses one C feature to defeat another.
How would you write it non-abberantly?
done = 0;
while(!done)
{
UISA->r[0] = 1;
if(!(done = fuibyte(0) < 0))
{
clearseg(i); etc etc
}
}
#define EVER ;;
Also, the for(; version allows the "clever" macro:
And maybe
#define NEVER ;0;
(useful for example code which can't simplily being commented out because it
contains comments itself...)
ais523 said:Instead of commenting out code, you can use
#if 0
/* code here */
#endif
which has the advantage of nesting.
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.