Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > infinite loop

Reply
Thread Tools

infinite loop

 
 
kenny
Guest
Posts: n/a
 
      01-24-2009

i want to write infinite loop

1. can do in c language ?

2. how is useful ?


 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      01-24-2009
kenny wrote:

>
> i want to write infinite loop
>
> 1. can do in c language ?


That's a philosophical question. You can write one that has no
algorithmic exit to the loop. There's little chance that one would be
truly infinite.

> 2. how is useful ?


I don't know, you're the one that wants one. Why do you think it would
be useful?




Brian
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-24-2009
kenny wrote:
> i want to write infinite loop
>
> 1. can do in c language ?


while (1) {} /* one way */

do {} while(1); /* another way */

for (; {} /* a third way */

foo: goto foo; /* a fourth way */


> 2. how is useful ?


To keep children amused.
 
Reply With Quote
 
Wolfgang Draxinger
Guest
Posts: n/a
 
      01-24-2009
kenny wrote:

> i want to write infinite loop


for(;{}

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-24-2009
kenny wrote:
> i want to write infinite loop
>
> 1. can do in c language ?


You can write it in the C language, but no actual C
implementation will execute it as written. (Ir follows
that all actual C implementations are non-conforming, or
at least that there is no conformance test that could
yield an unequivocal "Yes.")

> 2. how is useful ?


Only if satisfying your own stated desire is "useful."

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      01-24-2009
kenny wrote:
> i want to write infinite loop

....
> 2. how is useful ?


It's used in many embedded computing applications in which there is one
program that keeps running the same logic as long as the power is on.

It is used in general run-to-termination programs that terminate the
loop from within, typically as a function return or break when some
condition is satisfied:

while (1) {
do_something();
if (error) return -1
do_something_else();
if (different_error) return -2;
if (normal_exit) return 0;
get_ready_to_do_more();
}

--
Thad
 
Reply With Quote
 
Andrew Smallshaw
Guest
Posts: n/a
 
      01-27-2009
On 2009-01-24, kenny <(E-Mail Removed)> wrote:
>
> i want to write infinite loop
>
> 1. can do in c language ?


There are many ways. for (; {} is the canonical one, although
I've seen while (1) {} and even a label and goto used to implement
it. I strongly urge for (; since it is immediately recognisable
as an infinite loop. With a while (1) in a work in progress it
isn't always clear whether an infinite loop is desired or the 1 is
simply a placeholder for a condition that has yet to be determined.

> 2. how is useful ?


Any time you want to execute a given section of code an indefinite
number of times. It should be understood that no infinite loop is
really infinite: it is something of a misnomer. Instead an "infinite"
loop repeats until either the function or the program terminates.

Another reason that is sometimes overlooked is to take the loop
control inside the loop body. This uses the same for (; structure
but of course is even less of an infinite loop than the first case.
Perhaps the most common case for this is when you need to initialise
a number of data structures on a per-iteration basis _before_ you
can test your condition. Do all your initialisation, test the loop
condition and break out if it fails.

The alternative ends up with a loop body performing the second half
of one iteration followed by the first half of the next. This is
messy and difficult to understand, which only gets worse when you
remember you need to initialise for the first iteration before the
loop and clean up after the last one after the loop. Avoid code
that does that at all costs.

--
Andrew Smallshaw
(E-Mail Removed)
 
Reply With Quote
 
Nate Eldredge
Guest
Posts: n/a
 
      01-27-2009
Golden California Girls <(E-Mail Removed)> writes:

> Andrew Smallshaw wrote:
>> On 2009-01-24, kenny <(E-Mail Removed)> wrote:
>>> i want to write infinite loop
>>>
>>> 1. can do in c language ?

>>
>> There are many ways. for (; {} is the canonical one, although
>> I've seen while (1) {} and even a label and goto used to implement
>> it. I strongly urge for (; since it is immediately recognisable
>> as an infinite loop. With a while (1) in a work in progress it
>> isn't always clear whether an infinite loop is desired or the 1 is
>> simply a placeholder for a condition that has yet to be determined.

>
> Rather than while (1) {} which I agree can cause questions for later maintainers
> of the code I have seen while (hell_freezes_over) {} to make it obvious the
> author intends it to be infinite. I even seen the do {} while
> (hell_freezes_over) form.


That's pretty misleading; I would expect the loop to terminate
immediately, since hell is not yet freezing over. Maybe you got it from
a language that has repeat/until. I'd also probably make the condition
a macro and put it in caps.

Here's some other ideas:

while (A_THING_OF_BEAUTY_IS_A_JOY) { }
while (THERE_ARE_STARS_ABOVE_YOU) { }
while (DEATH_AND_TAXES_EXIST) { }
while (!ENGLAND_IS_A_SLAVE) { }
while (CRICKET_MATCH_CONTINUES) { }


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-27-2009
Golden California Girls <(E-Mail Removed)> writes:
> Andrew Smallshaw wrote:
>> On 2009-01-24, kenny <(E-Mail Removed)> wrote:
>>> i want to write infinite loop
>>>
>>> 1. can do in c language ?

>>
>> There are many ways. for (; {} is the canonical one, although
>> I've seen while (1) {} and even a label and goto used to implement
>> it. I strongly urge for (; since it is immediately recognisable
>> as an infinite loop. With a while (1) in a work in progress it
>> isn't always clear whether an infinite loop is desired or the 1 is
>> simply a placeholder for a condition that has yet to be determined.

>
> Rather than while (1) {} which I agree can cause questions for later
> maintainers of the code I have seen while (hell_freezes_over) {} to
> make it obvious the author intends it to be infinite. I even seen
> the do {} while (hell_freezes_over) form.


Yeah, I did that kind of thing myself many years ago. Specifically, I
used something like:

#define EVER ;;
...
for (EVER) {
...
}

It's cute -- and that's not a good thing. The English meaning of
"for (EVER)" is clear enough, but this isn't English, it's C.
The reader must either assume that you got the macro definition
right, or waste time tracking down the macro definition, mentally
expand it, and *then* understand that "for (;" is an infinite loop.

If you want your code to be understood, just write "for (;" or
"while (1)" and be done with it.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Mark Wooding
Guest
Posts: n/a
 
      01-27-2009
Nate Eldredge <(E-Mail Removed)> writes:

> while (A_THING_OF_BEAUTY_IS_A_JOY) { }

[etc.]

while (DIAMONDS) { /* ... */ }

-- [mdw]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
infinite loop unexpectly dies Vedran Vukotic Perl 0 03-02-2006 07:43 AM
loop thru a STL list causes an infinite loop Allerdyce.John@gmail.com C++ 5 01-31-2006 03:21 PM
Infinite loop when using Server.Transfer Alexander Bosch ASP .Net 11 11-10-2004 08:53 AM
Infinite loop when using Server.Transfer Alexander Bosch ASP .Net 1 11-01-2004 02:55 AM



Advertisments