while (1) vs. for ( ;; )

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael B Allen

    while (1) vs. for ( ;; )

    Should there be any preference between the following logically equivalent
    statements?

    while (1) {

    vs.

    for ( ;; ) {

    I suspect the answer is "no" but I'd like to know what the consensus is
    so that it doesn't blink through my mind anymore when I type it.

    Mike

  • caduardo21@yahoo.com.br

    #2
    Re: while (1) vs. for ( ;; )

    You're right. The answer is no. They'll both do the exact same thing.

    Comment

    • Walter Roberson

      #3
      Re: while (1) vs. for ( ;; )

      In article <pan.2005.08.29 .01.36.51.40360 4@ioplex.com>,
      Michael B Allen <mba2000@ioplex .com> wrote:[color=blue]
      >Should there be any preference between the following logically equivalent
      >statements?[/color]
      [color=blue]
      > while (1) {[/color]
      [color=blue]
      >vs.[/color]
      [color=blue]
      > for ( ;; ) {[/color]
      [color=blue]
      >I suspect the answer is "no" but I'd like to know what the consensus is
      >so that it doesn't blink through my mind anymore when I type it.[/color]

      Just my opinion, but I would tend to use them in slightly different
      contexts.

      I would tend to use while(1) when I was looping for something that
      will occur at an indeterminate future time, such as EOF or an
      alarm signal.

      I would tend to use for(;;) when I was looping for something that
      will occur within a bounded time, but which is inconvenient to
      express through loop control variables, such as iterating through
      a set of data structures until a certain property is detected.

      To give it a different slant: for(;;) conveys "iteration" , whereas
      while(1) conveys "repetition " that is not necessarily iterative.

      But as the two work out the same in the end, the choice amounts
      to no more than a hint to a human reader.
      --
      Daylight is a trademark of OSRAM SYLVANIA INC.

      Comment

      • Guillaume

        #4
        Re: while (1) vs. for ( ;; )

        Question of style, nothing else.
        Personally, I tend to prefer 'for (;;)', but that's just my style.

        It's like asking which is better:

        if () {
        }

        or:

        if ()
        {
        }

        I use the second form exclusively for various reasons, but it's a matter
        of style. Doesn't change the code one bit.

        Comment

        • Robert Gamble

          #5
          Re: while (1) vs. for ( ;; )

          Michael B Allen wrote:[color=blue]
          > Should there be any preference between the following logically equivalent
          > statements?
          >
          > while (1) {
          >
          > vs.
          >
          > for ( ;; ) {
          >
          > I suspect the answer is "no" but I'd like to know what the consensus is
          > so that it doesn't blink through my mind anymore when I type it.[/color]

          Many consider the latter preferable, it can be read as "for-ever" and
          makes the intention of the programmer clearer. "while" implies a
          predicate which may not always be true whereas "for" does not.

          Robert Gamble

          Comment

          • Chris McDonald

            #6
            Re: while (1) vs. for ( ;; )

            Michael B Allen <mba2000@ioplex .com> writes:
            [color=blue]
            >Should there be any preference between the following logically equivalent
            >statements?[/color]
            [color=blue]
            > while (1) {
            >vs.
            > for ( ;; ) {[/color]
            [color=blue]
            >I suspect the answer is "no" but I'd like to know what the consensus is
            >so that it doesn't blink through my mind anymore when I type it.[/color]


            Preference? Why not move on to something whose intention is clear:

            while(true) {

            --
            Chris.

            Comment

            • Ben Pfaff

              #7
              Re: while (1) vs. for ( ;; )

              Chris McDonald <chris@csse.uwa .edu.au> writes:
              [color=blue]
              > Michael B Allen <mba2000@ioplex .com> writes:
              >[color=green]
              >>Should there be any preference between the following logically equivalent
              >>statements?[/color]
              >[color=green]
              >> while (1) {
              >>vs.
              >> for ( ;; ) {[/color]
              >[color=green]
              >>I suspect the answer is "no" but I'd like to know what the consensus is
              >>so that it doesn't blink through my mind anymore when I type it.[/color]
              >
              > Preference? Why not move on to something whose intention is clear:
              >
              > while(true) {[/color]

              In what way are the former two statements' intentions not clear?
              Both of them obviously loop "forever".
              --
              A competent C programmer knows how to write C programs correctly,
              a C expert knows enough to argue with Dan Pop, and a C expert
              expert knows not to bother.

              Comment

              • pete

                #8
                Re: while (1) vs. for ( ;; )

                Michael B Allen wrote:[color=blue]
                >
                > Should there be any preference between the following logically equivalent
                > statements?
                >
                > while (1) {
                >
                > vs.
                >
                > for ( ;; ) {
                >
                > I suspect the answer is "no" but I'd
                > like to know what the consensus is
                > so that it doesn't blink through my mind anymore when I type it.[/color]

                while(1) gets a warning on my compiler.
                for(;;) doesn't.

                --
                pete

                Comment

                • Chris McDonald

                  #9
                  Re: while (1) vs. for ( ;; )

                  Ben Pfaff <blp@cs.stanfor d.edu> writes:
                  [color=blue]
                  >Chris McDonald <chris@csse.uwa .edu.au> writes:[/color]
                  [color=blue][color=green]
                  >> Michael B Allen <mba2000@ioplex .com> writes:
                  >>[color=darkred]
                  >>>Should there be any preference between the following logically equivalent
                  >>>statements ?[/color]
                  >>[color=darkred]
                  >>> while (1) {
                  >>>vs.
                  >>> for ( ;; ) {[/color]
                  >>[color=darkred]
                  >>>I suspect the answer is "no" but I'd like to know what the consensus is
                  >>>so that it doesn't blink through my mind anymore when I type it.[/color]
                  >>
                  >> Preference? Why not move on to something whose intention is clear:
                  >>
                  >> while(true) {[/color][/color]
                  [color=blue]
                  >In what way are the former two statements' intentions not clear?
                  >Both of them obviously loop "forever".[/color]


                  I would suggest that both while(1) and for(;;) are potentially unclear
                  to a person (undergrad. student) seeing C for the first time
                  (while assuming they haven't seen Java or C++, either).

                  OK, I suggest that while(true) is *clearer*.

                  --
                  Chris.

                  Comment

                  • Keith Thompson

                    #10
                    Re: while (1) vs. for ( ;; )

                    Chris McDonald <chris@csse.uwa .edu.au> writes:
                    [...][color=blue]
                    > I would suggest that both while(1) and for(;;) are potentially unclear
                    > to a person (undergrad. student) seeing C for the first time
                    > (while assuming they haven't seen Java or C++, either).[/color]

                    I would suggest that anyone who doesn't understand while(1) or for(;;)
                    isn't going to have much chance of understanding what follows it.

                    --
                    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                    We must do something. This is something. Therefore, we must do this.

                    Comment

                    • Chris McDonald

                      #11
                      Re: while (1) vs. for ( ;; )

                      Keith Thompson <kst-u@mib.org> writes:
                      [color=blue]
                      >Chris McDonald <chris@csse.uwa .edu.au> writes:
                      >[...][color=green]
                      >> I would suggest that both while(1) and for(;;) are potentially unclear
                      >> to a person (undergrad. student) seeing C for the first time
                      >> (while assuming they haven't seen Java or C++, either).[/color][/color]
                      [color=blue]
                      >I would suggest that anyone who doesn't understand while(1) or for(;;)
                      >isn't going to have much chance of understanding what follows it.[/color]


                      I'm sure that everyone reading this newsgroup understands the *role*
                      of while(1) and for(;;) but, as suggested, consider how a beginner
                      may read these the first few times.

                      I believe that it's these little points that can make one textbook a
                      better introduction than another. It's difficult to argue for the
                      continued writing of new code that could be written in a clearer fashion.

                      --
                      Chris.

                      Comment

                      • Randy Howard

                        #12
                        Re: while (1) vs. for ( ;; )

                        Robert Gamble wrote
                        (in article
                        <1125282206.880 912.35420@f14g2 000cwb.googlegr oups.com>):
                        [color=blue]
                        > Michael B Allen wrote:[color=green]
                        >> Should there be any preference between the following logically equivalent
                        >> statements?
                        >>
                        >> while (1) {
                        >>
                        >> vs.
                        >>
                        >> for ( ;; ) {
                        >>
                        >> I suspect the answer is "no" but I'd like to know what the consensus is
                        >> so that it doesn't blink through my mind anymore when I type it.[/color]
                        >
                        > Many consider the latter preferable, it can be read as "for-ever" and
                        > makes the intention of the programmer clearer. "while" implies a
                        > predicate which may not always be true whereas "for" does not.[/color]

                        This is where Pascal has an advantage. (I probably have some of
                        the syntax wrong, I haven't written any Pascal since the late
                        80s)

                        const hell_freezes_ov er = false;

                        repeat
                        something;
                        until hell_freezes_ov er;

                        Seems to convey the intent quite clearly. :-)

                        --
                        Randy Howard (2reply remove FOOBAR)

                        Comment

                        • Richard Heathfield

                          #13
                          Re: while (1) vs. for ( ;; )

                          Michael B Allen said:
                          [color=blue]
                          > Should there be any preference between the following logically equivalent
                          > statements?
                          >
                          > while (1) {
                          >
                          > vs.
                          >
                          > for ( ;; ) {
                          >
                          > I suspect the answer is "no" but I'd like to know what the consensus is
                          > so that it doesn't blink through my mind anymore when I type it.[/color]

                          while(1) will be flagged by many compilers as "conditiona l expression is
                          constant" or some such wording, whereas for(;;) will not be. Consequently,
                          for(;;) is preferable out of these two choices.

                          Personally, I prefer neither choice! I would rather have the loop control
                          statement explicitly document the exit condition (unless there genuinely
                          isn't one, such as might be the case in an electronic appliance like a
                          microwave oven, where "forever" can roughly be translated as "whilst power
                          is being supplied to the appliance").


                          --
                          Richard Heathfield
                          "Usenet is a strange place" - dmr 29 July 1999

                          Email rjh at the above domain

                          Comment

                          • Christian Bau

                            #14
                            Re: while (1) vs. for ( ;; )

                            In article <43127F62.1A60@ mindspring.com> ,
                            pete <pfiland@mindsp ring.com> wrote:
                            [color=blue]
                            > Michael B Allen wrote:[color=green]
                            > >
                            > > Should there be any preference between the following logically equivalent
                            > > statements?
                            > >
                            > > while (1) {
                            > >
                            > > vs.
                            > >
                            > > for ( ;; ) {
                            > >
                            > > I suspect the answer is "no" but I'd
                            > > like to know what the consensus is
                            > > so that it doesn't blink through my mind anymore when I type it.[/color]
                            >
                            > while(1) gets a warning on my compiler.
                            > for(;;) doesn't.[/color]

                            Could you tell us which compiler? And could you check what it thinks
                            about

                            do {
                            ...
                            } while (0);

                            ?

                            Comment

                            • akarl

                              #15
                              Re: while (1) vs. for ( ;; )

                              Randy Howard wrote:[color=blue]
                              > Robert Gamble wrote
                              > (in article
                              > <1125282206.880 912.35420@f14g2 000cwb.googlegr oups.com>):
                              >
                              >[color=green]
                              >>Michael B Allen wrote:
                              >>[color=darkred]
                              >>>Should there be any preference between the following logically equivalent
                              >>>statements ?
                              >>>
                              >>>while (1) {
                              >>>
                              >>>vs.
                              >>>
                              >>>for ( ;; ) {
                              >>>
                              >>>I suspect the answer is "no" but I'd like to know what the consensus is
                              >>>so that it doesn't blink through my mind anymore when I type it.[/color]
                              >>
                              >>Many consider the latter preferable, it can be read as "for-ever" and
                              >>makes the intention of the programmer clearer. "while" implies a
                              >>predicate which may not always be true whereas "for" does not.[/color]
                              >
                              >
                              > This is where Pascal has an advantage. (I probably have some of
                              > the syntax wrong, I haven't written any Pascal since the late
                              > 80s)
                              >
                              > const hell_freezes_ov er = false;
                              >
                              > repeat
                              > something;
                              > until hell_freezes_ov er;
                              >
                              > Seems to convey the intent quite clearly. :-)[/color]

                              Well, in C we have the similar do-while statement. In Modula on the
                              other hand there is a real exit-in-the-middle loop construct:

                              LOOP
                              ...
                              IF someCondition THEN EXIT END;
                              ...
                              END


                              August

                              Comment

                              Working...