J
James Watt
can anyone tell me how to do an infinite loop in C/C++, please ?
this is not a homework question .
this is not a homework question .
James said:can anyone tell me how to do an infinite loop in C/C++, please ?
this is not a homework question .
James said:can anyone tell me how to do an infinite loop in C/C++, please ?
this is not a homework question .
James Watt said:can anyone tell me how to do an infinite loop in C/C++, please ?
this is not a homework question .
James said:can anyone tell me how to do an infinite loop in C/C++, please ?
this is not a homework question .
James said:can anyone tell me how to do an infinite loop in C/C++, please ?
A better question is "why do an infinite loop?". Loops that are
deliberately written to be infinite are rare; ones where it was a good
idea are even rarer. Why do you want to do this?
Pietro said:Main loop in event-driven approach?
It would probably have some break statement to exit the loop when some
particular event happens, but it would still be an infinite loop ;-)
Pietro Cerutti wrote:
A loop that ever actually exits should not be written as if it were an
infinite loop. It's misleading and confusing. The main way of leaving
a loop should always involve the constructs that make it a loop.
A loop that ever actually exits should not be written as if it were an
infinite loop. It's misleading and confusing. The main way of leaving
a loop should always involve the constructs that make it a loop.
Jim said:for ( ;; )
/**/;
while (true)
/**/;
do
{ /**/ }
while ( true );
James said:can anyone tell me how to do an infinite loop in C/C++, please ?
this is not a homework question .
Gene said:I disagree with this if you mean that a loop should always exit
at the loop head or tail.
for (; {
... // yada yada
if ( <exit condition> ) break;
... // more yada yada
}
Gene said:I disagree with this if you mean that a loop should always exit at the
loop head or tail.
for (; {
... // yada yada
if ( <exit condition> ) break;
... // more yada yada
}
is very often clearer than a loop that uses some contrived boolean
flag and sentinel conditionals just so that it can exit at the header
Both goto statements and infinite loops, when typed in with the fingers
connected to the brain, in some cases could lead to better code quality
(read: clarity) than any other construct providing the same functionality.
I didn't say "always". I said "the main way". Breaking out of a loop by
other methods is OK, so long as it's reserved for exceptional exits from
the loop.
I'm no fan of the use of boolean flags for this purpose. In my
experience, you can almost always re-write the code test the exit
condition directly in loop construct itself. However, in the rare cases
where that can't be done, I prefer the boolean flag over a loop
construct that incorrectly gives the impression that it never exits.
can anyone tell me how to do an infinite loop in C/C++, please ?
this is not a homework question .
for(yadayada(); condition; moreyadayada())
{
}
is clearer still, no?
Only if yadayada has to be executed once. In the first loop it is
executed at every iteration.
Richard said:If you need to exit the loop "in the middle": while(1) { condition = foo();
if(!condition) { break; } bar(); } it's always because of an exit
condition that may or may not be true, that you must test. (Were this not
so, you wouldn't be using a loop.) The obvious way to deal with such a
situation is:
while(foo())
{
bar();
}
for(yadayada(); condition; moreyadayada())
{
}
is clearer still, no?
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.