# how to do an infinite loop

Discussion in 'C++' started by James Watt, Nov 15, 2007.

1. ### James WattGuest

can anyone tell me how to do an infinite loop in C/C++, please ?

this is not a homework question .

James Watt, Nov 15, 2007

2. ### Eric LiljaGuest

James Watt wrote:
> can anyone tell me how to do an infinite loop in C/C++, please ?
>
> this is not a homework question .
>
>

I think you should acquire some basic search engine skills...

Eric Lilja, Nov 15, 2007

3. ### Pietro CeruttiGuest

James Watt wrote:
> can anyone tell me how to do an infinite loop in C/C++, please ?
>
> this is not a homework question .
>
>

int main(void) {
while(1);
return (0);
}

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp

Pietro Cerutti, Nov 15, 2007
4. ### Default UserGuest

James Watt wrote:

> can anyone tell me how to do an infinite loop in C/C++, please ?
>
> this is not a homework question .

Do you understand what an infinite loop is?

Do you understand the various loop constructs?

If not, get a book and read. If you do, then apply the two and you'll
see how to do so.

Or you could go to Google and type in: infinite loop c

Brian

Default User, Nov 15, 2007
5. ### Jim LangstonGuest

"James Watt" <> wrote in message news:...
> can anyone tell me how to do an infinite loop in C/C++, please ?
>
> this is not a homework question .

for ( ;; )
/**/;

while (true)
/**/;

do
{ /**/ }
while ( true );

Jim Langston, Nov 15, 2007
6. ### Martin AmbuhlGuest

James Watt wrote:
> can anyone tell me how to do an infinite loop in C/C++, please ?

There being no such language as C/C++, it is impossible to do anything
with it.
In either C or C++, a simple statement like
while(1) ;
produces an infinite loop.

> this is not a homework question .

Certainly not; no teacher would be braindead enough to assign it.

Martin Ambuhl, Nov 16, 2007
7. ### Guest

James Watt wrote:
> 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?

, Nov 16, 2007
8. ### Pietro CeruttiGuest

wrote:
> James Watt wrote:
>> 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?

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

PGP Public Key:
http://gahr.ch/pgp

Pietro Cerutti, Nov 16, 2007
9. ### Guest

Pietro Cerutti wrote:
> wrote:
> > James Watt wrote:
> >> 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?

>
> 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 ;-)

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.

, Nov 16, 2007
10. ### GeneGuest

On Nov 15, 7:40 pm, wrote:
> 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.

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
or trailer. To wit, Ada, which is designed expressly code for clarity
and simplicity, has the construct

loop
... -- yada yada
exit when <condition> ;
... -- more yada yada
end loop;

Gene, Nov 16, 2007
11. ### Pietro CeruttiGuest

wrote:
> Pietro Cerutti wrote:
>> wrote:
>>> James Watt wrote:
>>>> 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?

>> 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 ;-)

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

Well, it's a debate that resembles the one about the use of goto
statements. I wouldn't say that they shouldn't be used, no matter what.

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.

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp

Pietro Cerutti, Nov 16, 2007
12. ### Keith ThompsonGuest

Jim Langston wrote:
> "James Watt" <> wrote in message news:...
>> can anyone tell me how to do an infinite loop in C/C++, please ?
>>
>> this is not a homework question .

>
> for ( ;; )
> /**/;
>
> while (true)
> /**/;
>
> do
> { /**/ }
> while ( true );

The question was (unwisely) cross-posted to comp.lang.c and
comp.lang.c++. I suspect you replied in comp.lang.c++. In C, "true" is
not a keyword or a predefined identifier (though it is a macro in
<stdbool.h> in C99).

To the original poster: there is no language called "C/C++". They are
two different (but closely related) languages.

--
Keith Thompson (The_Other_Keith) <>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Nov 16, 2007
13. ### Eric SosmanGuest

James Watt wrote:
> can anyone tell me how to do an infinite loop in C/C++, please ?
>
> this is not a homework question .

In one sense it's trivial: Both C and C++ have several
constructs which express a loop that repeats without end.
Perhaps the simplest is `label: goto label;', although the
anti-goto zealots won't like it.

In a practical sense it's impossible: Both C and C++
execute on real machines, and real machines have distressingly
finite lifetimes. If the Universe expands indefinitely and
just dwindles away into heat-death, few machines will find
enough energy deltas to operate with. And if the Universe
ends in a Big Crunch, Who will read the core dump?

--
Eric Sosman
lid

Eric Sosman, Nov 16, 2007
14. ### CBFalconerGuest

Gene wrote:
> wrote:
>> 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.

>
> 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
> }

How about:

do {
/* yada yada */
if (!exit_condition) {
/* more yada yada */
}
} while (!exit_condition);

fups set for c.l.c. Cross posting to c.l.c++ is unwise.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

CBFalconer, Nov 16, 2007
15. ### James KuyperGuest

Gene wrote:
> On Nov 15, 7:40 pm, wrote:
>> 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.

>
> I disagree with this if you mean that a loop should always exit at the
> loop head or tail.

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.

>
> 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

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.

James Kuyper, Nov 16, 2007
16. ### James KuyperGuest

Pietro Cerutti wrote:
....
> 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.

My objection is based precisely on the lack of clarity that occurs when
you hide the normal exit from a loop by placing it in any location other
than the loop construct itself.

James Kuyper, Nov 16, 2007
17. ### Richard HeathfieldGuest

James Kuyper said:

> Gene wrote:
>> On Nov 15, 7:40 pm, wrote:
>>> 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.

(Hear hear.)

>>> The main way of leaving
>>> a loop should always involve the constructs that make it a loop.

>>
>> I disagree with this if you mean that a loop should always exit at the
>> loop head or tail.

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

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 (; {
>> ... // 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

for(yadayada(); condition; moreyadayada())
{
}

is clearer still, no?

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

Right.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Richard Heathfield, Nov 16, 2007
18. ### Guest

On Nov 16, 1:22 am, James Watt <> wrote:
> can anyone tell me how to do an infinite loop in C/C++, please ?
>
> this is not a homework question .

It is indeed not a homework question, it's a troll question.

, Nov 16, 2007
19. ### Guest

> >> 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

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

, Nov 16, 2007
20. ### Richard HeathfieldGuest

said:

>
>> >> 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

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

Oops, good point.

Then I'd do it like this:

do
{
if(condition = yadayada())
{
moreyadayada();
}
} while(condition);

Simple logic, obvious control flow, no wild jumps, everything nicely
encapsulated in functions.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Richard Heathfield, Nov 16, 2007

### Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.