why does counter not stop?

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

    #1

    why does counter not stop?

    Hi,

    Question about For (...).
    I did this:
    for (i=1;i=5;i++)
    alert(i)

    and i get an infinite loop with value '5'. I thought that 'i=5' was the test
    which must be 'true' in order to execute 'alert(i)'. It's normal that i
    don't get 1 to 4 because in those case, the test is false. When i=5, i get
    alert(i) with 5 but appearantely, i never equals 6.

    By the way, is there a difference between i++ and ++i? I tried both but
    didn't find any difference.

    Thanks
    bob


  • David Dorward

    #2
    Re: why does counter not stop?

    bob wrote:
    [color=blue]
    > Question about For (...).
    > I did this:
    > for (i=1;i=5;i++)
    > alert(i)
    >
    > and i get an infinite loop with value '5'. I thought that 'i=5' was the
    > test which must be 'true' in order to execute 'alert(i)'.[/color]

    Two problems:

    (1) The second parameter of the for loop has to be true to keep looping. It
    will execute the content of block every time it loops.

    (2) "=" is the assignment operator. The numerical test operator is "==".
    [color=blue]
    > It's normal that i don't get 1 to 4 because in those case[/color]

    Nope. The first time round the loop you set i to 1, and then immediately
    afterwards you set it to 5.
    [color=blue]
    >, the test is false.[/color]

    i=5 is always true
    [color=blue]
    > When i=5, i get alert(i) with 5 but appearantely, i never equals 6.[/color]

    Becuase you add one to it, and then set it back to 5 again.
    [color=blue]
    > By the way, is there a difference between i++ and ++i? I tried both but
    > didn't find any difference.[/color]

    It's a case of when the increment happens.

    <script type="text/javascript">
    i = 0;
    document.write (i +'<br>'+ i++ +'<br>'+ i +'<br>'+ ++i +'<br>'+ i);
    </script>

    might make things clear if you follow it through carefully.

    --
    David Dorward http://dorward.me.uk/

    Comment

    • bob

      #3
      Re: why does counter not stop?

      thanks for replying


      "David Dorward" <dorward@yahoo. com> wrote in message
      news:bhnf87$gm$ 1$8300dec7@news .demon.co.uk...[color=blue]
      > bob wrote:
      >[color=green]
      > > Question about For (...).
      > > I did this:
      > > for (i=1;i=5;i++)
      > > alert(i)
      > >
      > > and i get an infinite loop with value '5'. I thought that 'i=5' was the
      > > test which must be 'true' in order to execute 'alert(i)'.[/color]
      >
      > Two problems:
      >
      > (1) The second parameter of the for loop has to be true to keep looping.[/color]
      It[color=blue]
      > will execute the content of block every time it loops.
      >
      > (2) "=" is the assignment operator. The numerical test operator is "==".
      >[color=green]
      > > It's normal that i don't get 1 to 4 because in those case[/color]
      >
      > Nope. The first time round the loop you set i to 1, and then immediately
      > afterwards you set it to 5.
      >[color=green]
      > >, the test is false.[/color]
      >
      > i=5 is always true
      >[color=green]
      > > When i=5, i get alert(i) with 5 but appearantely, i never equals 6.[/color]
      >
      > Becuase you add one to it, and then set it back to 5 again.
      >[color=green]
      > > By the way, is there a difference between i++ and ++i? I tried both but
      > > didn't find any difference.[/color]
      >
      > It's a case of when the increment happens.
      >
      > <script type="text/javascript">
      > i = 0;
      > document.write (i +'<br>'+ i++ +'<br>'+ i +'<br>'+ ++i +'<br>'+ i);
      > </script>
      >
      > might make things clear if you follow it through carefully.
      >
      > --
      > David Dorward http://dorward.me.uk/[/color]


      Comment

      Working...