Loading ...
Sorry, an error occurred while loading the content.

Loop

Expand Messages
  • Douglas Crockford
    A lot of my loops want to exit from the middle. I wish that JavaScript had a loop statement. loop { ... } But it doesn t, so instead of writing loop I must
    Message 1 of 7 , Jun 14 10:53 AM
    • 0 Attachment
      A lot of my loops want to exit from the middle. I wish that JavaScript had a loop statement.

      loop {
      ...
      }

      But it doesn't, so instead of writing loop I must write either while (true) or for (;;). I don't like either, so I am not sure which one to prefer.

      Any opinions?
    • Mark Volkmann
      On Tue, Jun 14, 2011 at 12:53 PM, Douglas Crockford ... I like while (true) much more! -- R. Mark Volkmann Object Computing, Inc. [Non-text portions of this
      Message 2 of 7 , Jun 14 11:04 AM
      • 0 Attachment
        On Tue, Jun 14, 2011 at 12:53 PM, Douglas Crockford
        <douglas@...>wrote:

        > A lot of my loops want to exit from the middle. I wish that JavaScript
        > had a loop statement.
        >
        > loop {
        > ...
        > }
        >
        > But it doesn't, so instead of writing loop I must write either while (true)
        > or for (;;). I don't like either, so I am not sure which one to prefer.
        >
        > Any opinions?
        >
        I like "while (true)" much more!

        --
        R. Mark Volkmann
        Object Computing, Inc.


        [Non-text portions of this message have been removed]
      • Erik Eckhardt
        I wish loops could ENTER in the middle, then `while` expression could be the exit condition, but not the initial entry condition. This would solve a common
        Message 3 of 7 , Jun 14 11:25 AM
        • 0 Attachment
          I wish loops could ENTER in the middle, then `while` expression could be the
          exit condition, but not the initial entry condition.

          This would solve a common problem where code either has to be repeated or
          the exit condition is in the middle of the loop. Here are examples of these:

          a = r.getNextRecord();
          while (!a.eof) {
          ___//do something with non-eof a
          ___a = r.getNextRecord(); //repeated statement
          }

          while (true) { // annoying forced loop
          ___a = r.getNextRecord();
          ___if (a.eof) { break; } // with break in the middle
          ___//do something with non-eof a
          }

          I propose something like this (very rough idea):

          using {
          ___a = r.getNextRecord();
          } until (a.eof) {
          ___// do something with non-eof a
          }

          in fact that is awfully like the for loop, except we want the initializer
          and the incrementer to be the same code

          for (a = r.getNextRecord(); !a.eof; a = r.getNextRecord()) {
          ___// do something with non-eof a
          }

          So perhaps a second, optional parameter to while, whose use would make the
          first parameter a loop initializer that runs before each loop before the
          condition is tested:

          dowhile (a = r.getNextRecord(); !a.eof) {
          ___// do something with non-eof a
          }

          On Tue, Jun 14, 2011 at 11:04 AM, Mark Volkmann
          <r.mark.volkmann@...>wrote:

          >
          >
          > On Tue, Jun 14, 2011 at 12:53 PM, Douglas Crockford
          > <douglas@...>wrote:
          >
          >
          > > A lot of my loops want to exit from the middle. I wish that JavaScript
          > > had a loop statement.
          > >
          > > loop {
          > > ...
          > > }
          > >
          > > But it doesn't, so instead of writing loop I must write either while
          > (true)
          > > or for (;;). I don't like either, so I am not sure which one to prefer.
          > >
          > > Any opinions?
          > >
          > I like "while (true)" much more!
          >
          > --
          > R. Mark Volkmann
          > Object Computing, Inc.
          >
          > [Non-text portions of this message have been removed]
          >
          >
          >


          [Non-text portions of this message have been removed]
        • Emmett Pickerel
          I definitely prefer while(true), but I d argue that if at all possible, the test for a loop should be the exit point. Exiting from the middle (return excepted)
          Message 4 of 7 , Jun 14 11:25 AM
          • 0 Attachment
            I definitely prefer while(true), but I'd argue that if at all possible, the test for a loop should be the exit point. Exiting from the middle (return excepted) smacks a little of GOTO to me.
          • Erik Eckhardt
            This is a stylistic consideration. Some people think that one should never exit a loop except at the end. It s similar to the stricture to never return from a
            Message 5 of 7 , Jun 14 12:36 PM
            • 0 Attachment
              This is a stylistic consideration. Some people think that one should never
              exit a loop except at the end. It's similar to the stricture to never return
              from a function except at the end, which has many strong proponents on both
              sides.

              My position is different, that a rigid rule on this is too much and that an
              intelligent programmer should be able to use the tool he thinks is best in
              the situation, including terminating a loop in the middle or returning from
              a function other than the end if he likes. GOTO was a terrible way to
              control flow, but that doesn't mean it was logically always bad, especially
              when instead of a fixed target (go to line 23) it functions as "leave the
              current block/scope" an entirely different thing.

              There are many, many reputable programmers who take both positions. This
              makes it hard to declare one as the absolutely best way.

              Erik

              On Tue, Jun 14, 2011 at 11:25 AM, Emmett Pickerel
              <emmett.thesane@...>wrote:

              >
              >
              > I definitely prefer while(true), but I'd argue that if at all possible, the
              > test for a loop should be the exit point. Exiting from the middle (return
              > excepted) smacks a little of GOTO to me.
              >
              >
              >


              [Non-text portions of this message have been removed]
            • Michael S. Mikowski
              I favor the LABEL:while(true){ ... break LABEL;} construction, as its intent seems most obvious. However, I find it often good to avoid this construction
              Message 6 of 7 , Jun 14 2:02 PM
              • 0 Attachment
                I favor the LABEL:while(true){ ... break LABEL;} construction, as its intent
                seems most obvious.

                However, I find it often good to avoid this construction altogether and put an
                upper bound on the loop, e.g: LABEL:for ( var i; i<10000; i++){... break
                LABEL;}


                On Tuesday, June 14, 2011 12:36:47 PM Erik Eckhardt wrote:
                > This is a stylistic consideration. Some people think that one should never
                > exit a loop except at the end. It's similar to the stricture to never
                > return from a function except at the end, which has many strong proponents
                > on both sides.
                >
                > My position is different, that a rigid rule on this is too much and that an
                > intelligent programmer should be able to use the tool he thinks is best in
                > the situation, including terminating a loop in the middle or returning from
                > a function other than the end if he likes. GOTO was a terrible way to
                > control flow, but that doesn't mean it was logically always bad, especially
                > when instead of a fixed target (go to line 23) it functions as "leave the
                > current block/scope" an entirely different thing.
                >
                > There are many, many reputable programmers who take both positions. This
                > makes it hard to declare one as the absolutely best way.
                >
                > Erik
                >
                > On Tue, Jun 14, 2011 at 11:25 AM, Emmett Pickerel
                >
                > <emmett.thesane@...>wrote:
                > > I definitely prefer while(true), but I'd argue that if at all possible,
                > > the test for a loop should be the exit point. Exiting from the middle
                > > (return excepted) smacks a little of GOTO to me.
                >
                > [Non-text portions of this message have been removed]
                >
                >
                >
                > ------------------------------------
                >
                > Yahoo! Groups Links
                >
                >
                >

                [Non-text portions of this message have been removed]
              • John Hawkinson
                Douglas Crockford wrote on Tue, 14 Jun 2011 ... for(;;) is quite potentially confusing to novice programmers. while(true) is much
                Message 7 of 7 , Jun 14 2:05 PM
                • 0 Attachment
                  Douglas Crockford <douglas@...> wrote on Tue, 14 Jun 2011
                  at 17:53:41 -0000 in <it8775+kvdj@...>:

                  > But it doesn't, so instead of writing loop I must write either while
                  > (true) or for (;;). I don't like either, so I am not sure which one
                  > to prefer.

                  for(;;) is quite potentially confusing to novice programmers.
                  while(true) is much clearer.

                  I hope we're not discussing what JSLint should complain about though,
                  since both are legitimate stylistic choices that are unambiguous and not
                  language hazards.

                  Erik Eckhardt <erik@...> wrote on Tue, 14 Jun 2011
                  at 11:25:39 -0700 in <BANLkTik0xdFw16bCvxAOF7HLTuHmjjfXLQ@...>:
                  ...
                  > So perhaps a second, optional parameter to while, whose use would make the
                  > first parameter a loop initializer that runs before each loop before the
                  > condition is tested:

                  Err, it's not as if we are changing the language.

                  --jhawk
                Your message has been successfully submitted and would be delivered to recipients shortly.