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

[jallist] Bug in FOR loops ?

Expand Messages
  • Benjamin Petersen
    It seems to me that when using the following FOR construct it might not do what you think is should do : VAR BYTE x = 255 FOR x LOOP portb = x delay_1ms --
    Message 1 of 3 , May 3, 1999
    • 0 Attachment
      It seems to me that when using the following "FOR" construct it might not do
      what you think is should do :


      VAR BYTE x = 255
      FOR x LOOP
      portb = x
      delay_1ms -- just so you don't say that the eye can't see it
      END LOOP

      What I expect is a binary counter on port b, but all LED's are always on.
      So my conclusion must be that even so the loop counts down, x isn't used as
      the counter, so in order to get what we want we have to do this :

      VAR BYTE x = 255
      WHILE x != 0 LOOP
      portb = x
      delay_1ms -- just so you don't say that the eye can't see it
      x = x - 1
      END LOOP

      Regards
      Benjamin Petersen


      ------------------------------------------------------------------------
      eGroup home: http://www.eGroups.com/group/jallist
      http://www.eGroups.com - Simplifying group communications
    • w. v. ooijen / f. hanneman
      ... do ... as ... What you get is not always what you expect! for N loop .. end loop loops N times, where N is an expression. When you summply a variable for N
      Message 2 of 3 , May 3, 1999
      • 0 Attachment
        > From: Benjamin Petersen <benjamin@...>
        >
        > It seems to me that when using the following "FOR" construct it might not
        do
        > what you think is should do :
        >
        >
        > VAR BYTE x = 255
        > FOR x LOOP
        > portb = x
        > delay_1ms -- just so you don't say that the eye can't see it
        > END LOOP
        >
        > What I expect is a binary counter on port b, but all LED's are always on.
        > So my conclusion must be that even so the loop counts down, x isn't used
        as
        > the counter, so in order to get what we want we have to do this :

        What you get is not always what you expect!
        for N loop .. end loop
        loops N times, where N is an expression.
        When you summply a variable for N its value is taken, the
        loop executes N times, and the variable is unaffected.....

        The manual just states
        A for statement causes the statements within the for to be executed the
        indicated number of times.
        That might need some more explanation in the next version.

        When I have the time I will expand the for statement with a control
        variable, start value
        end value step etc. But a plain for loop without such parafranalia will
        still be
        faster/smaller because it can count down and use decfsz.

        BTW Benjamin: I made some modifications mainly in jpic.jal and got your
        code
        down from ~770 to ~665 instructions. I think I can do still better, I'll
        let you know.

        Wouter.

        ------------------------------------------------------------------------
        eGroup home: http://www.eGroups.com/group/jallist
        http://www.eGroups.com - Simplifying group communications
      • Doc
        Hi Have you tried a longer delay, such as 100ms? ... From: Benjamin Petersen To: Date: Monday, May 03, 1999 7:48
        Message 3 of 3 , May 3, 1999
        • 0 Attachment
          Hi

          Have you tried a longer delay, such as 100ms?


          -----Original Message-----
          From: Benjamin Petersen <benjamin@...>
          To: <jallist@egroups.com>
          Date: Monday, May 03, 1999 7:48 AM
          Subject: [jallist] Bug in FOR loops ?


          >It seems to me that when using the following "FOR" construct it might not
          do
          >what you think is should do :
          >
          >
          >VAR BYTE x = 255
          >FOR x LOOP
          > portb = x
          > delay_1ms -- just so you don't say that the eye can't see it
          >END LOOP
          >
          >What I expect is a binary counter on port b, but all LED's are always on.
          >So my conclusion must be that even so the loop counts down, x isn't used as
          >the counter, so in order to get what we want we have to do this :
          >
          >VAR BYTE x = 255
          >WHILE x != 0 LOOP
          > portb = x
          > delay_1ms -- just so you don't say that the eye can't see it
          > x = x - 1
          >END LOOP
          >
          >Regards
          > Benjamin Petersen
          >
          >
          >------------------------------------------------------------------------
          >What's everyone looking at? Check out the Top40 most requested stocks!
          >Plus quotes, charts, news, portfolios, mutual funds, and discussion.
          >All free, fast, and easy. Visit: http://clickhere.egroups.com/click/63
          >
          >
          >eGroup home: http://www.eGroups.com/group/jallist
          >http://www.eGroups.com - Simplifying group communications
          >
          >
          >


          ------------------------------------------------------------------------
          eGroup home: http://www.eGroups.com/group/jallist
          http://www.eGroups.com - Simplifying group communications
        Your message has been successfully submitted and would be delivered to recipients shortly.