infinite loop

Discussion in 'C Programming' started by kenny, Jan 24, 2009.

  1. kenny

    kenny Guest

    i want to write infinite loop

    1. can do in c language ?

    2. how is useful ?
     
    kenny, Jan 24, 2009
    #1
    1. Advertising

  2. kenny

    Default User Guest

    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
     
    Default User, Jan 24, 2009
    #2
    1. Advertising

  3. 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.
     
    Martin Ambuhl, Jan 24, 2009
    #3
  4. kenny wrote:

    > i want to write infinite loop


    for(;;){}
     
    Wolfgang Draxinger, Jan 24, 2009
    #4
  5. kenny

    Eric Sosman Guest

    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
    lid
     
    Eric Sosman, Jan 24, 2009
    #5
  6. kenny

    Thad Smith Guest

    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
     
    Thad Smith, Jan 24, 2009
    #6
  7. On 2009-01-24, kenny <> 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
     
    Andrew Smallshaw, Jan 27, 2009
    #7
  8. Golden California Girls <> writes:

    > Andrew Smallshaw wrote:
    >> On 2009-01-24, kenny <> 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) { }

    ;-)
     
    Nate Eldredge, Jan 27, 2009
    #8
  9. Golden California Girls <> writes:
    > Andrew Smallshaw wrote:
    >> On 2009-01-24, kenny <> 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) <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"
     
    Keith Thompson, Jan 27, 2009
    #9
  10. kenny

    Mark Wooding Guest

    Nate Eldredge <> writes:

    > while (A_THING_OF_BEAUTY_IS_A_JOY) { }

    [etc.]

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

    -- [mdw]
     
    Mark Wooding, Jan 27, 2009
    #10
  11. On Jan 24, 5:59 am, Martin Ambuhl <> wrote:
    > 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 */
    >


    But, why does C provide some many
    ways of infinite loop ?

    Which one of the above methods consume
    less cycles ?

    Thx in advans,
    Karthik Balaguru
     
    karthikbalaguru, Jan 28, 2009
    #11
  12. On Jan 28, 10:54 am, Richard Heathfield <> wrote:
    > karthikbalaguru said:
    >
    >
    >
    > > On Jan 24, 5:59 am, Martin Ambuhl <> wrote:
    > >> 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 */

    >
    > > But, why does C provide some many
    > > ways of infinite loop ?

    >
    > Imagine that you are a craftsman in a workshop that contains many
    > tools. Someone comes in and asks you how many different ways you
    > can think of to insert a woodscrew into an offcut.
    >
    > First, you demonstrate how to do this with a manual screwdriver.
    >
    > Then you do the same thing, this time with an electric screwdriver.
    >
    > Then you show that it can be done with a hammer, too, albeit not
    > quite so neatly.
    >
    > Fourthly, you cut a small recess in the wood with a hammer and
    > chisel and place a screw into the recess.
    >
    > Fifthly, you drill a hole wider than the screw and drop the screw
    > into the hole. (Again, you can do this both manually and
    > electrically.)
    >
    > You may be able to think of some other ways to do this, too.
    >
    > But, says your visitor, WHY does carpentry provide so many ways to
    > do this?
    >
    > > Which one of the above methods consume
    > > less cycles ?

    >
    > That's the wrong question - partly because it makes little if any
    > difference, and partly because the answer would vary from machine
    > to machine,


    Considering a x86 machine, which one will be faster (Less Cycles) ?
    Any ideas ?

    Thx in advans,
    Karthik Balaguru
     
    karthikbalaguru, Jan 28, 2009
    #12
  13. On Jan 28, 10:54 am, Richard Heathfield <> wrote:
    > karthikbalaguru said:
    >
    >
    >
    > > On Jan 24, 5:59 am, Martin Ambuhl <> wrote:
    > >> 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 */

    >
    > > But, why does C provide some many
    > > ways of infinite loop ?

    >
    > Imagine that you are a craftsman in a workshop that contains many
    > tools. Someone comes in and asks you how many different ways you
    > can think of to insert a woodscrew into an offcut.
    >
    > First, you demonstrate how to do this with a manual screwdriver.
    >
    > Then you do the same thing, this time with an electric screwdriver.
    >
    > Then you show that it can be done with a hammer, too, albeit not
    > quite so neatly.
    >
    > Fourthly, you cut a small recess in the wood with a hammer and
    > chisel and place a screw into the recess.
    >
    > Fifthly, you drill a hole wider than the screw and drop the screw
    > into the hole. (Again, you can do this both manually and
    > electrically.)
    >
    > You may be able to think of some other ways to do this, too.
    >
    > But, says your visitor, WHY does carpentry provide so many ways to
    > do this?
    >
    > > Which one of the above methods consume
    > > less cycles ?

    >
    > That's the wrong question - partly because it makes little if any
    > difference, and partly because the answer would vary from machine
    > to machine,


    Considering a x86 machine, which one will be faster (Less Cycles) ?
    and Which one will consume more memory ?
    Any ideas ?

    Thx in advans,
    Karthik Balaguru
     
    karthikbalaguru, Jan 28, 2009
    #13
  14. On Jan 28, 12:54 pm, Richard Heathfield <> wrote:
    > karthikbalaguru said:
    >
    >
    >
    > > On Jan 28, 10:54 am, Richard Heathfield <>
    > > wrote:
    > >> karthikbalaguru said:

    >
    > <snip>
    >
    > >> > Which one of the above methods [of programming an infinite
    > >> > loop] consume less cycles ?

    >
    > >> That's the wrong question - partly because it makes little if any
    > >> difference, and partly because the answer would vary from machine
    > >> to machine,

    >
    > > Considering a x86 machine, which one will be faster (Less Cycles)
    > > ? Any ideas ?

    >
    > It's still the wrong question.
    >
    > Consider this: either the loop body will be doing a lot of work (in
    > which case the tiny cost of looping, *however* you do it, will be
    > insignificant), or the loop body will not be doing a lot of work
    > (in which case the overhead doesn't matter anyway, because there's
    > plenty of capacity).
    >


    Thx for that info !!
    But, i am really interested to know considering
    the loop body does not do anything. (It is just an infinite loop
    without body.)

    I am intested to know this because various forms of
    programming an infinite loop is supported .

    Also, are there any specific scenarios in which
    those methods should be used ?

    Any ideas ?

    Thx in advans,
    Karthik Balaguru
     
    karthikbalaguru, Jan 28, 2009
    #14
  15. karthikbalaguru <> writes:
    > On Jan 24, 5:59 am, Martin Ambuhl <> wrote:
    >> 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 */
    >>

    >
    > But, why does C provide some many
    > ways of infinite loop ?


    C doesn't provide any construct that's specifically for infinite
    loops. It provides a number of distinct looping constructs, each with
    its own uses. (You could get by with just one, but the different
    forms are more convenient in different circumstances.) And, not
    surprisingly, each of the forms of loop can be specialized to form an
    infinite loop.

    > Which one of the above methods consume
    > less cycles ?


    As Richard explained at some length, you're asking the wrong question.
    But to answer your question anyway, very probably they all consume
    exactly the same number of cycles. A decent compiler will probably
    generate exactly the same code for all of them.

    --
    Keith Thompson (The_Other_Keith) <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"
     
    Keith Thompson, Jan 28, 2009
    #15
  16. kenny

    Guest

    On 28 Jan, 06:38, karthikbalaguru <> wrote:
    > On Jan 28, 10:54 am, Richard Heathfield <> wrote:
    > > karthikbalaguru said:
    > > > On Jan 24, 5:59 am, Martin Ambuhl <> wrote:
    > > >> 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 */

    >
    > > > But, why does C provide some many
    > > > ways of infinite loop ?

    >
    > > Imagine that you are a craftsman in a workshop that contains many
    > > tools. Someone comes in and asks you how many different ways you
    > > can think of to insert a woodscrew into an offcut.

    >
    > > First, you demonstrate how to do this with a manual screwdriver.

    >
    > > Then you do the same thing, this time with an electric screwdriver.

    >
    > > Then you show that it can be done with a hammer, too, albeit not
    > > quite so neatly.

    >
    > > Fourthly, you cut a small recess in the wood with a hammer and
    > > chisel and place a screw into the recess.

    >
    > > Fifthly, you drill a hole wider than the screw and drop the screw
    > > into the hole. (Again, you can do this both manually and
    > > electrically.)

    >
    > > You may be able to think of some other ways to do this, too.

    >
    > > But, says your visitor, WHY does carpentry provide so many ways to
    > > do this?

    >
    > > > Which one of the above methods consume
    > > > less cycles ?

    >
    > > That's the wrong question - partly because it makes little if any
    > > difference, and partly because the answer would vary from machine
    > > to machine,

    >
    > Considering a x86 machine, which one will be faster (Less Cycles) ?
    > and Which one will consume more memory ?
    > Any ideas ?


    its been explained to you why this is a bad question.
    If you *really* want to know (and I can think of
    no good reason) then write a program and look at the assembler
    generated. You will find the code very similar in all cases.
     
    , Jan 28, 2009
    #16
  17. kenny

    Bartc Guest

    "karthikbalaguru" <> wrote in message
    news:...
    > On Jan 24, 5:59 am, Martin Ambuhl <> wrote:
    >> 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 */
    >>

    >
    > But, why does C provide some many
    > ways of infinite loop ?


    Why does it provide so many ways of looping?

    >
    > Which one of the above methods consume
    > less cycles ?


    For an /infinite/ loop, will it really make any difference? Everyone will
    still be dead long before.

    --
    Bartc
     
    Bartc, Jan 28, 2009
    #17
  18. karthikbalaguru wrote:

    > But, i am really interested to know considering
    > the loop body does not do anything. (It is just an infinite loop
    > without body.)
    >
    > I am intested to know this because various forms of
    > programming an infinite loop is supported .
    >
    > Also, are there any specific scenarios in which
    > those methods should be used ?
    >
    > Any ideas ?


    This is just silly. If a loop is truly infinite it has an infinite
    number of cycles performed in an infinite amount of time. So constuct 1
    has infinity[1] cycles / infinity[1] time and construct 2 has
    infinity[2] cycles / infinity[2] time.
     
    Martin Ambuhl, Jan 28, 2009
    #18
  19. "karthikbalaguru" <> wrote in message
    news:...
    (snipped far too long quote)
    >
    > Considering a x86 machine, which one will be faster (Less Cycles) ?
    > Any ideas ?
    >
    > Thx in advans,
    > Karthik Balaguru


    Any real compiler is likely to optimize all those kinds of loops into:

    foo: goto foo;

    But in case it won't (maybe a very dumb/old compiler), writing that directly
    shouldn't result in more than just a jump. There is no guarantee though, the
    compiler is free to (for example) insert a whole load of nops between the
    label and the goto (or anywhere else). I would trust most C compilers to be
    "sane" in addition to "conforming to the C standard" though.
     
    Harold Aptroot, Jan 28, 2009
    #19
  20. kenny

    Eric Sosman Guest

    karthikbalaguru wrote:
    > On Jan 24, 5:59 am, Martin Ambuhl <> wrote:
    >> 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 */
    >>

    >
    > But, why does C provide some many
    > ways of infinite loop ?
    >
    > Which one of the above methods consume
    > less cycles ?


    All of them consume more cycles than your machine can execute.
    Aleph-null is aleph-null is aleph-null, and the Universe will rot
    away before any of these loops completes.

    (Unless you've got a DeathStation 9000, of course. The DS9K
    is so fast it can complete a well-optimized infinite loop in twenty
    seconds.)

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jan 28, 2009
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vedran Vukotic

    infinite loop unexpectly dies

    Vedran Vukotic, Mar 2, 2006, in forum: Perl
    Replies:
    0
    Views:
    4,492
    Vedran Vukotic
    Mar 2, 2006
  2. Alexander Bosch

    Infinite loop when using Server.Transfer

    Alexander Bosch, Oct 28, 2004, in forum: ASP .Net
    Replies:
    11
    Views:
    914
    Steven Cheng[MSFT]
    Nov 10, 2004
  3. Alexander Bosch

    Infinite loop when using Server.Transfer

    Alexander Bosch, Oct 31, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    561
    Steven Cheng[MSFT]
    Nov 1, 2004
  4. Replies:
    5
    Views:
    624
    benben
    Jan 31, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    444
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page