Using equality in 'for' loop - why not?

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

    Using equality in 'for' loop - why not?

    A 'for' loop takes 3 arguments (initialize; test; increment). The 'test'
    must equate as true or false

    This doesn't work...
    x = 5;
    for (y=1; (y==5); y+=1) {
    alert(x * y);
    }
    ...nor does...
    x = 5;
    for (y=1; (y===5); y+=1) {
    alert(x * y);
    }
    ....but this does..
    x = 5;
    for (y=1; (y<6); y+=1) {
    alert(x * y);
    }

    Why do the first two fail? If i is value 5 then i==5 is true, as is
    i===5.

    Can anyone explain what I'm missing here?

    Regards




  • Hywel

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

    In article <c97mqu$ihk$1$8 300dec7@news.de mon.co.uk>, Mark Anderson
    says...[color=blue]
    > A 'for' loop takes 3 arguments (initialize; test; increment). The 'test'
    > must equate as true or false
    >
    > This doesn't work...
    > x = 5;
    > for (y=1; (y==5); y+=1) {
    > alert(x * y);
    > }[/color]

    This fails because after the first loop "y" becomes 12. Try
    for (y=1; y!=5; y++)

    [color=blue]
    > Why do the first two fail? If i is value 5 then i==5 is true, as is
    > i===5.[/color]

    The loop executes while the condition is true. In your example you
    assign 1 to y, so the condition y==5 is false.

    [color=blue]
    > Can anyone explain what I'm missing here?[/color]

    You're missing the documentation:


    --
    Hywel I do not eat quiche


    Comment

    • Erwin Moller

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

      Mark Anderson wrote:

      Hi Mark
      [color=blue]
      > A 'for' loop takes 3 arguments (initialize; test; increment). The 'test'
      > must equate as true or false
      >
      > This doesn't work...
      > x = 5;
      > for (y=1; (y==5); y+=1) {
      > alert(x * y);
      > }[/color]

      yes, I think it works.
      Just not as expected.
      Your 'test' fails the first time because y=1 and not 5.

      Think of for-loop as follows, maybe that helps:

      for (initialize; test; increment){
      statements;
      }

      is equal to:

      initialize;
      while(test) {
      statements;
      increment;
      }


      Now you can explain the rest too. :-)
      [color=blue]
      > ..nor does...
      > x = 5;
      > for (y=1; (y===5); y+=1) {
      > alert(x * y);
      > }
      > ...but this does..
      > x = 5;
      > for (y=1; (y<6); y+=1) {
      > alert(x * y);
      > }
      >
      > Why do the first two fail? If i is value 5 then i==5 is true, as is
      > i===5.
      >
      > Can anyone explain what I'm missing here?
      >
      > Regards[/color]


      Regards,
      Erwin Moller

      Comment

      • Lee

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

        Mark Anderson said:[color=blue]
        >
        >A 'for' loop takes 3 arguments (initialize; test; increment). The 'test'
        >must equate as true or false
        >
        >This doesn't work...
        >x = 5;
        >for (y=1; (y==5); y+=1) {
        > alert(x * y);
        >}
        >..nor does...
        >x = 5;
        >for (y=1; (y===5); y+=1) {
        > alert(x * y);
        >}
        >...but this does..
        >x = 5;
        >for (y=1; (y<6); y+=1) {
        > alert(x * y);
        >}
        >
        >Why do the first two fail? If i is value 5 then i==5 is true, as is
        >i===5.
        >
        >Can anyone explain what I'm missing here?[/color]

        You're setting x=5 and y=1 and seem to be surprised that y!=5.
        Without knowing you, it's hard to guess whether this is oversight
        or if you really are missing something.

        Comment

        • Stephen Chalmers

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


          "Hywel" <hyweljenkins@h otmail.com> wrote in message
          news:MPG.1b2172 c03e6af95e98972 7@news.individu al.net...[color=blue]
          > In article <c97mqu$ihk$1$8 300dec7@news.de mon.co.uk>, Mark Anderson
          > says...[color=green]
          > > A 'for' loop takes 3 arguments (initialize; test; increment). The 'test'
          > > must equate as true or false
          > >
          > > This doesn't work...
          > > x = 5;
          > > for (y=1; (y==5); y+=1) {
          > > alert(x * y);
          > > }[/color]
          >
          > This fails because after the first loop "y" becomes 12.[/color]

          Why is the first iteration performed and how does 'y' become 12?

          --
          S.C.


          Comment

          • Mick White

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

            Stephen Chalmers wrote:

            [color=blue]
            > Why is the first iteration performed and how does 'y' become 12?
            >[/color]
            x = 5;
            for (y=1; y==5; y+=1) {
            alert(x * y);
            }[color=blue]
            > S.C.[/color]
            It doesn't.
            The loop never starts, since y doesn't equal 5.

            Mick

            Comment

            • Mark Anderson

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

              Lee

              "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
              news:c97pch02pu 7@drn.newsguy.c om...[color=blue]
              > Mark Anderson said:[color=green]
              > >
              > >A 'for' loop takes 3 arguments (initialize; test; increment). The[/color][/color]
              'test'[color=blue][color=green]
              > >must equate as true or false
              > >
              > >This doesn't work...
              > >x = 5;
              > >for (y=1; (y==5); y+=1) {
              > > alert(x * y);
              > >}
              > >..nor does...
              > >x = 5;
              > >for (y=1; (y===5); y+=1) {
              > > alert(x * y);
              > >}
              > >...but this does..
              > >x = 5;
              > >for (y=1; (y<6); y+=1) {
              > > alert(x * y);
              > >}
              > >
              > >Why do the first two fail? If i is value 5 then i==5 is true, as is
              > >i===5.
              > >
              > >Can anyone explain what I'm missing here?[/color]
              >[/color]
              [color=blue]
              > Without knowing you, it's hard to guess whether this is oversight
              > or if you really are missing something.
              >[/color]
              [color=blue]
              > You're setting x=5 and y=1 and seem to be surprised that y!=5.[/color]

              And so I should be! I'm saying "start with y=1 ad while y is not 5
              (ergo less than or equal to 5) do the loop and increment by five.

              Your explanation is implying I'm testing in loop #1...

              x = 5;
              for (y=5; (y===5); etc...

              I feel oppressed by all the right-brain thinking here.

              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.

              Please folks I'm not an engineer or a logic PhD. Could posters please
              *explain* their put-downs rather than just say you're wrong. I asked the
              question both because I don't understand and I wish to understand why.
              So far all that I've read is "I'm cleverer than you" answers telling me
              I'm wrong. I know! Just please explain why...

              Regards

              Mark


              Comment

              • Klaus Johannes Rusch

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

                Mark Anderson wrote:[color=blue]
                > And so I should be! I'm saying "start with y=1 ad while y is not 5
                > (ergo less than or equal to 5) do the loop and increment by five.[/color]

                You set y to 1 and then loop while y is equal to 5. Since y is not equal
                to 5 before the first iteration, the loop body is never executed.
                [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]

                They are allowed, and there are cases where they make sense, for example

                for (counter = 0; stillrunning == 1; counter++) {
                if (something) stillrunning = 0;
                }

                The condition can be more complex, too -- in any case the way the loop
                is processed is such that the condition is checked *before* executing
                the loop, so if your condition is not true to start with, the loop is
                executed zero times.

                Hope that helps

                --
                Klaus Johannes Rusch
                KlausRusch@atme dia.net

                Comment

                • Evertjan.

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

                  Mark Anderson wrote on 28 mei 2004 in comp.lang.javas cript:[color=blue]
                  > This doesn't work...
                  > x = 5;
                  > for (y=1; (y==5); y+=1) {
                  > alert(x * y);
                  >}[/color]

                  Yes it works

                  start with y=1
                  then do something WHILE y==5

                  WHICH boolean is not true from the start,
                  so no alert output !!!
                  [color=blue]
                  > ..nor does...
                  > x = 5;
                  > for (y=1; (y===5); y+=1) {
                  > alert(x * y);
                  >}[/color]

                  Yes it works

                  start with y=1
                  then do something WHILE y===5

                  WHICH boolean is NOT true from the start,
                  so no alert output !!!
                  [color=blue]
                  > ...but this does..
                  > x = 5;
                  > for (y=1; (y<6); y+=1) {
                  > alert(x * y);
                  >}[/color]

                  Yes it surely works

                  start with y=1
                  then do something WHILE y<6

                  WHICH boolean is true when y=1, 2, 3, 4, 5

                  So 5 times an alert output

                  =============== =====

                  btw:
                  1 the () around y<6 are not needed
                  2 y+=1 is the same as y++
                  3 alerts in a loop can drive you crazy
                  4 using var can sometimes save you trouble when in a function,
                  because it leaves global values alone

                  Commonly this is used:

                  var x = 5;
                  for (var y=1; y<6; y++) {
                  document.write( x*y + "<br>");
                  }


                  --
                  Evertjan.
                  The Netherlands.
                  (Please change the x'es to dots in my emailaddress)

                  Comment

                  • Evertjan.

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

                    Klaus Johannes Rusch wrote on 28 mei 2004 in comp.lang.javas cript:
                    [color=blue]
                    > They are allowed, and there are cases where they make sense, for example
                    >
                    > for (counter = 0; stillrunning == 1; counter++) {
                    > if (something) stillrunning = 0;
                    >}[/color]

                    No, no sense, but this does:

                    var stillrunning = 17
                    for (counter = 0; stillrunning == 1; counter++) {
                    if (counter==2300 or somethingelse) stillrunning = 1;
                    }

                    though I would prefer:

                    var stillrunning = true
                    for (counter = 0; stillrunning ; counter++) {
                    if (counter==2301 or somethingelse) stillrunning = false;
                    }


                    --
                    Evertjan.
                    The Netherlands.
                    (Please change the x'es to dots in my emailaddress)

                    Comment

                    • Mark Anderson

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

                      Stephen,

                      "Stephen Chalmers" <me@here.com> wrote in message
                      news:40b78067_2 @mk-nntp-2.news.uk.tisca li.com...[color=blue]
                      >
                      > "Hywel" <hyweljenkins@h otmail.com> wrote in message
                      > news:MPG.1b2172 c03e6af95e98972 7@news.individu al.net...[color=green]
                      > > In article <c97mqu$ihk$1$8 300dec7@news.de mon.co.uk>, Mark Anderson
                      > > says...[color=darkred]
                      > > > A 'for' loop takes 3 arguments (initialize; test; increment). The[/color][/color][/color]
                      'test'[color=blue][color=green][color=darkred]
                      > > > must equate as true or false
                      > > >
                      > > > This doesn't work...
                      > > > x = 5;
                      > > > for (y=1; (y==5); y+=1) {
                      > > > alert(x * y);
                      > > > }[/color]
                      > >
                      > > This fails because after the first loop "y" becomes 12.[/color]
                      >
                      > Why is the first iteration performed and how does 'y' become 12?
                      >
                      > --
                      > S.C.[/color]

                      Wait a minute, a 'for' loop excutes *until* test it true. Ergo, on
                      iteration #1, i=1 and test is 'false' and lop #1 executes - except it
                      doesn't. Can you please prove y=12 rather than state such a provactive
                      aasertion without explanation. I posted here to learn not be told I'm
                      too dumb to understand.

                      I don't mind rules as long as they can be understood. It seems the logic
                      on the for loop rests on some abstruse logic that - self-evidently -
                      isn't obvious.

                      Even so I'm a pragmatist and I'll go with the flow but it's a shame no
                      JS book or online tutorial I can find says not to use == or === (or
                      their negative equivalents) in a for loop as the test.

                      Regards

                      Mark


                      Comment

                      • Mark Anderson

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

                        Evertjan,

                        "Evertjan." <exjxw.hannivoo rt@interxnl.net > wrote in message
                        news:Xns94F7DE8 466340eejj99@19 4.109.133.29...[color=blue]
                        > Mark Anderson wrote on 28 mei 2004 in comp.lang.javas cript:[color=green]
                        > > This doesn't work...
                        > > x = 5;
                        > > for (y=1; (y==5); y+=1) {
                        > > alert(x * y);
                        > >}[/color]
                        >
                        > Yes it works
                        >
                        > start with y=1
                        > then do something WHILE y==5
                        >
                        > WHICH boolean is not true from the start,
                        > so no alert output !!!
                        >[color=green]
                        > > ..nor does...
                        > > x = 5;
                        > > for (y=1; (y===5); y+=1) {
                        > > alert(x * y);
                        > >}[/color]
                        >
                        > Yes it works
                        >
                        > start with y=1
                        > then do something WHILE y===5
                        >
                        > WHICH boolean is NOT true from the start,
                        > so no alert output !!!
                        >[color=green]
                        > > ...but this does..
                        > > x = 5;
                        > > for (y=1; (y<6); y+=1) {
                        > > alert(x * y);
                        > >}[/color]
                        >
                        > Yes it surely works
                        >
                        > start with y=1
                        > then do something WHILE y<6
                        >
                        > WHICH boolean is true when y=1, 2, 3, 4, 5
                        >
                        > So 5 times an alert output
                        >
                        > =============== =====
                        >
                        > btw:
                        > 1 the () around y<6 are not needed
                        > 2 y+=1 is the same as y++
                        > 3 alerts in a loop can drive you crazy
                        > 4 using var can sometimes save you trouble when in a function,
                        > because it leaves global values alone
                        >
                        > Commonly this is used:
                        >
                        > var x = 5;
                        > for (var y=1; y<6; y++) {
                        > document.write( x*y + "<br>");
                        > }
                        >
                        >
                        > --
                        > Evertjan.
                        > The Netherlands.
                        > (Please change the x'es to dots in my emailaddress)[/color]

                        And the prize goes to EvertJan. Now it is explained I understand.
                        Shame no books tutorials stoop so low as to explain this simple fact.

                        I now know why I won't use ++ etc., except in the exceptional cases such
                        as Klaus just described.

                        Thanks. My faith in Usenet is restored!

                        Regards

                        Mark


                        Comment

                        • Lee

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

                          Mark Anderson said:[color=blue]
                          >
                          >Lee
                          >
                          >"Lee" <REM0VElbspamtr ap@cox.net> wrote in message
                          >news:c97pch02p u7@drn.newsguy. com...[color=green]
                          >> Mark Anderson said:[color=darkred]
                          >> >
                          >> >A 'for' loop takes 3 arguments (initialize; test; increment). The[/color][/color]
                          >'test'[color=green][color=darkred]
                          >> >must equate as true or false
                          >> >
                          >> >This doesn't work...
                          >> >x = 5;
                          >> >for (y=1; (y==5); y+=1) {
                          >> > alert(x * y);
                          >> >}
                          >> >..nor does...
                          >> >x = 5;
                          >> >for (y=1; (y===5); y+=1) {
                          >> > alert(x * y);
                          >> >}
                          >> >...but this does..
                          >> >x = 5;
                          >> >for (y=1; (y<6); y+=1) {
                          >> > alert(x * y);
                          >> >}
                          >> >
                          >> >Why do the first two fail? If i is value 5 then i==5 is true, as is
                          >> >i===5.
                          >> >
                          >> >Can anyone explain what I'm missing here?[/color]
                          >>[/color]
                          >[color=green]
                          >> Without knowing you, it's hard to guess whether this is oversight
                          >> or if you really are missing something.
                          >>[/color]
                          >[color=green]
                          >> You're setting x=5 and y=1 and seem to be surprised that y!=5.[/color]
                          >
                          >And so I should be! I'm saying "start with y=1 ad while y is not 5
                          >(ergo less than or equal to 5) do the loop and increment by five.[/color]

                          No, you've got the sense of the test wrong, and possibly
                          misunderstand the increment (did you mean to say "increment by one"?).

                          You're saying "start with y=1 and while y *is* 5 do the loop
                          and increment by 1".

                          The loop is performed *while* the condition is true,
                          not *until* it is true.

                          If you want to start with y=1 and increment by 1 while y is
                          not 5, you should use:

                          for (y=1; y<5; y++)

                          Comment

                          • Hywel

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

                            In article <40b78067_2@m k-nntp-2.news.uk.tisca li.com>, Stephen Chalmers
                            says...[color=blue]
                            >
                            > "Hywel" <hyweljenkins@h otmail.com> wrote in message
                            > news:MPG.1b2172 c03e6af95e98972 7@news.individu al.net...[color=green]
                            > > In article <c97mqu$ihk$1$8 300dec7@news.de mon.co.uk>, Mark Anderson
                            > > says...[color=darkred]
                            > > > A 'for' loop takes 3 arguments (initialize; test; increment). The 'test'
                            > > > must equate as true or false
                            > > >
                            > > > This doesn't work...
                            > > > x = 5;
                            > > > for (y=1; (y==5); y+=1) {
                            > > > alert(x * y);
                            > > > }[/color]
                            > >
                            > > This fails because after the first loop "y" becomes 12.[/color]
                            >
                            > Why is the first iteration performed and how does 'y' become 12?[/color]

                            My mistake - I used document.write( y) in a simpler test but forgot to
                            use something to separate the values.

                            Mark - the reason your code is wrong is simple: you're testing to see it
                            y is equal to 5. You're trying you get a loop to work while y is 5. As
                            you start your loop with y=1, it is never 5. How many people have told
                            you that?

                            If you read the JavaScript documentation at
                            http://developer.netscape.com/ you'll see that the syntax of a for loop
                            is quite simple:

                            for ([initial-expression]; [condition]; [increment-expression]) {
                            statements
                            }

                            The fact that the tutorials you're used do not explicitly tell you not
                            to use == or === is simple: the test will never evaluate in the manner
                            that you need for your code to continue. The logic is quite clear. For
                            example, this code
                            for (x=0; x<5; x++)
                            says assign the value zero to x. While x is less than 5 increment it.
                            When x is equal to 5, stop.

                            Your code
                            for (y=1; (y==5); y+=1)
                            says assign the value one to y. While y is equal to 5, increment it.
                            When, and how, will y ever become 5 in your code?

                            I appreciate that there is a learning process involved and that it may
                            not be always clear, but I'm sure you'll find it becomes more obvious
                            the more you use JavaScript.

                            --
                            Hywel I do not eat quiche


                            Comment

                            • Thomas 'PointedEars' Lahn

                              #15
                              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. If we
                              assume that this is only a snippet, it may execute as previous statements
                              may have assigned 5 to y. If we assume that this is the whole code, it
                              never executes, since no value has been assigned to "y" and thus its
                              value is `undefined' which is even with type casting not equal to 5.


                              PointedEars

                              Comment

                              Working...