how to do an infinite loop

N

Nick Keighley

rami said:
please everybody ,can anyone tell me how to do an infinite loop in C

well the obvious way that applies to pretty well any programming
language
(I'm sure some will take it as a challange to give a counter example
(*)
is to use a while statement.

while (expression)
statement;

This executes "statement" while "expression" is true. So all you have
to
do is choose an expression that is always true

while (1 == 1)
statement;

as C treats any non-zero value as true then more succinctly

while (1)
statement;

The idiomatic (C-like) way to do it is

for (;;)
statement;

read your textbook to understand why this works. Even if you
don't like this you must recognise it as you will see it in other
people's code


(*) I bid Algol-60

--
Nick Keighley

"ALGOL 60 was a language so far ahead of its time that it
was not only an improvement on its predecessors but also
on nearly all its successors".
--C.A.R. Hoare
 
R

Richard Heathfield

rami said:
please everybody ,can anyone tell me how to do an infinite loop in C

I have written a program for you, that contains an infinite loop, but it
wouldn't be right to show you the code until its test run is complete.
 
P

pemo

rami said:
please everybody ,can anyone tell me how to do an infinite loop in C

I have seen the

while(1)

construct produce warnings with some compilers - 'constant in conditional
expression' or some such thing.

The proper C way is I believe to use

for( ; ; )

that should *not* produce any complaints.
 
V

Vladimir S. Oka

pemo said:
I have seen the

while(1)

construct produce warnings with some compilers - 'constant in conditional
expression' or some such thing.

The proper C way is I believe to use

for( ; ; )

that should *not* produce any complaints.

IMHO, this is a matter of taste (I prefer `while (1)`). They're both
correct.

I see no reason for either to produce diagnostic messages, although I
could think of a weak rationale for the one you were seeing.
 
J

John Bode

rami said:
please everybody ,can anyone tell me how to do an infinite loop in C

Pick any of the following:

1. for(;;) {...}
2. while(1) {...}
3. do {...} while(1);

I personally use 1.
 
P

pemo

Vladimir said:
IMHO, this is a matter of taste (I prefer `while (1)`). They're both
correct.

I see no reason for either to produce diagnostic messages, although I
could think of a weak rationale for the one you were seeing.

I must admit that I haven't seen this warning for quite a while now, but, I
can see why a compiler might see some merit in it, e.g., while(x = 1) might
produce the same output - and, of course, be rather useful.

For those that don't read post too fully - yes, I know that x = 1 should
normally be x == 1 :)
 
J

John F

pemo said:
I must admit that I haven't seen this warning for quite a while now, but,
I can see why a compiler might see some merit in it, e.g., while(x = 1)
might produce the same output - and, of course, be rather useful.

Yes, that's the rationale for the warning (which can be quite annoying when
porting code from another compiler. But it is better to be warned in a fwe
cases than falling into an endless loop (evenmore on embedded contol
devices, which is OT here, but a huge field where C is used in various
dialects)). The message usually gives something like "conditional expression
in ... is always true".
or "... false", which is annoying when someone is using do{/*code goes
here*/}while(0) to encapsulate a function like macro.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

John said:
Pick any of the following:

1. for(;;) {...}
2. while(1) {...}
3. do {...} while(1);

add to that list
4. label: ... goto label;


- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEBaTAagVFX4UWr64RAiaiAKDbFvIGUrKGQxe7r4M9eOK8/V4zjgCfc+eb
huRZZhoqn/pUhwWw/IlHSo0=
=mMqY
-----END PGP SIGNATURE-----
 
A

August Karlstrom

Nick said:
well the obvious way that applies to pretty well any programming
language
(I'm sure some will take it as a challange to give a counter example
(*)
is to use a while statement.

while (expression)
statement;

Well, some languages, as Oberon, have a special loop statement for
infinite loops and exit-in-the-middle loops:

LOOP
...
IF someCondition THEN EXIT END;
...
END

That way you can clearly express your intention with the loop. Moreover,
in Oberon local exits (break) are only allowed in LOOP statements.

August
 
C

CBFalconer

John said:
Pick any of the following:

1. for(;;) {...}
2. while(1) {...}
3. do {...} while(1);

I personally use 1.

Nah. Real programmers use:

meaningfullabel:
....
goto meaningfullabel;

which avoids all that flow of control indentation nonsense. It
also makes that infinite loop accessible from anywhere else in the
function, and encourages creative naming of meaningfullabel.

It is a good idea to present a message within the infinite loop
that says something like:

"Please wait. Calibrating, do not disturb or data will be lost".

If the message terminates with '\r' it may avoid scrolling the
screen or otherwise disturbing the peace of the observer. An
fflush may be needed. Avoid any '\n's.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
N

Nick Keighley

August said:
Well, some languages, as Oberon, have a special loop statement for
infinite loops and exit-in-the-middle loops:

LOOP
...
IF someCondition THEN EXIT END;
...
END

That way you can clearly express your intention with the loop. Moreover,
in Oberon local exits (break) are only allowed in LOOP statements.

I'm not sure I'd describe that as the "obvious" way to program an
infinite
loop. But then I've never programmed in Oberon.
 
P

Pedro Graca

Lew said:
add to that list
4. label: ... goto label;

Do not add to the list (unless you're considering an "infinite" computer)
void f(void) { ...; f(); }
 
V

Vladimir S. Oka

Nick said:
I'm not sure I'd describe that as the "obvious" way to program an
infinite
loop. But then I've never programmed in Oberon.

It's only obvious if the whole loop fits onto one screen. That's also
why I don't consider C construct (to pull this back on-topic)

do
{
/* whatever */;
} while (1);

a very good way to express infinite loops.

If you know in advance it's an infinite loop -- spell it out
immediatelly.
 
C

Charles Krug

please everybody ,can anyone tell me how to do an infinite loop in C

Our "shop standard" follows Stroustroup and uses for(;;), pronounced
"forever."

Several shops ago, I used while(17) until my boss complained he couldn't
understand it.
 
C

CBFalconer

Charles said:
Our "shop standard" follows Stroustroup and uses for(;;),
pronounced "forever."

Several shops ago, I used while(17) until my boss complained he
couldn't understand it.

#define until while
#define the_cows_come_home 17

do {
... stuff ...
} until (the_cows_come_home);

some alternatives (mix and match)

#define hell_freezes_over (1 != 0)
#define no_dog_has_fleas (EOF < 0)

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
I

Ico

Richard Heathfield said:
rami said:


I have written a program for you, that contains an infinite loop, but it
wouldn't be right to show you the code until its test run is complete.

Done yet ?
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top