for(;;) or while(1)?

Discussion in 'C Programming' started by Rick, Sep 30, 2003.

  1. Rick

    Rick Guest

    Hi,

    For portability, can an issue arise if we use while(1) for an infinite
    loop in C? If so, should we then use for(;;)? Thanks,

    Rick
    Rick, Sep 30, 2003
    #1
    1. Advertising

  2. In article <3f79749d$>, Rick wrote:
    > Hi,
    >
    > For portability, can an issue arise if we use while(1) for an infinite
    > loop in C? If so, should we then use for(;;)? Thanks,


    Some C compilers may generate slightly different machine code,
    but they ought to always be functionally identical. Choosing
    one instead of the other will not break a program or make it
    unportable.


    --
    Andreas Kähäri
    Andreas Kahari, Sep 30, 2003
    #2
    1. Advertising

  3. Rick

    pete Guest

    Rick wrote:
    >
    > Hi,
    >
    > For portability, can an issue arise if we use while(1)
    > for an infinite loop in C?


    My compiler generates a warning for that kind of code,
    when I have the warning level high, where I like it.

    > If so, should we then use for(;;)?


    My compiler doesn't generate a warning for that code,
    so that's what I use.

    --
    pete
    pete, Sep 30, 2003
    #3
  4. "Rick" <rrquick@nospam-com> wrote in message
    news:3f79749d$...
    | For portability, can an issue arise if we use while(1) for an infinite
    | loop in C? If so, should we then use for(;;)? Thanks,

    It should come down to the same.

    However, I use for(;;) because I use some compilers which send out
    warnings of the kind: "Constant used as conditional expression".

    go figure...
    --
    http://ivan.vecerina.com
    Ivan Vecerina, Sep 30, 2003
    #4
  5. Rick wrote:
    > For portability, can an issue arise if we use while(1) for an infinite
    > loop in C? If so, should we then use for(;;)? Thanks,


    There aren't any real portability issues, but the `1' seems too
    arbitrary to me (as pointed out in <news:>,
    which is where I first saw that objection, I think. Using `true'
    seems less objectionable, somehow, if you have a C99 (or C++) compiler
    (or define it yourself).

    Jeremy.
    Jeremy Yallop, Sep 30, 2003
    #5
  6. Greetings.

    In article <blc263$aiv7m$-berlin.de>, Jeremy Yallop
    wrote:
    >> For portability, can an issue arise if we use while(1) for an infinite
    >> loop in C? If so, should we then use for(;;)? Thanks,

    >
    > There aren't any real portability issues, but the `1' seems too
    > arbitrary to me


    More so than while(42)? There's plenty of precedent for using '1' as a
    generic truth constant; less so for other nonzero numbers.

    --
    _
    _V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
    / |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
    (7_\\ http://www.nothingisreal.com/ >< To finish what you
    Tristan Miller, Sep 30, 2003
    #6
  7. Rick

    j Guest

    "Rick" <rrquick@nospam-com> wrote in message
    news:3f79749d$...
    > Hi,
    >
    > For portability, can an issue arise if we use while(1) for an infinite
    > loop in C?


    No.

    > If so, should we then use for(;;)? Thanks,
    >


    You use for(;;) when you do not want to imply a test, otherwise for that
    purpose, you use while(1)
    The practical result will be the same for all but the very stupidest
    compilers.

    > Rick
    >
    j, Sep 30, 2003
    #7
  8. Rick

    Noah Roberts Guest

    Rick wrote:
    > Hi,
    >
    > For portability, can an issue arise if we use while(1) for an infinite
    > loop in C? If so, should we then use for(;;)? Thanks,
    >
    > Rick
    >


    Well using it directly in code it has no matter, but what about this:

    #define forever for(;;)

    do
    {
    ...x...
    } forever;

    NR
    Noah Roberts, Sep 30, 2003
    #8
  9. Rick

    Rick Guest

    > Jeremy Yallop writes:
    > I wouldn't expect *any* constant to get rid of the warning message.


    Wasn't that what Ivan said.. the compiler issues a "Constant used as
    conditional expression" warning. How will *any* constant get rid of the
    warning when the warning is about the use of a constant itself?

    > I much prefer the while(1) construct. The for:)::) looks to me like someone is
    > trying to impress someone with their arcane knowledge.


    I don't think it's about making impressions, I'd say it's a matter of
    taste. I myself use for(;;).. I believe it's much clearer to what's
    happening; and like you said "while implies a condition that will vary",
    that's just why while(constant) can be a little misleading at first
    glance. My compiler once gave me a warning for while(constant), I
    wondered if using such a constract was non-portable, but that's clearly
    not the case.

    Rick
    Rick, Sep 30, 2003
    #9
  10. Noah Roberts wrote:
    > Rick wrote:
    >
    >> Hi,
    >>
    >> For portability, can an issue arise if we use while(1) for an infinite
    >> loop in C? If so, should we then use for(;;)? Thanks,
    >>
    >> Rick
    >>

    >
    > Well using it directly in code it has no matter, but what about this:
    >
    > #define forever for(;;)
    >
    > do
    > {
    > ...x...
    > } forever;
    >
    > NR
    >


    I didn't think this was valid syntax (after macro substitution):
    do
    {
    /* ...x... */
    } for (;;); /* after preprocessor substitution */

    My understanding is the syntax is:
    do
    {
    } while();

    I don't believe you can replace the "while" with "for" in this
    syntax. I've never heard of a "do-for" loop.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Sep 30, 2003
    #10
  11. Rick

    osmium Guest

    Jeremy Yallop writes:

    > > For portability, can an issue arise if we use while(1) for an infinite
    > > loop in C? If so, should we then use for(;;)? Thanks,

    >
    > There aren't any real portability issues, but the `1' seems too
    > arbitrary to me (as pointed out in

    <news:>,
    > which is where I first saw that objection, I think. Using `true'
    > seems less objectionable, somehow, if you have a C99 (or C++) compiler
    > (or define it yourself).


    I wouldn't expect *any* constant to get rid of the warning message. while
    implies a condition that will vary. I can see the reason for the message
    but I'm glad the compiler I have doesn't issue such a warning. I much
    prefer the while(1) construct. The for:)::) looks to me like someone is
    trying to impress someone with their arcane knowledge.
    osmium, Sep 30, 2003
    #11
  12. Rick

    Mike Wahler Guest

    "Rick" <rrquick@nospam-com> wrote in message
    news:3f79749d$...
    > Hi,
    >
    > For portability, can an issue arise if we use while(1) for an infinite
    > loop in C? If so, should we then use for(;;)? Thanks,


    for(;;) has seven characters, and
    while(1) has eight, so the former should
    weight slightly less, thus be a bit more
    portable.

    Also, if carrying the construct in a backback,
    the rounded edges of 'f' 'o' and 'r' should
    be more comfortable than those pointy 'w', 'h',
    'i', and 'l' characters. :)

    Seriously, either one is equally portable. Use
    whichever you find most natural, unless you have
    coding standards to follow.

    -Mike
    Mike Wahler, Sep 30, 2003
    #12
  13. Rick wrote:
    >> Jeremy Yallop writes:

    [snip]

    I didn't write any of the quoted text. Please be more careful with
    your attributions.

    Jeremy.
    Jeremy Yallop, Sep 30, 2003
    #13
  14. Rick

    Joe Wright Guest

    pete wrote:
    >
    > Rick wrote:
    > >
    > > Hi,
    > >
    > > For portability, can an issue arise if we use while(1)
    > > for an infinite loop in C?

    >
    > My compiler generates a warning for that kind of code,
    > when I have the warning level high, where I like it.
    >
    > > If so, should we then use for(;;)?

    >
    > My compiler doesn't generate a warning for that code,
    > so that's what I use.
    >
    > --
    > pete


    It's just your compiler's rant about programming style. The while (1)
    and for (;;) constructs are identical.
    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Sep 30, 2003
    #14
  15. Rick

    Minti Guest

    Rick <rrquick@nospam-com> wrote in message news:<3f79749d$>...
    > Hi,
    >
    > For portability, can an issue arise if we use while(1) for an infinite
    > loop in C? If so, should we then use for(;;)? Thanks,
    >
    > Rick


    Well I use

    while ( 1 )

    if I wish to.

    BUT

    I use

    for ( ; ; )

    If I wish to.

    In terms of performance any *intelligent* compiler would produce the
    same binary code. Though a compiler made possibly as a *experimental
    project* **might** not do the optimization and might include a sort of
    condition for checking the truth value of the expression in while,
    which would be true, always. OTOH if you love goto you might also
    wanna use

    inf_loop:
    ......
    goto inf_loop;

    The last one would have the same performence level as that of a for( ;
    ; ).

    why not use

    for ( ; 1 ; )

    which is *completly* semantically equivalent to the while ( 1 )



    --
    Imanpreet Singh Arora
    imanpreet_arora AT yahoo DOT co DOT in

    Aut inveniam viam aut faciam
    Minti, Sep 30, 2003
    #15
  16. Rick

    Rick Guest

    Sorry about that Jeremy, I meant to qoute Osmium.


    Rick

    Jeremy Yallop wrote:
    > Rick wrote:
    >
    >>>Jeremy Yallop writes:

    >
    > [snip]
    >
    > I didn't write any of the quoted text. Please be more careful with
    > your attributions.
    >
    > Jeremy.
    Rick, Sep 30, 2003
    #16
  17. Thomas Matthews wrote:

    > Noah Roberts wrote:
    >> Rick wrote:
    >>
    >>> Hi,
    >>>
    >>> For portability, can an issue arise if we use while(1) for an infinite
    >>> loop in C? If so, should we then use for(;;)? Thanks,
    >>>
    >>> Rick
    >>>

    >>
    >> Well using it directly in code it has no matter, but what about this:
    >>
    >> #define forever for(;;)
    >>
    >> do
    >> {
    >> ...x...
    >> } forever;
    >>
    >> NR
    >>

    >
    > I didn't think this was valid syntax (after macro substitution):


    That was, I believe, the point he was making (i.e. that for(;;) and while(1)
    are not interchangeable in all cases).

    For what it's worth, I think while(condition) is clearer than either for(;;)
    or while(1).

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Sep 30, 2003
    #17
  18. Rick

    osmium Guest

    Rick writes:

    > Sorry about that Jeremy, I meant to qoute Osmium.
    >
    >
    > Rick
    >
    > Jeremy Yallop wrote:
    > > Rick wrote:
    > >
    > >>>Jeremy Yallop writes:

    > >
    > > [snip]
    > >
    > > I didn't write any of the quoted text. Please be more careful with
    > > your attributions.


    I didn't write it either. Try again.
    --
    Osmium
    osmium, Sep 30, 2003
    #18
  19. On Tue, 30 Sep 2003, Ridimz wrote:
    >
    > "osmium" <> wrote in message
    > >
    > > I didn't write it either. Try again.
    > > --
    > > Osmium

    >
    > Yea you did! Read your reply again! It's attached just in case
    > you need to see which one!


    Maybe osmium means that someone else has been posting to Usenet
    using his account... ;-)

    Please don't post attachments to non-binaries groups.
    A simple link to Google Groups, or, even better, a quick
    reminder to "look three messages up the darn thread and
    *read* it" would have sufficed.

    And in general, don't use spaces in filenames. Some
    uudecode clients might not even parse your attachment
    correctly (I dunno what any RFC on the topic says; I'm
    just pointing out the possibility).

    -Arthur
    Arthur J. O'Dwyer, Oct 1, 2003
    #19
  20. Rick

    Minti Guest

    Thomas Matthews <> wrote in message news:<AOheb.1533$>...
    > Noah Roberts wrote:
    > > Rick wrote:
    > >
    > >> Hi,
    > >>
    > >> For portability, can an issue arise if we use while(1) for an infinite
    > >> loop in C? If so, should we then use for(;;)? Thanks,
    > >>
    > >> Rick
    > >>

    > >
    > > Well using it directly in code it has no matter, but what about this:
    > >
    > > #define forever for(;;)
    > >
    > > do
    > > {
    > > ...x...
    > > } forever;
    > >
    > > NR
    > >

    >
    > I didn't think this was valid syntax (after macro substitution):
    > do
    > {
    > /* ...x... */
    > } for (;;); /* after preprocessor substitution */
    >
    > My understanding is the syntax is:
    > do
    > {
    > } while();
    >


    Nitpick: replace while() with while(expr)

    > I don't believe you can replace the "while" with "for" in this
    > syntax. I've never heard of a "do-for" loop.


    Me neither. A lot of books would need to be appended if it existed.


    --
    Imanpreet Singh Arora
    iimmaannpprreeeett_aarroorraa@@yyaahhoooo..ccoo..iinn

    Singularize the letters above to send mail.
    Minti, Oct 1, 2003
    #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. ^CrazyCoder^
    Replies:
    3
    Views:
    7,255
    Terrence Benade
    Sep 15, 2003
  2. David
    Replies:
    1
    Views:
    486
    David
    Jul 19, 2004
  3. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,228
    Tim Slattery
    Mar 30, 2005
  4. Sam
    Replies:
    0
    Views:
    414
  5. Timothy Fitz

    while 1 vs while True

    Timothy Fitz, Dec 13, 2004, in forum: Python
    Replies:
    16
    Views:
    625
    Terry Reedy
    Dec 15, 2004
Loading...

Share This Page