how to do an infinite loop

J

James Watt

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

this is not a homework question .
 
P

Pietro Cerutti

James said:
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);
}
 
D

Default User

James said:
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
 
J

Jim Langston

James Watt said:
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 );
 
M

Martin Ambuhl

James said:
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.
 
J

jameskuyper

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

Pietro Cerutti

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

jameskuyper

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

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

Gene

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

Pietro Cerutti

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

Keith Thompson

Jim said:
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.
 
E

Eric Sosman

James said:
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?
 
C

CBFalconer

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
}

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

James Kuyper

Gene said:
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.
 
J

James Kuyper

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

Richard Heathfield

James Kuyper said:

(Hear hear.)
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(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.
 
V

vippstar

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

paolo.brandoli

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

Richard Heathfield

(e-mail address removed) said:
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.
 
C

Chris Dollin

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?

That depends on `condition` (or the insides of `foo`). Sometimes the mere
act of abstracting the prelude-and-test of the loop introduces complexity
(eg when the prelude updates local variables, so the extracted function
takes multiple pointer arguments); or when the prelude declares a variable
that is used in the body, one /can't/ simply do an extraction.

Myself, I take the view that

while(TRUE) /* or for(;;) -- I have no favourite here */
{
... prelude ...
if (doneNow()) break;
... postlude aka body ...
}

is /visibly/ not an "infinite" loop. (As you know, Bob [1], I would claim
that if the break wasn't visible, the loop body is anyways too big; fix
that /first/.)

[1] The traditional name, not any Bob that may be here.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top