Using equality in 'for' loop - why not?

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

    #16
    Re: Using equality in 'for' loop - why not?

    Thanks to everyone - really! I just wish FAQs etc. Made this clear.
    Running several FAQs myself I know how things I see as 'obvious' aren't
    to a newbie and there's oftenno way to guess your way to expertise.

    Request for addition to the n/g's FAQ? In that vein I'd also request I'd
    also suggest a note to explain the safe version of var i++. Users of the
    wonderful JS Lint are told it is dangerous to use. Why, is less clear;
    put your JS smarts aside and try to find an explanation online - that a
    newbie understands - and more importantly, a description of what you use
    instead (I believe it is i+=1 ).

    Thanks again to everyone for haring, now I truly understand why the
    books are so cra*p and what I was doing wrong after reading them.

    Regards

    Mark


    Comment

    • Thomas 'PointedEars' Lahn

      #17
      Re: Using equality in 'for' loop - why not?

      Mark Anderson wrote:
      [color=blue]
      > Thanks to everyone - really! I just wish FAQs etc. Made this clear.
      > Running several FAQs myself I know how things I see as 'obvious' aren't
      > to a newbie and there's oftenno way to guess your way to expertise.
      >
      > Request for addition to the n/g's FAQ?[/color]

      Please don't, that is well-documented behavior. RTFM, i.e. see the
      ECMAScript (3) Specification, the Netscape Core JavaScript Reference(s)
      and the MSDN Library. `for' is not different in ECMAScript and its
      implementations from any other language of the C family. It is one
      of the first things people should learn when they learn how to code,
      not when they learn how to code J(ava)Script.


      PointedEars

      Comment

      • Thomas 'PointedEars' Lahn

        #18
        Re: Using equality in 'for' loop - why not?

        Mark Anderson wrote:
        [color=blue]
        > "Stephen Chalmers" <me@here.com> wrote in message
        > news:40b78067_2 @mk-nntp-2.news.uk.tisca li.com...[/color]

        Please do not write attribution novels, see
        <http://netmeister.org/news/learn2quote.htm l>
        [color=blue][color=green]
        >> "Hywel" [...] wrote [...][color=darkred]
        >>> [...] Mark Anderson [...] says...
        >>>> This doesn't work...
        >>>> x = 5;
        >>>> for (y=1; (y==5); y+=1) {
        >>>> alert(x * y);
        >>>> }
        >>>
        >>> This fails because after the first loop "y" becomes 12.[/color]
        >>
        >> Why is the first iteration performed and how does 'y' become 12?
        >> [...][/color]
        >
        > Wait a minute, a 'for' loop excutes *until* test it true. [...][/color]

        If you think of `test' as in

        for (init; test; loop_stmt)
        {
        ...
        }

        that is wrong. A "for" loop executes *as long as* `test'
        evaluates to `true'. Such a loop is head-controlled,
        terminated and thus equivalent to

        init;
        while (test)
        {
        ...
        loop_stmt;
        }

        That is not equivalent to

        init;
        do
        {
        ...
        loop_stmt;
        }
        while (test)

        as you apparently think.
        [color=blue]
        > Ergo, on iteration #1, i=1 and test is 'false' and lop #1 executes[/color]

        No, it does not, unless `test' is true, i.e. "y" evaluates to 5. Since 1
        has been assigned to "y" previously in the initialization part, it does not
        evaluate to 5, so the loop is never executed.


        PointedEars

        Comment

        • Mick White

          #19
          Re: Using equality in 'for' loop - why not?

          Mark Anderson wrote:

          [color=blue]
          >
          > Why didn't the blokes (perhaps ladies?) who wrote the spec just say
          > explicitly don't use ==/!=/===/!== in for loop test if they don't work.
          > I've looked at the NS docs - and there's no indication that these aren't
          > allowed.
          >[/color]

          There are situations when you can use "== !="

          y=5
          for(i=0;y==5;i+ +){
          alert(y);
          if(i==2){y=4;}
          }
          alert(y)

          Mick

          [color=blue]
          > Mark
          >
          >[/color]

          Comment

          • Stephen Chalmers

            #20
            Re: Using equality in 'for' loop - why not?


            "Thomas 'PointedEars' Lahn" <PointedEars@nu rfuerspam.de> wrote in message
            news:40B7C721.2 050107@PointedE ars.de...[color=blue]
            > `for' is not different in ECMAScript and its
            > implementations from any other language of the C family.[/color]

            Hmmm. In ECMAScript, the scope of variables declared in the initialisation
            statement, is not the same as it is in C++.

            --
            S.C.


            Comment

            • Thomas 'PointedEars' Lahn

              #21
              Re: Using equality in 'for' loop - why not?

              Stephen Chalmers wrote:
              [color=blue]
              > "Thomas 'PointedEars' Lahn" <PointedEars@nu rfuerspam.de> wrote in message
              > news:40B7C721.2 050107@PointedE ars.de...[/color]

              Please do not write attribution novels, see
              <http://netmeister.org/news/learn2quote.htm l>
              [color=blue][color=green]
              >> `for' is not different in ECMAScript and its
              >> implementations from any other language of the C family.[/color]
              >
              > Hmmm. In ECMAScript, the scope of variables declared in the initialisation
              > statement, is not the same as it is in C++.[/color]

              ACK. I was referring to the test expression, this is a C-style `for'.


              PointedEars

              Comment

              • Dr John Stockton

                #22
                Re: Using equality in 'for' loop - why not?

                JRS: In article <c97mqu$ihk$1$8 300dec7@news.de mon.co.uk>, seen in
                news:comp.lang. javascript, Mark Anderson <mark@notmeyear dley.demon.co.u k[color=blue]
                > posted at Fri, 28 May 2004 16:45:50 :[/color]
                [color=blue]
                >A 'for' loop takes 3 arguments (initialize; test; increment). The 'test'
                >must equate as true or false[/color]

                ISTM that this is not a matter for the FAQ, but that it is a matter for
                tutorials.

                In different languages, loops are expressed in many different ways; but
                ISTM that in the relevant respect there are exactly two different
                classes.

                For any loop, there is an initialisation, there is a change-between-
                loops, and there is a condition. The condition is always equivalent to
                a Boolean - that is inevitable, though Booleanisation may be implicit.

                One class of loop has a termination condition; false to run.
                One class of loop has a continuation condition; true to run.

                The first class is exemplified by
                Algol's for J := 3 step 6 until 9 // J=9
                Pascal's for J := 8 down to 4 // J=4
                repeat Inc(J) ; ... until J=9
                (the first two could be interpreted with <=, and are probably
                implemented that way; but the mind is likely to consider the condition
                as "finish after J equals given value");
                and the second by
                Javascript's for (J=3 ; J<=9 ; J+=6)
                Pascal's while J<9 do begin Inc(J) ; ... end ;

                In the second, the Boolean is usually, but not always, an ordered
                comparison - > >= < <=, explicit or implicit. However :
                for (J=new Date();J.getMon th()==4;J=new Date()) {}
                for (J=new Date();J.getMon th()!=5;J=new Date()) {}
                should, ISTM, each loop until June.


                The OP has been giving a termination condition where a continuation
                condition is required.


                There is another dichotomy, that of whether the finalisation condition
                is evaluated before or after the loop body
                while (new Date()<12345678 90000) {}
                do {} while (new Date()<12345678 90000)
                each of which should loop until 2009-02-13 Fri 23:31:30 GMT. IIRC, a
                FORTRAN IV loop tested at the end, so the loop was always executed once;
                but an Algol 60 loop tested at the beginning.

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                Comment

                • Lasse Reichstein Nielsen

                  #23
                  Re: Using equality in 'for' loop - why not?

                  "Mark Anderson" <mark@notmeyear dley.demon.co.u k> writes:
                  [color=blue]
                  > Shame no books tutorials stoop so low as to explain this simple fact.[/color]

                  What about:
                  <URL:http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/reference/stmt.html#10048 04>
                  <URL:http://msdn.microsoft. com/library/en-us/script56/html/js56jsstmfor.as p?frame=false>
                  [color=blue]
                  > I now know why I won't use ++ etc., except in the exceptional cases such
                  > as Klaus just described.[/color]

                  I always use ++, and can see no problem with that. It's just the comparison
                  that should usually not be an "equals-to" test.

                  /L
                  --
                  Lasse Reichstein Nielsen - lrn@hotpop.com
                  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                  'Faith without judgement merely degrades the spirit divine.'

                  Comment

                  • rh

                    #24
                    Re: Using equality in 'for' loop - why not?

                    Lasse Reichstein Nielsen wrote:
                    [color=blue]
                    > "Mark Anderson" <mark@notmeyear dley.demon.co.u k> writes:
                    >[color=green]
                    > > Shame no books tutorials stoop so low as to explain this simple fact.[/color]
                    >
                    > What about:
                    > <URL:http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/reference/stmt.html#10048 04>
                    > <URL:http://msdn.microsoft. com/library/en-us/script56/html/js56jsstmfor.as p?frame=false>[/color]

                    Both references appear to be satisfactory for the description of the
                    for "condition" .

                    However, the latter reference describes the "initialization ",
                    "condition" , and "increment" as being required. The former describes
                    the "condition" as optional, with the potential mistaken implication
                    that the other two are required.

                    In fact, the form

                    for (;;); // Infinite loop, by the way

                    is compliant with ECMA-262/3. Later browsers (don't know about earlier
                    ones) accept the above form, including MS IE 6, so it appears it may
                    be just the documentation that is not entirely "with the program". :)

                    ../rh

                    Comment

                    Working...