how to do an infinite loop

Discussion in 'C Programming' started by rami, Mar 1, 2006.

  1. rami

    rami Guest

    please everybody ,can anyone tell me how to do an infinite loop in C
    rami, Mar 1, 2006
    #1
    1. Advertising

  2. rami

    Guest

    while (1)
    {
    // you code here
    }
    , Mar 1, 2006
    #2
    1. Advertising

  3. rami wrote:
    > 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
    Nick Keighley, Mar 1, 2006
    #3
  4. 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.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Mar 1, 2006
    #4
  5. rami

    pemo Guest

    rami wrote:
    > 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.


    --
    ==============
    Not a pedant
    ==============
    pemo, Mar 1, 2006
    #5
  6. pemo wrote:
    > rami wrote:
    > > 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.


    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.

    --
    BR, Vladimir
    Vladimir S. Oka, Mar 1, 2006
    #6
  7. rami

    John Bode Guest

    rami wrote:
    > 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.
    John Bode, Mar 1, 2006
    #7
  8. rami

    pemo Guest

    Vladimir S. Oka wrote:
    > pemo wrote:
    >> rami wrote:
    >>> 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.

    >
    > 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 :)

    --
    ==============
    Not a pedant
    ==============
    pemo, Mar 1, 2006
    #8
  9. rami

    John F Guest

    "pemo" wrote:
    > Vladimir S. Oka wrote:
    >> pemo wrote:
    >>> rami wrote:
    >>>> please everybody ,can anyone tell me how to do an infinite loop in C


    <snip> [while(1) vs. for(;;)]

    >> 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.


    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.

    --
    regards
    John
    John F, Mar 1, 2006
    #9
  10. rami

    Lew Pitcher Guest

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

    John Bode wrote:
    > rami wrote:
    >> 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);


    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-----
    Lew Pitcher, Mar 1, 2006
    #10
  11. Nick Keighley wrote:
    > rami wrote:
    >
    >>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;


    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

    --
    I am the "ILOVEGNU" signature virus. Just copy me to your
    signature. This email was infected under the terms of the GNU
    General Public License.
    August Karlstrom, Mar 1, 2006
    #11
  12. rami wrote:
    > please everybody ,can anyone tell me how to do an infinite loop in C


    I would do

    #include <stdbool.h>
    ....
    while (true) { ... }


    August

    --
    I am the "ILOVEGNU" signature virus. Just copy me to your
    signature. This email was infected under the terms of the GNU
    General Public License.
    August Karlstrom, Mar 1, 2006
    #12
  13. rami

    CBFalconer Guest

    John Bode wrote:
    > rami wrote:
    >
    >> 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.


    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/>
    CBFalconer, Mar 1, 2006
    #13
  14. August Karlstrom wrote:
    > Nick Keighley wrote:
    > > rami wrote:


    > >>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;

    >
    > 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.


    --
    Nick Keighley
    Nick Keighley, Mar 1, 2006
    #14
  15. rami

    Pedro Graca Guest

    Lew Pitcher wrote:
    >
    > John Bode wrote:
    >> rami wrote:
    >>> 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);

    >
    > 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(); }

    --
    If you're posting through Google read <http://cfaj.freeshell.org/google>
    Pedro Graca, Mar 1, 2006
    #15
  16. Nick Keighley wrote:
    > August Karlstrom wrote:
    > > Nick Keighley wrote:
    > > > rami wrote:

    >
    > > >>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;

    > >
    > > 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.


    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.

    --
    BR, Vladimir
    Vladimir S. Oka, Mar 1, 2006
    #16
  17. rami

    Charles Krug Guest

    On 2006-03-01, rami <> wrote:
    > 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.
    Charles Krug, Mar 2, 2006
    #17
  18. Charles Krug said:

    > On 2006-03-01, rami <> wrote:
    >> please everybody ,can anyone tell me how to do an infinite loop in C
    >>

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


    And does it work?

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Mar 2, 2006
    #18
  19. rami

    CBFalconer Guest

    Charles Krug wrote:
    > On 2006-03-01, rami <> wrote:
    >
    >> 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.


    #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/>
    CBFalconer, Mar 2, 2006
    #19
  20. rami

    Ico Guest

    Richard Heathfield <> wrote:
    > 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.


    Done yet ?

    --
    :wq
    ^X^Cy^K^X^C^C^C^C
    Ico, Mar 2, 2006
    #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,450
    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:
    867
    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:
    538
    Steven Cheng[MSFT]
    Nov 1, 2004
  4. Replies:
    5
    Views:
    590
    benben
    Jan 31, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    352
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page