how to do an infinite loop | | |
can anyone tell me how to do an infinite loop in C/C++, please ?
this is not a homework question . | | | | re: how to do an infinite loop
James Watt wrote: Quote:
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 | | | | re: how to do an infinite loop
James Watt wrote: Quote:
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 | | | | re: how to do an infinite loop
"James Watt" <jwatt@no.spamwrote in message news:nb0v05-i28.wns@no.spam... Quote:
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 ); | | | | re: how to do an infinite loop
James Watt wrote: Quote:
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. Quote:
this is not a homework question .
Certainly not; no teacher would be braindead enough to assign it. | | | | re: how to do an infinite loop
James Watt wrote: Quote:
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? | | | | re: how to do an infinite loop jameskuyper@verizon.net wrote: Quote:
James Watt wrote: Quote:
>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 | | | | re: how to do an infinite loop
Pietro Cerutti wrote: Quote: jameskuyper@verizon.net wrote: Quote:
James Watt wrote: Quote:
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. | | | | re: how to do an infinite loop
On Nov 15, 7:40 pm, jameskuy...@verizon.net wrote: Quote:
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; | | | | re: how to do an infinite loop jameskuyper@verizon.net wrote: Quote:
Pietro Cerutti wrote: Quote:
> jameskuyper@verizon.net wrote: Quote:
>>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 | | | | re: how to do an infinite loop
Jim Langston wrote: Quote:
"James Watt" <jwatt@no.spamwrote in message news:nb0v05-i28.wns@no.spam... Quote:
>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.hin 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) <kst-u@mib.org>
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" | | | | re: how to do an infinite loop
James Watt wrote: Quote:
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 esosman@ieee-dot-org.invalid | | | | re: how to do an infinite loop
Gene wrote: Quote:
On Nov 15, 7:40 pm, jameskuy...@verizon.net wrote: Quote:
>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. Quote:
>
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. | | | | re: how to do an infinite loop
Pietro Cerutti wrote:
.... Quote:
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. | | | | re: how to do an infinite loop
James Kuyper said: Quote:
Gene wrote: Quote:
>On Nov 15, 7:40 pm, jameskuy...@verizon.net wrote: Quote:
>>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.) Quote: Quote: Quote:
>>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();
} Quote: Quote:
>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? Quote:
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 | | | | re: how to do an infinite loop
On Nov 16, 1:22 am, James Watt <jw...@no.spamwrote: Quote:
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. | | | | re: how to do an infinite loop Quote: Quote: Quote:
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. | | | | re: how to do an infinite loop paolo.brandoli@gmail.com said: Quote:
> Quote: Quote:
>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 | | | | re: how to do an infinite loop
Richard Heathfield wrote: Quote:
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();
}
> Quote: Quote:
>>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?
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.
--
Chris "whose languages have movable while/until constructs" Dollin
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England | | | | re: how to do an infinite loop
On Fri, 16 Nov 2007 00:40:40 +0100, Pietro Cerutti
<gahr_AT_gahr_DOT_ch_DO_NOT_SPAMwrote: Quote:
>James Watt wrote: Quote:
>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);
>}
#define HELL_NOT_FROZEN_OVER (1)
..
..
..
while(HELL_NOT_FROZEN_OVER)
{
do_stuff();
}
..
..
..
:-)
--
PGP key ID 0xEB7180EC | | | | re: how to do an infinite loop
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 | | | | re: how to do an infinite loop jameskuyper@verizon.net wrote: Quote:
James Watt wrote: Quote:
>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?
When you are programming a microcontroller or DSP you'll need usually an
infinite loop.
--
Regards
Miguel Giménez | | | | re: how to do an infinite loop
On Nov 16, 1:23 am, Pietro Cerutti <gahr_AT_gahr_DOT_ch_DO_NOT_SPAM>
wrote: Quote:
jameskuy...@verizon.net wrote: Quote:
James Watt wrote: Quote:
can anyone tell me how to do an infinite loop in C/C++, please ?
Quote: Quote:
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?
Quote:
Main loop in event-driven approach?
Quote:
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.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 | | | | re: how to do an infinite loop
On Nov 16, 2:13 am, Pietro Cerutti <gahr_AT_gahr_DOT_ch_DO_NOT_SPAM>
wrote: Quote:
jameskuy...@verizon.net wrote: Quote:
Pietro Cerutti wrote: Quote:
jameskuy...@verizon.net 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?
Quote: Quote: Quote:
It would probably have some break statement to exit the loop when some
particular event happens, but it would still be an infinite loop ;-)
Quote: Quote:
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.
Quote:
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. Quote:
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. Quote:
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.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 | | | | re: how to do an infinite loop
Martin Ambuhl wrote: Quote:
>
James Watt wrote: Quote:
can anyone tell me how to do an infinite loop in C/C++, please ?
>
There being no such language as C/C++,
.... that you are aware of. Quote:
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). Quote:
In either C or C++, a simple statement like
while(1) ;
produces an infinite loop.
> Quote:
this is not a homework question .
>
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:ThisIsASpamTrap@gmail.com> | | | | re: how to do an infinite loop
Martin Ambuhl wrote: Quote:
James Watt wrote: Quote:
>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? Quote:
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. Quote: Quote:
>this is not a homework question .
>
Certainly not; no teacher would be braindead enough to assign it.
My experience tells me some teachers are. | | | | re: how to do an infinite loop
James Watt wrote: Quote:
Martin Ambuhl wrote:
> Quote:
>James Watt wrote: Quote:
>>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?
There is no such thing. There are, however, ignorant toads working in
"human resources". | | | | re: how to do an infinite loop
James Watt wrote: Quote:
Martin Ambuhl wrote:
> Quote:
>James Watt wrote: Quote:
>>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++. 8-)}
--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
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" | | | | re: how to do an infinite loop
On Nov 17, 6:22 am, Keith Thompson <ks...@mib.orgwrote: Quote:
James Watt wrote: Quote:
Actually, this came up in a job offer that explicitly listed, and I
quote
Quote: Quote:
...experience in C/C++ [is] an advantage, but not a requirement...
Quote: Quote:
Are you saying there is no such thing?
Quote:
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:-). Quote:
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. Quote:
Or perhaps "C || C++", which means that if C is good enough,
you don't need to go on to C++. 8-)}
More likely "rand() % 2 ? C : C++":-).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 | | | | re: how to do an infinite loop
Keith Thompson wrote: Quote:
James Watt wrote: Quote:
>Martin Ambuhl wrote:
>> Quote:
>>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++. 8-)}
"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. | | | | re: how to do an infinite loop
Keith Thompson wrote: Quote:
>
James Watt wrote: Quote:
Martin Ambuhl wrote: Quote:
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:
news:alt.comp.lang.learn.c-c++
--
pete | | | | re: how to do an infinite loop
pete said: Quote:
Keith Thompson wrote: <snip> Quote: Quote:
>>
>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:
>
news:alt.comp.lang.learn.c-c++
That's not for C/C++, but for C-C++.
--
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 | | | | re: how to do an infinite loop
Richard Heathfield wrote: Quote:
>
pete said:
> Quote:
Keith Thompson wrote: <snip> Quote: Quote:
>
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:
news:alt.comp.lang.learn.c-c++
>
That's not for C/C++, but for C-C++.
Oh ..., C *-* C++ !!!
*Now* I get it!
--
pete | | | | re: how to do an infinite loop
Gene wrote: Quote:
jameskuy...@verizon.net wrote: Quote:
>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 | | | | re: how to do an infinite loop
On Thu, 15 Nov 2007 16:13:00 -0800 (PST), jameskuyper@verizon.net
wrote: Quote:
>James Watt wrote: Quote:
>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?
To prevent my OS-less microcontroller based gizmo's main() from
returning.
--
Dan Henry | | | | re: how to do an infinite loop
Dan Henry said: Quote:
On Thu, 15 Nov 2007 16:13:00 -0800 (PST), jameskuyper@verizon.net
wrote:
> Quote:
>>James Watt wrote: Quote:
>>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?
>
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..."
--
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 | | | | re: how to do an infinite loop
Richard Heathfield wrote, On 18/11/07 00:13: Quote:
Dan Henry said:
> Quote:
>On Thu, 15 Nov 2007 16:13:00 -0800 (PST), jameskuyper@verizon.net
>wrote:
>> Quote:
>>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?
>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..."
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.
--
Flash Gordon | | | | re: how to do an infinite loop
James Watt wrote: Quote:
>
Martin Ambuhl wrote:
> Quote:
James Watt wrote: Quote:
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?
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". Quote: Quote:
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.
Well, there's a difference (to me, at least) between "write a program
to perform func X forever" and "write an infinite loop". Quote: Quote: Quote:
this is not a homework question .
Certainly not; no teacher would be braindead enough to assign it.
>
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:ThisIsASpamTrap@gmail.com> | | | | re: how to do an infinite loop
Miguel Gimenez wrote: Quote:
When you are programming a microcontroller or DSP you'll need usually an
infinite loop.
Aren't busy loops bad for energy consumption? | | | | re: how to do an infinite loop
On Mon, 19 Nov 2007 22:04:04 +0200, Juha Nieminen wrote: Quote:
Miguel Gimenez wrote: Quote:
>When you are programming a microcontroller or DSP you'll need usually
>an infinite loop.
>
Aren't busy loops bad for energy consumption?
Yep. I haven't done it for a while (8051 micro), but at the very least
you should use timers + interrupt functions.
--
Sohail Somani http://uint32t.blogspot.com | | | | re: how to do an infinite loop
Juha Nieminen wrote, On 19/11/07 20:04: Quote:
Miguel Gimenez wrote: Quote:
>When you are programming a microcontroller or DSP you'll need usually an
>infinite loop.
>
Aren't busy loops bad for energy consumption?
An infinite busy loop would be, but I don't think that is what Miguel
was suggesting. I've done an infinite loop of the form:
for (;;) {
suspend_processor();
do_something();
}
On another project:
for (;;) suspend_processor();
In each case, suspend_processor() stops the processor from executing
code until it receives an interrupt.
--
Flash Gordon | | | | re: how to do an infinite loop
Flash Gordon wrote: Quote:
An infinite busy loop would be, but I don't think that is what Miguel
was suggesting. I've done an infinite loop of the form:
>
for (;;) {
suspend_processor();
do_something();
}
>
On another project:
>
for (;;) suspend_processor();
>
In each case, suspend_processor() stops the processor from executing
code until it receives an interrupt.
It's a kind of processor which simply continues execution from where
it left it when it receives an interrupt instead of jumping to an
interrupt handler defined in some interrupt table? | | | | re: how to do an infinite loop
Juha Nieminen wrote, On 19/11/07 22:59: Quote:
Flash Gordon wrote: Quote:
>An infinite busy loop would be, but I don't think that is what Miguel
>was suggesting. I've done an infinite loop of the form:
>>
>for (;;) {
> suspend_processor();
> do_something();
>}
>>
>On another project:
>>
>for (;;) suspend_processor();
>>
>In each case, suspend_processor() stops the processor from executing
>code until it receives an interrupt.
>
It's a kind of processor which simply continues execution from where
it left it when it receives an interrupt instead of jumping to an
interrupt handler defined in some interrupt table?
No. It does the interrupt processing then returns and executes
do_something (in the first case). Also note that the second example does
*nothing* in the main loop apart from suspend the processor.
The most common processor I've done this kind of loop on is the Z80, but
I've also used it on the TMS320C25 and maybe other processors.
--
Flash Gordon | | | | re: how to do an infinite loop
On Mon, 19 Nov 2007 23:09:19 +0000, Flash Gordon wrote: Quote:
>Juha Nieminen wrote, On 19/11/07 22:59: Quote:
>Flash Gordon wrote: Quote:
>>On another project:
>>>
>>for (;;) suspend_processor();
>>>
>>In each case, suspend_processor() stops the processor from executing
>>code until it receives an interrupt.
>
It does the interrupt processing then returns and executes
do_something (in the first case). Also note that the second
example does *nothing* in the main loop apart from suspend
the processor.
The most common processor I've done this kind of loop on is the
Z80, but I've also used it on the TMS320C25 and maybe other processors.
As an interesting part of trivia, the main loop of Super Mario Bros. on
the 8-bit Nintendo is exactly of this kind. The main loop does literally
nothing else but wait for the next interrupt. (Except that the NES does
not have a suspend/wait opcode, so it was a for(;;); loop instead.)
Most other NES games defaulted to the busyloop once they had
done processing the game logic of that particular display frame;
some constantly updating the random number generator seed in that loop.
Follow-ups set.
--
Joel Yliluoma - http://iki.fi/bisqwit/ | | | | re: how to do an infinite loop
"Richard Heathfield" <rjh@see.sig.invalida écrit dans le message de news: evmdnf5ncJaYu6DanZ2dnUVZ8umdnZ2d@bt.com... Quote:
James Kuyper said:
> Quote:
>Gene wrote: Quote:
>>On Nov 15, 7:40 pm, jameskuy...@verizon.net 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.)
> Quote: Quote:
>>>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();
}
> Quote: Quote:
>>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?
Certainly not!
Even barring the fact that it implements different semantics ;-)
What is not clear about the for (;;) loop is the *hiding* if the exit test.
If you make it stand out as it should, separating it from yadiyada with
blank lines and breaking the break on a separate line, it becomes obvious.
After seeing for (;;), he reader knows what to look for: either a break or a
return will exit the loop. Quote: Quote:
>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.
That is your opinion, not an obsolute truth.
--
Chqrlie. | | | | re: how to do an infinite loop
"CBFalconer" <cbfalconer@yahoo.coma écrit dans le message de news: 473D1B21.34A4C5FE@yahoo.com... Quote:
Gene wrote: Quote:
>jameskuy...@verizon.net wrote: Quote:
>>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.
<exit conditionis an arbitrary expression, your proposed alternative
requires at least an extra local variable definition. It may also produce
extra code for the redundant test. IME neebies tend to produce more bugs
when using do/while loops... but that's another can of worms.
--
Chqrlie. | | | | re: how to do an infinite loop
"Richard Heathfield" <rjh@see.sig.invalida écrit dans le message de
news: -fadnc6ZJK-_uqLaRVnyhAA@bt.com... Quote:
pete said:
> Quote:
>Keith Thompson wrote: <snip> Quote: Quote:
>>>
>>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:
>>
> news:alt.comp.lang.learn.c-c++
>
That's not for C/C++, but for C-C++.
C/C++ and C-C++ both invoke undefined behaviour.
--
Chqrlie. | | | | re: how to do an infinite loop
"Keith Thompson" <kst-u@mib.orga écrit dans le message de news:
fhltq5$4d4$1@aioe.org... Quote:
James Watt wrote: Quote:
>Martin Ambuhl wrote:
>> Quote:
>>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++. 8-)}
More precisely ;-)
- if C has a non zero value, the Standard requires that C++ not be
evaluated.
- if C has a zero value, so does C++, and in the end C will have non zero
value.
--
Chqrlie. | | | | re: how to do an infinite loop
Charlie Gordon wrote: Quote:
"Richard Heathfield" <rjh@see.sig.invalida écrit dans le message de
news: -fadnc6ZJK-_uqLaRVnyhAA@bt.com... Quote:
>pete said:
>> Quote:
>>Keith Thompson wrote:
><snip> Quote:
>>>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:
>>>
>> news:alt.comp.lang.learn.c-c++
>That's not for C/C++, but for C-C++.
>
C/C++ and C-C++ both invoke undefined behaviour.
Not necessarily:
#define C x,y |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|