while (1) vs. for ( ;; )

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pete

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

    Christian Bau wrote:[color=blue]
    >
    > In article <43127F62.1A60@ mindspring.com> ,
    > pete <pfiland@mindsp ring.com> wrote:
    >[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]
    > >
    > > 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);
    >[/color]

    It doesn't like that one either.

    --
    pete

    Comment

    • Guillaume

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

      Randy Howard wrote:[color=blue]
      > const hell_freezes_ov er = false;
      >
      > repeat
      > something;
      > until hell_freezes_ov er;
      >
      > Seems to convey the intent quite clearly. :-)[/color]

      That is, if hell means anything to you. If it doesn't, then it does not
      convey much. ;-)

      Comment

      • Richard Tobin

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

        In article <deu31g$dbn$1@e nyo.uwa.edu.au> ,
        Chris McDonald <chris@csse.uwa .edu.au> wrote:
        [color=blue]
        >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.[/color]

        If you're writing code for beginners to read you may well come to
        different conclusions from those where you're writing a program to be
        maintained by experienced programmers.

        -- Richard

        Comment

        • Anton Petrusevich

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

          Chris McDonald wrote:
          [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]

          What about following?
          #defined loop_forever for(;;)
          --
          Anton Petrusevich

          Comment

          • Anton Petrusevich

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

            pete wrote:
            [color=blue][color=green][color=darkred]
            >> > 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);
            >>[/color]
            > It doesn't like that one either.[/color]

            What about following?
            while(1 == 1) {
            ....
            }
            and
            do {
            ....
            } while(1 == 0);
            --
            Anton Petrusevich

            Comment

            • akarl

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

              Anton Petrusevich wrote:[color=blue]
              > Chris McDonald wrote:
              >
              >[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]
              >
              >
              > What about following?
              > #defined loop_forever for(;;)[/color]

              Defining a new language with the macro preprocessor is seldom a good idea.

              August

              Comment

              • Anton Petrusevich

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

                akarl wrote:
                [color=blue][color=green][color=darkred]
                >>> Chris McDonald wrote:[/color][/color][/color]
                [color=blue][color=green][color=darkred]
                >>> 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]
                [color=blue][color=green]
                >> What about following?
                >> #define loop_forever for(;;)[/color]
                > Defining a new language with the macro preprocessor is seldom a good idea.[/color]

                I agree with you, but I was asking Chris McDonald. :)
                --
                Anton Petrusevich

                Comment

                • Ben Pfaff

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

                  Anton Petrusevich <casus@att-ltd.biz> writes:
                  [color=blue]
                  > What about following?
                  > #defined loop_forever for(;;)[/color]

                  As long as you want to be "cute":
                  #define ever ;;
                  for (ever) { ... }
                  (I would never use this.)
                  --
                  int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
                  \n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
                  );while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
                  );}return 0;}

                  Comment

                  • Alan Balmer

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

                    On Sun, 28 Aug 2005 21:36:51 -0400, Michael B Allen
                    <mba2000@ioplex .com> 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]
                    They do exactly the same thing. I use the "for ever" form because some
                    of the compilers I use give a warning on while(1).
                    --
                    Al Balmer
                    Balmer Consulting
                    removebalmercon sultingthis@att .net

                    Comment

                    • Kenneth Brody

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

                      Walter Roberson wrote:[color=blue]
                      >
                      > In article <pan.2005.08.29 .01.36.51.40360 4@ioplex.com>,
                      > Michael B Allen <mba2000@ioplex .com> wrote:[color=green]
                      > >Should there be any preference between the following logically equivalent
                      > >statements?[/color]
                      >[color=green]
                      > > while (1) {
                      > >vs.
                      > > for ( ;; ) {[/color][/color]

                      I typically read them as "while true" and "forever".
                      [color=blue][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][/color]

                      They are functionally identical, and will possibly generate the same
                      machine code.
                      [color=blue]
                      > 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.[/color]

                      I typically declare an "int done=0;" and then use "while (!done)" in
                      such cases. It just reads better to me and allows a "clean" exit from
                      the loop.
                      [color=blue]
                      > 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.[/color]

                      True.

                      --
                      +-------------------------+--------------------+-----------------------------+
                      | Kenneth J. Brody | www.hvcomputer.com | |
                      | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
                      +-------------------------+--------------------+-----------------------------+
                      Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>


                      Comment

                      • Alan Balmer

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

                        On Mon, 29 Aug 2005 08:42:47 +0000 (UTC), Richard Heathfield
                        <invalid@invali d.invalid> wrote:
                        [color=blue]
                        >Michael B Allen said:
                        >[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) 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").[/color]

                        It often happens that there are multiple exit conditions. Is it your
                        preference to set a "get out" flag for these situations? That's my
                        preference, though I don't feel strongly about it (and I'm likely to
                        use multiple function returns, too, when a single return is too
                        contrived.)
                        --
                        Al Balmer
                        Balmer Consulting
                        removebalmercon sultingthis@att .net

                        Comment

                        • Randy Howard

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

                          Guillaume wrote
                          (in article <43130cab$0$212 96$7a628cd7@new s.club-internet.fr>):
                          [color=blue]
                          > Randy Howard wrote:[color=green]
                          >> const hell_freezes_ov er = false;
                          >>
                          >> repeat
                          >> something;
                          >> until hell_freezes_ov er;
                          >>
                          >> Seems to convey the intent quite clearly. :-)[/color]
                          >
                          > That is, if hell means anything to you. If it doesn't, then it does not
                          > convey much. ;-)[/color]

                          It seems that the expression "when hell freezes over" is not as
                          widespread as I thought. :-)



                          --
                          Randy Howard (2reply remove FOOBAR)

                          Comment

                          • Richard Heathfield

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

                            Alan Balmer said:
                            [color=blue]
                            > On Mon, 29 Aug 2005 08:42:47 +0000 (UTC), Richard Heathfield
                            > <invalid@invali d.invalid> wrote:
                            >[color=green]
                            >>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").[/color]
                            >
                            > It often happens that there are multiple exit conditions. Is it your
                            > preference to set a "get out" flag for these situations?[/color]

                            Yes: done = 1;
                            [color=blue]
                            > That's my
                            > preference, though I don't feel strongly about it (and I'm likely to
                            > use multiple function returns, too, when a single return is too
                            > contrived.)[/color]

                            I'm quite unlikely to use multiple returns, although I seem to recall that I
                            have done so on occasion, when in a tearing hurry.

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

                            Email rjh at the above domain

                            Comment

                            • Guillaume

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

                              Randy Howard wrote:[color=blue]
                              > It seems that the expression "when hell freezes over" is not as
                              > widespread as I thought. :-)[/color]

                              Lol, I was just being difficult. ;-)

                              Comment

                              • pete

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

                                Anton Petrusevich wrote:[color=blue]
                                >
                                > pete wrote:
                                >[color=green][color=darkred]
                                > >> > while(1) gets a warning on my compiler.
                                > >> > for(;;) doesn't.
                                > >> Could you tell us which compiler? And could you check what it thinks
                                > >> about
                                > >> do {
                                > >> ...
                                > >> } while (0);
                                > >>[/color]
                                > > It doesn't like that one either.[/color]
                                >
                                > What about following?
                                > while(1 == 1) {
                                > ...
                                > }
                                > and
                                > do {
                                > ...
                                > } while(1 == 0);[/color]

                                My compiler warns about a constant expression being
                                he condition in a loop.
                                (1 == 1) and (1 == 0) are both such constant expressions.

                                --
                                pete

                                Comment

                                Working...