how to do an infinite loop

P

paolo.brandoli

while(TRUE) /* or for(;;) -- I have no favourite here */

Visual Studio likes better "for(;;)": when using "while(true)" you get
a warning message that sounds like "constant in condition" or
something similar (warning level = 4). Nothing too bad, but I prefer
to have a clean build result without warnings.

Paolo Brandoli
 
M

Miguel Gimenez

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?

When you are programming a microcontroller or DSP you'll need usually an
infinite loop.
 
J

James Kanze

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's not infinite if you leave it. For whatever reasons.
Although practically... this would mean that an infinite loop
is impossible---no hardware is that reliable. And even barring
hardware problems, no program under Unix resists a "kill -9".

I guess the real question is what you mean by "infinite loop".
If I'm talking about a program, I exclude termination conditions
outside the program, like hardware failure or an uncaught
signal, even though these do effetively stop the loop. I also
tend to ignore things like exit() and abort(). I would include
any actual code physically in the loop, however. (In cleanly
written code, of course, a loop won't be terminated by a break.
But spaghetti is still very popular.) And I can't decide with
regards to exceptions.

Of course, most modern server or GUI code will be along the
lines of:

int
main()
{
// initialize everything...
// start threads...
while ( atLeastOneThreadUnjoined ) {
joinWithSomeRandomThread() ;
}
return whatever ;
}

Clicking on the exit button in a GUI will eventually result in
all of the threads terminating, which terminates the loop above.
 
J

James Kanze

Well, it's a debate that resembles the one about the use of
goto statements.

There's no real debate. The issue was resolved against goto
some twenty years ago, or more.
I wouldn't say that they shouldn't be used, no matter what.

Agreed. They're perfectly acceptable in entries to the IOCCC,
for example.
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've been programming now for over thirty years, and I've never
seen code with a goto that couldn't be written better without
it. For that matter, the same holds for using break to leave a
loop. When I'm given such junk to maintain, the first thing I
do is rewrite it to eliminate the goto's and the break's; every
time, it has resulted in cleaner code.
 
K

Kenneth Brody

Martin said:
There being no such language as C/C++,

.... that you are aware of.
it is impossible to do anything
with it.

I'm sure the authors (if any) of the C/C++ language (if it exists)
will differ in their opinion (if they have one).
In either C or C++, a simple statement like
while(1) ;
produces an infinite loop.


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

Nor would any teacher assign "get the size of something without
using sizeof", or "have a function modify the value of an automatic
variable in the calling function, without storing or passing the
address of that variable", or "explain i = i++ + ++i".

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
D

Dan Henry

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?

To prevent my OS-less microcontroller based gizmo's main() from
returning.
 
J

James Watt

Martin said:
There being no such language as C/C++, it is impossible to do anything
with it.

Actually, this came up in a job offer that explicitly listed, and I
quote

...experience in C/C++ [is] an advantage, but not a requirement...

Are you saying there is no such thing?
In either C or C++, a simple statement like
while(1) ;
produces an infinite loop.

My guess would be they want applicants debate advantages/disadvantages
of the various possible control structures (are there any?), but not
being a programmer I can only guess.
Certainly not; no teacher would be braindead enough to assign it.

My experience tells me some teachers are.
 
M

Martin Ambuhl

James said:
Martin said:
There being no such language as C/C++, it is impossible to do anything
with it.

Actually, this came up in a job offer that explicitly listed, and I
quote

...experience in C/C++ [is] an advantage, but not a requirement...

Are you saying there is no such thing?

There is no such thing. There are, however, ignorant toads working in
"human resources".
 
K

Keith Thompson

James said:
Martin said:
There being no such language as C/C++, it is impossible to do anything
with it.

Actually, this came up in a job offer that explicitly listed, and I
quote

...experience in C/C++ [is] an advantage, but not a requirement...

Are you saying there is no such thing?
[...]

Correct, there is no such language as "C/C++". C and C++ are two
distinct (but closely related) languages.

People often use the term "C/C++" as if it were the name of a language.
The charitable interpretation of this is that it really means "C and
C++", or "C or C++", or "C and/or C++".

Or perhaps "C || C++", which means that if C is good enough, you don't
need to go on to C++. :cool:}
 
J

James Kanze

James said:
Actually, this came up in a job offer that explicitly listed, and I
quote
...experience in C/C++ [is] an advantage, but not a requirement...
Are you saying there is no such thing?

Correct, there is no such language as "C/C++". C and C++ are two
distinct (but closely related) languages.

I sometimes wonder if they are really that closely related
anymore:).
People often use the term "C/C++" as if it were the name of a language.
The charitable interpretation of this is that it really means "C and
C++", or "C or C++", or "C and/or C++".

Yes. In a job specification, it probably means "C and/or C++".
I'm still sceptical about it: if it said precisely "C and C++",
it's clear, you need both, but otherwise, what does it mean? As
a secondary requirement, however (as above), it seems pretty
clear that the "and/or" is meant.

In the context of newsgroups, I'd use it when talking about the
common subset of the two languages: things like sequence points,
or the possible representations of the basic types. The answer
to the question "what is the result of `i ++ + ++ i' is the same
in both languages. I think we could call it a C/C++ question.
Or perhaps "C || C++", which means that if C is good enough,
you don't need to go on to C++. :cool:}

More likely "rand() % 2 ? C : C++":).
 
J

James Kuyper

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

Actually, this came up in a job offer that explicitly listed, and I quote

...experience in C/C++ [is] an advantage, but not a requirement...

Are you saying there is no such thing?
[...]

Correct, there is no such language as "C/C++". C and C++ are two
distinct (but closely related) languages.

People often use the term "C/C++" as if it were the name of a language.
The charitable interpretation of this is that it really means "C and
C++", or "C or C++", or "C and/or C++".

Or perhaps "C || C++", which means that if C is good enough, you don't
need to go on to C++. :cool:}

"C or C++" seems the only plausible interpretation to me. The features
of C are not an exact subset of the features of C++, but it comes pretty
close if you throw out a few features of C that no competent C
programmer should be using, such as unprototyped function declarations.
It comes close enough that I would expect any competent C++ programmer
to be able to adapt to a C programming environment with some grumpiness,
but no great difficult. On the other hand, C programmers with no C++
experience or training would have to acquire that training before they
could make use of or understand most of the features that are specific
to C++. Therefore, requiring "C and C++" is pretty close to being redundant.
 
P

pete

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

Actually, this came up in a job offer that explicitly listed, and I
quote

...experience in C/C++ [is] an advantage,
but not a requirement...

Are you saying there is no such thing?
[...]

Correct, there is no such language as "C/C++". C and C++ are two
distinct (but closely related) languages.

People often use the term "C/C++"
as if it were the name of a language.
The charitable interpretation of this is that it really means "C and
C++", or "C or C++", or "C and/or C++".

There's a newsgroup for learning it:
 
R

Richard Heathfield

Dan Henry said:
To prevent my OS-less microcontroller based gizmo's main() from
returning.

Yes, that is the only reasonable use I can think of for an infinite loop -
"we're gonna keep doing this as long as power remains, and we have no way
of knowing how long that will be..."
 
F

Flash Gordon

Richard Heathfield wrote, On 18/11/07 00:13:
Dan Henry said:


Yes, that is the only reasonable use I can think of for an infinite loop -
"we're gonna keep doing this as long as power remains, and we have no way
of knowing how long that will be..."

Or we are going to keep this daemon/TSR/whatever running until it
receives-a-SIGINT/is-given-some-implementation-specific-signal-telling-
it-to-terminate/is-killed-by-the-OS.
 
R

Richard Heathfield

Flash Gordon said:
Richard Heathfield wrote, On 18/11/07 00:13:

Or we are going to keep this daemon/TSR/whatever running until it
receives-a-SIGINT/is-given-some-implementation-specific-signal-telling-
it-to-terminate/is-killed-by-the-OS.

Yes - that is, the program itself neither has nor requires any logic for
stopping, so any stoppage is brought upon it by a change of outside
circumstances.
 
K

Kenneth Brody

James said:
Martin said:
There being no such language as C/C++, it is impossible to do anything
with it.

Actually, this came up in a job offer that explicitly listed, and I
quote

...experience in C/C++ [is] an advantage, but not a requirement...

Are you saying there is no such thing?

Could be. I've seen ads with impossible requirements, which would be
the equivalent of an ad run today which states "5 years experience in
Windows Vista required".
My guess would be they want applicants debate advantages/disadvantages
of the various possible control structures (are there any?), but not
being a programmer I can only guess.

Well, there's a difference (to me, at least) between "write a program
to perform func X forever" and "write an infinite loop".
My experience tells me some teachers are.

I've been fortunate to not have had first-hand experience with such.
But, I've seen enough evidence here and elsewhere to convince me that
they do exist.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top